2016-05-16 42 views
1

所以我有一個從ViewController segue到SecondViewController。這個segue由ViewController中的UIButton觸發,模態地呈現SecondViewController。 Segue的標識符已設置(「SegueIdentifier」),我可以從我的ViewController中以編程方式調用segue。不能調用標識符從AppDelegate

當我嘗試在我的AppDelegate中做同樣的事情時,我得到編譯器無法找到具有我設置的標識符的segue的錯誤。

let viewController = ViewController()    
viewController.performSegueWithIdentifier("SegueIdentifier", sender: nil) 

同樣,我字面上複製和粘貼從上述方法中的ViewController的performSegueWithIdentifier方法調用,其中我還呼籲performSegueWithIdentifier對於相同SEGUE和它的工作原理。

任何想法?

+0

是您要的這個初始視圖推? –

+0

@GurPreet_Singh不,我想推的視圖不是初始視圖。 – Jonas

+0

由於您使用簡單的初始化程序創建ViewController實例,因此它不與故事板相關聯。您應該從您的碎片的主窗口訪問根視圖控制器UIApplication實例 – Paulw11

回答

1

在我的一個項目,我正在努力應付這種情況類似,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
// Override point for customization after application launch. 

NSString *identifier; 
BOOL isSaved = [[NSUserDefaults standardUserDefaults] boolForKey:@"loginSaved"]; 
if (isSaved) 
{ 
    [email protected]"home1"; 



} 
else 
{ 
    [email protected]"dis1"; 
} 
UIStoryboard * storyboardobj=[UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
UIViewController *screen = [storyboardobj instantiateViewControllerWithIdentifier:identifier]; 



[self.window setRootViewController:screen]; 


return YES; 
} 

它在客觀c和僅供參考。如果它可以幫助你:)

更新:

在迅速類似,

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 



    var identifier: String = String() 

    let isSaved = NSUserDefaults.standardUserDefaults().boolForKey("loginsaved") 


    if isSaved 

    { 
     identifier = "home1" 
    } 
    else{ 

     identifier = "dis1" 
    } 

    let storyboardobj: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
    let screen: UIViewController = storyboardobj.instantiateViewControllerWithIdentifier(identifier) 
    self.window!.rootViewController = screen 

    // Override point for customization after application launch. 
    return true 
} 

希望這將有助於:)

1

如果你是不是想顯示你的初步意見,然後這樣做:

UIApplication.sharedApplication().keyWindow?.rootViewController.performSegueWithIdentifier("SegueIdentifier", sender: nil) 

而且還這取決於你Segue公司設爲您的RootViewController的。在你這樣做之前,請檢查一下。

+0

如果我沒有rootViewController,這仍然可以工作嗎? – Jonas

+0

每個應用程序都必須有一個根視圖控制器。沒有根視圖,你的應用程序將無法工作。 –

+0

好的謝謝!我會在稍後嘗試。 – Jonas

1

塞格連接有一些信息,如源控制器,目標控制器。所以當你從ViewController類調用performSegue方法時,它會工作,因爲這個類有這個Segue連接的信息。 從App Delegate調用相同的Segue方法時,它不起作用。因爲App Delegate類沒有關於Segue對象的信息或定義。

您還應該檢查調用對象是否在導航控制器下。

0

故事板segue必須綁定到您試圖從中延伸的特定viewController。所以你必須在屏幕上有一個當前視圖控制器的實例。您可以使用UIApplication.sharedApplication().keyWindow?.rootViewController

方法可以訪問該窗口中的RootViewController的,但是如果你提出的RootViewController的上述視圖控制器,你想,你必須訪問rootViewController.presentedViewController所呈現的視圖控制器上執行SEGUE。因爲你可能有多個presentedViewControllers,你做的是這樣的:

//get the root view controller 
var currentViewController = UIApplication.sharedApplication().keyWindow?.rootViewController 
//loop over the presented view controllers and until you get the top view controller 
while currentViewController.presentedViewController != nil{ 
    currentViewController = currentViewController.presentedViewController 
} 
//And finally 
currentViewController.performSegueWithIdentifier("identifier", sender: nil) 
相關問題