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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...