NSNotification Errors

I seem to be stuck in NSNotification for some reason.

I am sending a notification in the IBAction button method. When the user deletes this button, I want to receive a notification about this so that I can set the text in the text box. Without a button click, the NSString button will still be zero, so I need to know when they do this.

So, in the button method, I have this:

- (IBAction)suggestionsButton:(UIButton *)sender {

    self.usernameSelected = sender.titleLabel.text;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"UserTappedButton" object:self];
}

This is the UITableviewCell class.

Then I add an observer to the view controller that is associated with this action:

 (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(userPickedAuserNameFromSuggestion:) name:@"UserTappedButton" object:nil];
}

Things I checked:

  • So that the observer method is called first before the post method (this)
  • So that the name is correct for both methods
  • The correct signature of the selector

Looked at a few SO answers and also didn't help.

Is there something I'm missing here guys?

* UPDATE *

- , :

-(void)userPickedAuserNameFromSuggestion: (NSNotification *)notification
{
    NSLog (@"Selected Username: %@", self.usernameCell.usernameSelected);

}

+3
2

-addObserver: viewDidAppear -removeObserver: viewDidDisappear

- (void)viewDidAppear:(BOOL)animated
{
    //...
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(userPickedAuserNameFromSuggestion:)
                                                 name:@"UserTappedButton"
                                               object:nil];
    //...
}

- (void)viewDidDisappear:(BOOL)animated
{
    //...
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:@"UserTappedButton"
                                                  object:nil];
    //...
}
+2

, Notification , :

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UserTappedButton" object:nil];

dealloc.

0

All Articles