从UINavigationView到UIView的转发模型

时间:2014-10-29 07:58:58

标签: ios xcode cocoa-touch swift

当用户从UITableCell a 中选择UITableViewController时,显示segue 会向UINavigationController执行

import UIKit

class ItemNavigationController: UINavigationController {

    // this is our "model" - its value is set by the "prepareForSegue" method of the parent UITableViewController
    var item: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        // here I want to "forward" the "model" to the ItemViewController
        // self.someMethodToGetItemViewController.item = item
    }

}

ItemViewController上设置模型后,我们“刷新”标签:

import UIKit

class ItemViewController: UIViewController {

    var item: String!

    @IBOutlet
    weak var nameLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // does not do anything because item is empty...
        nameLabel.text = item
    }

}

如何设置item ItemViewController的{​​{1}}属性?

1 个答案:

答案 0 :(得分:1)

如果ItemViewController是ItemNavigationController的topViewController,那么你可以通过调用topViewController方法直接获取它,否则,你应该循环遍历ItemNavigationController的viewControllers属性并找到它。