2016-05-16 43 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)