2010-04-14 33 views
0

之间传递NSMutableArray我有两个视图控制器名称RootViewController和SecondViewController。在FirstViewController我有这样的NSMutableArray在两个UIViewController的

@interface RootViewController : UITableViewController { 

NSMutableArray *allClasses;}@property (nonatomic,retain) NSMutableArray *allClasses; 

在RootViewController的我填充的UITableView与内allClasses

所有对象在我SecondViewController我有

@interface SecondViewController : UIViewController <UITextFieldDelegate,UIPickerViewDelegate> { 

NSMutableArray *arrayStrings;} 

我有增加了新的方法NSString到arrayStrings。我的目标是通过尝试类似于allClasses = arrayStrings的方法将arrayStrings传递给RootViewController。这样,当RootViewController被加载时,它可以填充新的信息。

我如何才能完成这项任务?

回答

0

在RootViewController的实现

-(void)viewWillAppear:(BOOL)animated 

此委托方法。在此方法中

self.allClasses = SecondViewControllerObj.arrayStrings; 

SecondViewControllerObj是SecondViewController的对象RootViewController的声明为成员,这是用来导航到该视图

if(SecondViewControllerObj == nil) 
    { 
     SecondViewControllerObj = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
    } 

     [self.navigationController pushViewController:SecondViewControllerObj animated:YES]; 
0

您需要在第二个视图控制器中具有根视图控制器的引用。在那里你需要将allclasses设置为arrayStrings; 一般而言,您可以通过以下代码找到对推入堆栈的任何视图控制器的引用。

NSArray *viewConts = [[self navigationController] viewControllers]; 
     for(int i=0;i<[viewConts count];i++) 
     { 
      if([[viewConts objectAtIndex:i] isKindOfClass:[RootViewController class]]){ 
       RootViewController *rootController = (RootViewController *)[viewConts objectAtIndex:i]; 
       [rootController setAllClasses:arrayStrings]; 

      } 

     } 

现在,在viewWillAppear of RootViewController中,您需要重新加载视图的内容。

希望这会有所帮助。

感谢,

Madhup

相关问题