ios - 通过 UIButton 从 Appdelegate 导航到另一个 View

标签 ios iphone swift uibutton uistoryboardsegue

我有以下问题。我在我的 Appdelegate 中制作了一个 UIButton,我希望该按钮将我导航到另一个 ViewController。我已经从我的 Storyboard 建立了连接,这个连接是一种 Modal Segue 类型。我认为 performSegueWithIdentifier 会完成这项工作,但它给了我以下错误“没有带有标识符 'showCamera' 的 segue”。而且我很确定我已经通过 Storyboard 制作了那个标识符。

    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) 
        }

最佳答案

不要在 appDelegate 中添加任何 View 组件。只需将 UIViewController 设置为窗口的 rootViewController 即可。然后将按钮添加到 UIViewController。像这样,

- (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 组件。

关于ios - 通过 UIButton 从 Appdelegate 导航到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29250903/

相关文章:

ios - swift 中 WKInterfaceController openParentApplication 的替代方案

ios - 为什么我的 iOS 配置文件是 "Not Trusted"?

iphone - 将 NSString 转换为 std::string 撇号字符

iphone - 允许滚动内容大小等于帧大小

ios - NSURLConnection的困惑sendAsynchronous:request:queue:completionHandler和sendSynchronousRequest:returningResponse:error:不起作用

ios - 如何使用一键添加不同整数的分数(swift3)

iphone - OpenGL 从另一个 View 返回后绘制黑屏

ios - 如何在 swift 3 中从 NSNotification 获取用户信息

ios - 了解具有 self 属性的变异函数

swift - ASPasswordCredential - 来自哪里?