NSViewController performSegueWithIdentifier

时间:2016-02-11 12:26:09

标签: swift macos nsviewcontroller

我试图了解performSegueWithIdentifier是如何工作的。我做了一些谷歌,阅读这里的帖子,关注一些YouTube视频,但它根据视频不起作用。我错过了什么吗?

我设置了一个非常简单的测试

1)ViewController.swift和SecondViewController.swift

2)按住Ctrl键从ViewController视图拖动到SecondViewController视图,然后选择Modal

3)在ViewController视图上添加一个Text框和Go按钮,并在ViewController.swift上设置变量

4)添加要显示的标签以显示从ViewController传递的数据

问题:

1)第二个视图控制器永远不会启动??

2)调试时,看起来performSegueWithIdentifier没有被激活,因为我在performSegueWithIdentifier函数中放置了一个打印命令来打印出一些东西,以确保它已被调用但没有打印

我错过了什么?

以下是我的ViewController.swift

import Cocoa

class ViewController: NSViewController {
    @IBOutlet weak var txt_val: NSTextField!

    @IBAction func btnConfirm(sender: AnyObject) {
        self.performSegueWithIdentifier("SecondViewController", sender: txt_val.stringValue)
    }

    override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject?) {
        print("Called \(segue)") // Nothing print out here when btnConfirm was clicked
        if segue.identifier == "SecondViewController"{
            let vc = segue.destinationController as! SecondViewController
            vc.viaSegue = txt_val.stringValue
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }
}

下面是我的SecondViewController.swift代码:

import Cocoa

class SecondViewController: NSViewController {
    @IBOutlet weak var dsp_label: NSTextField!
    var viaSegue:String = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        dsp_label.stringValue = viaSegue
        print("viaSegue: \(viaSegue)")
    }
}

0 个答案:

没有答案