从Appdelegate通过UIButton导航到另一个视图

时间:2015-03-25 08:40:21

标签: ios iphone swift uibutton uistoryboardsegue

我有以下问题。我在Appdelegate中创建了一个UIButton,我希望该按钮可以导航到另一个ViewController。我已经从我的故事板上建立了连接,这种连接是一种类型的Modal Segue。我认为performSegueWithIdentifier会完成这项工作,但它会给我以下错误"没有带标识符的segue' showCamera' &#34 ;.而且我非常确定我已通过故事板制作了该标识符。

    let camera = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
     camera.addTarget(self, action: "takePicture:", forControlEvents: .TouchUpInside);

    //this function works 
    func takePicture(sender: UIButton!){
        println("Open Camera")

    //this throws an error        

self.window?.rootViewController?.performSegueWithIdentifier("showCamera", sender: self) 
        }

1 个答案:

答案 0 :(得分:0)

不要在appDelegate中添加任何视图组件。只需将UIViewController设置为窗口的rootViewController即可。然后添加按钮到UIViewController.Like,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

{
     self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];

    ViewController *vc = [[ViewController alloc]init];
   UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:vc];
    self.window.rootViewController = navController;

    // Override point for customization after application launch.
    return YES;
}

在ViewController.m中添加所需的UI组件。