因为某种原因,我似乎被困在NSNotification上了。
我在IBAction按钮方法中发布通知。当用户点击该按钮时,我希望得到通知,这样我就可以在文本字段中设置文本。如果没有他们按下按钮,NSString仍然是零--这就是为什么我需要知道他们什么时候这么做。
因此,在按钮方法中,我有以下内容:
- (IBAction)suggestionsButton:(UIButton *)sender {
self.usernameSelected = sender.titleLabel.text;
[[NSNotificationCenter defaultCenter] postNotificationName:@"UserTappedButton" object:self];
}这是在一个UITableviewCell类中。
然后,在与此操作有关的视图控制器中添加观察者:
(void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(userPickedAuserNameFromSuggestion:) name:@"UserTappedButton" object:nil];
}我检查过的东西:
看了几个这样的答案,也没有帮上忙。
伙计们,这里有我遗漏的东西吗?
*更新*
对不起-这是我想要调用的方法:
-(void)userPickedAuserNameFromSuggestion: (NSNotification *)notification
{
NSLog (@"Selected Username: %@", self.usernameCell.usernameSelected);
}但是它没有被调用
发布于 2014-02-07 06:25:15
将-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];
//...
}发布于 2016-08-10 03:30:59
我认为您的通知观察者没有正确地发布,您需要这样做:
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UserTappedButton" object:nil];在dealloc函数中。
https://stackoverflow.com/questions/21619962
复制相似问题