0

我的NSN通知延遲。NS從A級到B級的通知延遲

在myVC,我有兩個按鈕,按鈕a和buttonB。每個鏈接到它們各自的pdf,pdfA和pdfB。有一個按鈕Push,在按下A或B後按下。 Push將用戶帶到有UIWebView的RVC。

我想它,以便通過推壓A或B,所述的UIWebView將顯示相應的pdf文件。爲了調試這個,我將它設置爲不改變pdf,它將使用NSLog顯示文本。但是,直到通過按下不同的按鈕從RVC返回到myVC之後,它纔會起作用。

在myVC.m文件

- (IBAction)open_pictures_A:(id)sender 
{ 

    //do some button alert popup action/whatever button does 


    RootViewController *dataObject = [RootViewController new]; 

    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:dataObject 
                 forKey:@"buttonA"]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" 
                 object:nil 
                 userInfo:userInfo]; 



} 

而且有一個這樣的buttonB,但forKey將是 「buttonB」

在viewDidLoad中

爲myVC.m我有

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" 
                 object:nil]; 

在我RVC,

RVC.h我有

@property (nonatomic, strong) NSString *property1; 

而在RVC.m我

- (void)viewDidLoad 
{ 

    // other stuff 

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(eventListenerDidReceiveNotification:) 
               name:@"MyNotification" 

               object:nil]; 
} 




- (void)eventListenerDidReceiveNotification:(NSNotification *)notif 
{ 
    if ([[notif name] isEqualToString:@"MyNotification"]) 
    { 
     NSLog(@"Successfully received the notification from buttonB!"); 

     NSDictionary *userInfo = notif.userInfo; 
     RootViewController *dataObject = [userInfo objectForKey:@"buttonB"]; 

     // Your response to the notification should be placed here 
     NSLog(@"dataObject.property1 -> %@", dataObject.property1); 

    } 
} 

然而,當我按下一個按鈕RVC的退出了回myVC

這裏是日誌條目只顯示了代碼我使用MVC去RVC

-(IBAction)goToRVC{ 

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    RootViewController *RVC = [storyboard instantiateViewControllerWithIdentifier:@"Root"]; 
    [UIApplication sharedApplication].delegate.window.RootViewController = RVC; 

    } 

然後從RVC到myVC

-(IBAction)backtomyVC{ 


    [[[UIApplication sharedApplication] delegate] performSelector:@selector(myVC)]; 
    [self disconnect]; 

} 

爲什麼我的通知操作被延遲?

+1

我的問題是你爲什麼要經過這麼多的努力才能回到你的rootViewController?如果您正確使用viewController堆棧,那麼您的代碼通過從您的第一個viewController向您的webView的viewController添加push或模式segue來完成所有這些。 –

回答

0

這不是因爲延誤。 當您按下按鈕A或B創建新RVC對象,但是您發佈通知不存在/ RVC推視圖控制器(你只是初始化它),因此RVC還沒有解僱viewDidLoad方法,它具有不註冊爲觀察員。

之後,你打電話,你創建RVC對象goToRVC方法,你把它添加到視圖層次結構,以便viewDidLoad方法是調用和對象自身註冊爲一個觀察者。

當你回到MVC中的RVC所以它接收通知,你可以看到日誌尚未釋放。

希望它很清楚。