[英]How to open an existing viewcontroller in ios
我需要在收到推送通知时从AppDelegate打开现有的viewcontroller。 目前,我每次都打开一个新文件,所以问题是每次都称为viewDidLoad,并且一次又一次地重新初始化所有变量。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSUserDefaults standardUserDefaults] setObject:@"Yes" forKey:@"Got Message"];
[[NSUserDefaults standardUserDefaults] setObject:userInfo forKey:@"message"];
[[NSUserDefaults standardUserDefaults]synchronize];
HomeViewController* room = [[HomeViewController alloc] init];
[self.window.rootViewController presentViewController:room
animated:NO
completion:nil];
}
尝试这样做:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSUserDefaults standardUserDefaults] setObject:@"Yes" forKey:@"Got Message"];
[[NSUserDefaults standardUserDefaults] setObject:userInfo forKey:@"message"];
[[NSUserDefaults standardUserDefaults]synchronize];
[self.window.rootViewController presentViewController:self.room
animated:NO
completion:nil];
}
- (UIViewController *)room {
if (_room == NULL) {
_room = [[HomeViewController alloc] init];
}
return _room;
}
然后,您可以重用您的视图控制器(但是,它将在您的AppDelegate中公开视图控制器,这可能是纯代码的一种体验)。
由于要使用现有的视图控制器,因此为什么要使用代码HomeViewController* room = [[HomeViewController alloc] init];
?
按照您的目标,我的建议是使用一个属性来保留现有的视图控制器,就像:
@property (strong, nonatomic) UIViewController *existingViewController;
你呢
[self.window.rootViewController presentViewController:existingViewController
animated:NO
completion:nil];
您可以在AppDelegate method中获取UINavigationController
UIViewController *yourViewController = //your view controller to show after push
UINavigationController *navController = self.window.rootViewController;
[navController popToViewController:yourViewController animated:YES]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.