2013-02-25 52 views
2

我试图让我的视图使用重大位置更新刷新。位置管理器位于我的应用程序委托中,并且我尝试使用NSNotificationCenter来告诉视图控制器在用户更改位置时进行刷新。我不知道如何刷新以刷新视图。我现在有如下设置。我做了一个“视图控制器(刷新视图)”块,我认为刷新代码会发生。任何人都知道如何使这项工作?谢谢!重大位置更改的重新加载/刷新视图控制器(从应用程序委托)

应用代理(监听显著位置的变化):

- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation 
*)newLocation fromLocation:(CLLocation *)oldLocation { 

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

} 

视图控制器(听NSNotificationCenter):

- (void)viewDidLoad { 

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

} 

视图控制器(刷新视图):

-(void)refreshView:(NSNotification *) notification { 

//CODE TO REFRESH THE VIEW HERE 

} 
+0

UIViewController * root = _window.rootViewController; UINavigationController * navController =(UINavigationController *)root; YourViewController * mycontroller =(YourViewController *)[[navController viewControllers] objectAtIndex:0]; [mycontroller reloadInputViews]; – iPatel 2013-02-25 09:14:06

回答

4
-(void)refreshView:(NSNotification *) notification { 

[self viewDidLoad]; 
[self viewWillAppear]; // If viewWillAppear also contains code 

} 
+0

感谢Sudesh。所以我可以从字面上再次调用[self viewDidLoad]? – Brandon 2013-02-25 09:16:04

+1

是的,你可以:)但小心如果任何变量是在viewDidLoad内创建的,那么它将创建另一个实例并增加内存。 – 2013-02-25 09:18:45

+2

因此,在创建任何变量释放旧版本之前,如果你不使用ARC。 – 2013-02-25 09:19:59

相关问题