你好,我在 app delegate.m 的方法中分配了 nsnotifiaction,这个方法每 30 秒调用一次 eprox,我希望它在 viewcontroller 和 execute 方法中得到通知, 这是我的 appdelegate .m 代码
- (void)layoutAnimated:(BOOL)animated{
BOOL yy= self.bannerView.bannerLoaded;
if (yy==1){
self.iAdString=[NSMutableString stringWithString:@"1"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"BannerViewActionWillBegin" object:self];
}
else{
self.iAdString=[NSMutableString stringWithString:@"0"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"BannerViewActionDidFinish" object:self];
}
}
在 viewcontroller.m 中
//viewdidload方法中定义的i
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willBeginBannerViewActionNotification:) name:@"BannerViewActionWillBegin "object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishBannerViewActionNotification:) name:@"BannerViewActionDidFinish" object:nil];
}
它的方法是..
- (void)willBeginBannerViewActionNotification:(NSNotification *)notification{
[self.view addSubview:self.app.bannerView];
NSLog(@"come");
}
- (void)didFinishBannerViewActionNotification:(NSNotification *)notification {
NSLog(@"come");
[self.app.bannerView removeFromSuperview];
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
在 appdelegate 文件中读取方法时,我没有收到过多方法的响应。
请帮帮我。
最佳答案
你有一个拼写错误。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willBeginBannerViewActionNotification:) name:@"BannerViewActionWillBegin "object:nil];
//Your error here-------------------------------------------------------------------------------------------------------------------------------------^
你在那里放了一个空格。
旁注:对于所有通知名称,您应该/可以创建一个单独的文件并将所有通知名称作为常量字符串。
const NSString *kBannerViewActionWillBegin=@"BannerViewActionWillBegin";
这将更容易更改值,并且不会发生此类拼写错误。
关于iphone - NSNotification 未在 View Controller 中接收来自 appDelegate 的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15872586/