简体   繁体   English

如何在iOS中打开现有的ViewController

[英]How to open an existing viewcontroller in ios

I need to open existing viewcontroller from AppDelegate while receiving push notification. 我需要在收到推送通知时从AppDelegate打开现有的viewcontroller。 Currently i am opening new one every time so issue is that it is called viewDidLoad every time and all variable are reinitialized again and again. 目前,我每次都打开一个新文件,所以问题是每次都称为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];



}

Try to do so: 尝试这样做:

- (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;
    }

Then you can reuse your view controller (However, it will exposure the view controller in your AppDelegate, which may be a taste in clean code). 然后,您可以重用您的视图控制器(但是,它将在您的AppDelegate中公开视图控制器,这可能是纯代码的一种体验)。

Since you want to use a existing view controller, why do you use the code HomeViewController* room = [[HomeViewController alloc] init]; 由于要使用现有的视图控制器,因此为什么要使用代码HomeViewController* room = [[HomeViewController alloc] init]; ?

Follow your goal, my suggestion is use a property to retain the existing view controller, just like: 按照您的目标,我的建议是使用一个属性来保留现有的视图控制器,就像:

@property (strong, nonatomic) UIViewController *existingViewController;

and you 你呢

[self.window.rootViewController presentViewController:existingViewController
                                                     animated:NO
                                                   completion:nil];

You can get UINavigationController in AppDelegate meethod 您可以在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM