use*_*282 1 objective-c nsnotification ios
我想在我的AppDelegate.m中NSNotification从这个方法发送一个(当UIButton点击时):
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
//cancel clicked ...do your action
// HERE
}
}
Run Code Online (Sandbox Code Playgroud)
..并在我的一个接收它UIViewControllers.我怎样才能做到这一点?
编辑更多信息:我正在制作一个警报应用程序,当用户按下时UIButton,我想停止警报.我想这NSNotifications是从AppDelegate.m文件获取信息到ViewController.m文件的唯一方法?
您应该注册接收方对象以接受从通知中心发送的一些消息.
假设您有Obj A控制您的警报,值"stopAlarm"是可以停止警报的消息.您应该为"stopAlarm"消息创建一个观察者.
你可以这样做:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(controller:)
name:@"stopAlarm"
object:nil];
Run Code Online (Sandbox Code Playgroud)
现在,您应该创建一个管理此消息的方法控制器:
- (void)controller:(NSNotification *) notification {
if ([[notification name] isEqualToString:@"stopAlarm"]){
//Perform stop alarm with A object
}
}
Run Code Online (Sandbox Code Playgroud)
最后,您可以在代码中发送消息"stopAlarm":
[[NSNotificationCenter defaultCenter]
postNotificationName:@"stopAlarm"
object:nil];
Run Code Online (Sandbox Code Playgroud)
我希望这可能有所帮助.
编辑:
当您的UIViewController被卸载或应用程序终止时,您应该调用:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Run Code Online (Sandbox Code Playgroud)
停止观察.就这样.
感谢热门舔修.
| 归档时间: |
|
| 查看次数: |
2097 次 |
| 最近记录: |