0

I have the following snippet of code in viewDidLoad which I post a notification to be received by a different ViewController, and prior to iOS 7 (and XCode 5) this has worked:

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
    }

And in the segued ViewController, I have the following:

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

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

Currently, testSetupFunction does not executed, which means the notification is not received. I am unsure whether its because I have segued to a different view and then posted the notification, or that this is something new with iOS 7, but currently the notification is no longer received.

Thanks for your help!

4

2 回答 2

3

一旦视图控制器有时间加载,就发布通知。

- (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];
}

您还可以从您的 segued View Controller 发布通知,说明它已加载,并响应该通知,发布您的testSetup通知。

于 2013-10-17T00:00:38.433 回答
0

您是否尝试在 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以检查当时是否确实加载了视图,有时加载视图需要时间,仅在需要时才加载视图。

于 2013-10-17T01:29:31.293 回答