簡體   English   中英

如何檢測您的ViewController是否從AppDelegate推送?

[英]How to detect if your ViewController was pushed from AppDelegate?

在我的AppDelegate中,如果用戶鍵入我的自定義URL方案,則會顯示一個ViewController。 它在我的ViewController出現時起作用。 但是,我需要檢測ViewController是否從App委托中推送。 這樣做的最佳方法或最佳方法是什么? 我只想檢測它是否來自AppDelegate,沒有其他地方。

           - (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url
   sourceApplication:(NSString *)sourceApplication
     annotation:(id)annotation {
// attempt to extract a token from the url

  ViewController *controller = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
 [self.navigationController pushViewController:controller animated:YES];

編輯:為了澄清我正在嘗試做的更好的事情,當appdelegate呈現我的viewcontroller時,我需要能夠檢測到我的viewcontroller內部由於應用程序委托中的方法而出現的情況。 像這樣

ViewController

-(void) ViewDidLoad{
          if (this controller was presented from App delegate){
          do this
        }
         else{
          do nothing
          }

像這樣簡單地做:

 - (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
       sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {

     ViewController *controller = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
     [self.navigationController pushViewController:controller animated:YES];

// here you know that your controller is pushed from the AppDelegate, then you can do other things just after the push

    [controller callControllerPublicMethod];
    }

當您將其推入另一個控制器時,什么也不做。

為什么不直接在ViewController編寫您自己的自定義初始化方法,並在從應用程序委托調用時將參數設置為true,而在其他任何地方都將其設置為false。

暫無
暫無

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

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