سؤال

I'm currently trying to get NSNotification to work but I'm having some trouble.

I have two (2) ViewControllers: A. MainViewController & B. LoginViewController.

In my MainViewController I have a logout button that will send a url to my LoginViewController to load it (without showing my loginView). However, it's not working.

In my MainViewController this is what I have:

- (IBAction)logout:(id)sender {

NSURL *logoutURL = [NSURL URLWithString:@"https://myurl.com/logout"];

[[NSNotificationCenter defaultCenter] postNotificationName:@"logoutInitiated" object:logoutURL];


}

This is what I have in my LoginViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    WebView.delegate = self;
    WebView.scalesPageToFit = YES;
    WebView.multipleTouchEnabled = YES;
    loadCount = 0;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(submitLogout) name:@"logoutInitiated" object:nil];

}

- (IBAction)submitLogout:(NSNotification*)notification {


   [WebView stopLoading];

    NSURL * signOutUrl = (NSURL*)[notification object];
    [self loadURL:nil withURL:signOutUrl];
}

My problem is that when I press the logoutButton nothing happens. (Using NSLogs, I see that it never triggers the next step) Thank you!!!!

هل كانت مفيدة؟

المحلول

This is because your method name you are passing in selector is wrong. You need to add colon : at submitLogout: suffix Use

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(submitLogout:) name:@"logoutInitiated" object:nil]; 

in place of

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(submitLogout) name:@"logoutInitiated" object:nil];

Hope it helps you.

نصائح أخرى

When you add self as an observer, you use the selector "submitLogout" without a semicolon! But your method has an argument, so the correct selector would be @selector(submitLogout:).

Note the SEMICOLON

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(**submitLogout:**) name:@"logoutInitiated" object:nil];    

- (IBAction)logout:(id)sender 
{
        NSURL *logoutURL = [NSURL URLWithString:@"https://myurl.com/logout"];    
        [[NSNotificationCenter defaultCenter] postNotificationName:@"logoutInitiated" object:nil       userInfo:[NSDictionary dictionaryWithObjectsAndKeys:logoutURL,@"RECEIVED_URL", nil]];
}

- (IBAction)submitLogout:(NSNotification*)notification 
{       
    [WebView stopLoading];
     NSURL * signOutUrl = (NSURL*)[notification objectForKey:@"RECEIVED_URL"];
    [self loadURL:nil withURL:signOutUrl];
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top