0

当从粉红色区域的灰色按钮调用时, funcyesBtSetTextTo不打印 nil,但从黄色按钮调用时,相同的 func 打印 nil。我迷路了。

我的主要 veiwController.swift 没有,prepareForSegue因为我在这里不需要它。

在此处输入图像描述

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

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

0 回答 0