2015-02-09 60 views
0

是否可以將代理或NSNotificationCenter觀察者分配給在VC1之後加載的VC2,哪裏發生了發佈?在海報NSNotificationCenter解決方法之後添加觀察者?

我有一個帶有多個VC的tabbar應用程序。 VC 1首先被加載,並且在加載VC2之前觸發一個帖子的動作發生。在VC2中,我需要複製或從VC1獲取數組的引用。

有沒有其他方法可以做到這一點?請幫忙!我已經在這4個小時了。由於

+0

你的意思是VC2自身添加作爲觀察員通知其已經發布?通知帖子什麼時候發佈? vc2什麼時候添加爲觀察者? – KudoCC 2015-02-09 08:17:48

+0

是的!首先裝入VC 1,然後調用VC內部的SelectSelect。那麼VC2加載並添加爲觀察者;基本上這個帖子是在VC1的didSelect方法內完成的。 – 2015-02-09 08:21:02

+0

爲什麼不將VC2作爲觀察者添加到其init方法中? 'viewDidLoad'方法只在第一次訪問'UIViewController'的視圖屬性時被調用。 – KudoCC 2015-02-09 08:27:45

回答

1

切換他們試試這個,它可以幫助你也許是更好的。

FirstViewController

-(void)viewDidAppear:(BOOL)animated 
{ 
    NSArray *temp=[NSArray arrayWithObjects:@"1",@"2", nil]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:@"postArrayObject" object:temp]; 

} 

SecondViewController

-(void)viewWillAppear:(BOOL)animated 
{ 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(objFirstViewController:) name:nil object:nil]; 

} 
-(void)objFirstViewController:(NSNotification *)notification 
{ 
    if ([[notification name]isEqualToString:@"postArrayObject"]) 
    { 
     NSArray *cellData = [notification object]; 
     if (cellData) 
     { 
      UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"WORKING" 
                   message:nil 
                  delegate:nil 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil]; 

      [message show]; 
     } 

    } 

} 
+0

OH!有趣的。我甚至沒有考慮生命週期方法!謝謝 – 2015-02-09 08:34:04

0

,如果你創造一種NavigationController的是擁有所有VC的參考,然後用addSubviewremoveFromSuperview

相關問題