簡體   English   中英

NSNotificationCenter以前在iOS 6.1中工作-iOS 7問題?

[英]NSNotificationCenter previously working in iOS 6.1 - iOS 7 issue?

我在viewDidLoad有以下代碼段,該代碼段發布了要由其他ViewController接收的通知,並且在iOS 7(和XCode 5)之前,此方法有效:

if ([PFUser currentUser] && [PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]) {
        NSLog(@"Current user exists and is logged in"); //current works, so I know that this if-statement is satisfied
        [self performSegueWithIdentifier:@"GoToRatingsView" sender:self];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"testSetup" object:nil]; //part in question
    }

在隔離的ViewController中,我有以下內容:

(void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testSetupFunction) name:@"testSetup" object:nil];
}

(void)testSetupFunction
{
    NSLog(@"This function executed");
}

當前, testSetupFunction未執行,這意味着未收到通知。 我不確定是否是因為我已經鎖定了其他視圖然后發布了通知,或者這是iOS 7的新功能,但是當前不再收到通知。

謝謝你的幫助!

ViewController有時間加載后,發布通知。

- (void)postNotificaiton
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"testSetup" object:nil];
}

if ([PFUser currentUser] && [PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]) {
    [self performSegueWithIdentifier:@"GoToRatingsView" sender:self];
    [self performSelector:@selector(postNotification) withObject:nil afterDelay:0.0];
}

您還可以從已定購的View Controller中發布一條通知,說該通知已加載,並響應該通知,發布testSetup通知。

您是否嘗試在init方法中添加觀察者

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testSetupFunction) name:@"testSetup" object:nil];                   

        // Custom initialization
    }
    return self;
}

或者,您也可以嘗試在viewDidLoad放置一個日志,以檢查當時是否實際加載了視圖,有時需要花費一些時間來加載視圖,僅在需要時才加載視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM