具有有效引用的相同视图,当从委托

时间:2015-12-14 07:02:39

标签: swift2.1 xcode7.2

当从粉色区域中的灰色按钮调用时,func yesBtSetTextTo打印不是nil,但是当从黄色按钮调用时,相同的func打印nil。我迷失在这一个。

我的主要veiwController.swift没有prepareForSegue因为我没有在这里需要的东西。

enter image description here

有问题的func位于此代码块的末尾,

MainBtVC.swift

protocol MainBtDelegate {
func yesBtSetTextTo(name: String)
}
class MainBtVC: UIViewController, MainBtDelegate {

@IBOutlet weak var yesBt: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func yesBtSetTextTo(name: String) {
    print("called")
    if yesBt == nil {
        print("button is nil")
    } else {
        print("button not nil")
    }
    //      yesBt.setTitle("Exit", forState: UIControlState.Normal)
}

@IBAction func yesBtTapped(sender: AnyObject) {
    yesBtSetTextTo("dummy")
}

TopVC.swift

class TopVC: UIViewController {
var delegate: MainBtDelegate?

override func viewDidLoad() {
    super.viewDidLoad()
    delegate = MainBtVC()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

@IBAction func buttonTapped(sender: AnyObject) {
    delegate?.yesBtSetTextTo("Exit")
}

0 个答案:

没有答案