iphone - NSNotification 未在 View Controller 中接收来自 appDelegate 的信息

标签 iphone ios objective-c nsnotificationcenter

你好,我在 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/

相关文章:

ios - iOS:除非将区域设置为印度,否则印地语语言本地化将不起作用

ios - CTRunDelegateRef iPhone 的内存管理

iphone - 在 Objective C 中复制 Java 加密

javascript - 如何使用 JavaScript 更改 UIWebView 字体?

ios - 将 URL 转换为 NSDATA 并解析 JSON 的方法正确与否?

ios - 如何仅转义未转义的引号 Objective C

javascript - 动态插入大量 DOM 元素时,Mobile Safari (iPhone/iPad) 崩溃

iphone - 由于不受信任的证书,无法通过 HTTPS 接收 JSON 请求

iphone - 可以在 Switch 语句中进行类型声明吗?

ios - 使用 FCM 的 ios 中不显示远程通知