簡體   English   中英

NSNotificationCenter不起作用

[英]NSNotificationCenter doesn't work

我做這樣的通知:mainView中有代碼:

- (void)viewDidLoad
{

CGRect view1Hrame;
view1Hrame.origin.x = 160;
view1Hrame.origin.y = 215;
view1Hrame.size = self.currenWeatherView.frame.size;

weatherViewController *view1 = [[weatherViewController alloc] initWithForecast:[[Forecast alloc] initWithForecastInCurrentLocation]];    

[[NSNotificationCenter defaultCenter] addObserver:view1 selector:@selector(forecastChanged) name:@"forecastChanged" object:nil];

view1.view.backgroundColor = [UIColor colorWithRed:0 green:255 blue:0 alpha:0.06];

[currenWeatherView addSubview: view1.view];
}

在預測類中:

 [[NSNotificationCenter defaultCenter] postNotificationName:@"forecastChanged" object:nil];

在weatherViewController中

 - (void)viewDidLoad
  {
   [super viewDidLoad];

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

    // Do any additional setup after loading the view from its nib.
   }

這是行不通的。 我做錯了什么?

如果要在weatherViewController調用通知,則需要定義此類的viewDidLoad方法。

確保您正在調用此viewController的viewDidLoad。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM