簡體   English   中英

如何根據遠程通知在ios應用程序中更改初始視圖控制器?

[英]How do you change the initial view controller in an ios application based on a remote notification?

我有一個正在實現遠程通知的應用程序。 我的願望是根據以下三個條件之一來更改初始視圖控制器:用戶自行啟動應用程序(無通知); 或者,用戶收到了可疑的活動警報; 或者,用戶收到了犯罪警報。 我已經在appDelegate中實現了應用程序didReceiveRemoteNotification:方法。 基於通知和用戶對通知的響應,我實現了以下代碼:

UITabBarController *tbContr = (UITabBarController*) self.window.rootViewController;
UINavigationController *navContr = [tbContr.viewControllers][2];
ViewCrimesController *viewCrimes = [navContr.storyboard instantiateViewControllerWithIdentifier:@"ViewCrimes"];
[navContr presentViewController:viewCrimes animated:YES completion:nil];

[self.window.makeKeyAndVisible];

我遇到的問題是導航控件-即返回按鈕和導航欄標題; 呈現時不在ViewCrimesController上。 我試圖以多種不同方式加載ViewCrimesController。 無論哪種方式,我都會收到錯誤提示說沒有segue(此視圖是映射視圖的模型視圖),或者我試圖在活動視圖上加載該視圖,或者再次沒有導航控件。

我是否需要對導航控件進行專門編程,還是在嘗試加載視圖時缺少某些東西?

我在其他文章中看到了有關動態更改初始視圖的參考。 但是我還沒有看到任何指示需要添加控件的特定編程的東西。 您能提供的任何幫助將不勝感激!

蘇珊

您是否看過帖子, 使用情節提要以編程方式設置初始視圖控制器

該帖子的答案可能會解決您的問題,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];

   self.window.rootViewController = viewController;
   [self.window makeKeyAndVisible];

   return YES;
}

您還可以下載此樣本,然后根據需要實施它。

只需更改鍵窗口的rootViewController,示例代碼放在此處,希望對您有所幫助:

UIStoryboard *storyboard = self.window.rootViewController.storyboard;
    UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"rootNavigationController"];
    self.window.rootViewController = rootViewController;

暫無
暫無

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

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