微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 使用代码执行Segue会显示黑屏

我正在尝试使用代码对另一个屏幕进行segue,但它使用 Xcode 7 beta 6向我显示黑屏.
这是我的第一个视图控制器文件代码

//  ViewController.swift
//  Segue through programming
//

import UIKit

class ViewController: UIViewController{

    @IBAction func buttonpressed(sender: AnyObject) {
        presentViewController(secondController(),animated: true) { () -> Void in

        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // dispose of any resources that can be recreated.
    }

}

解决方法

这仅在secondController以编程方式创建其视图时才有效.如果要使用故事板场景(更常见),可以执行以下操作:

@IBAction func buttonpressed(sender: AnyObject) {
    let controller = storyboard?.instantiateViewControllerWithIdentifier("foo")
    presentViewController(controller!,animated: true,completion: nil)
}

这显然假设您已为目标场景指定了故事板标识符.

enter image description here

或者,您可以通过控制从第一个场景顶部的视图控制器图标拖动到第二个场景,在IB中的两个场景之间创建一个segue:

enter image description here

然后给那个segue自己的故事板id:

enter image description here

然后,您可以以编程方式调用segue:

@IBAction func buttonpressed(sender: AnyObject) {
    performSegueWithIdentifier("bar",sender: self)
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐