且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Swift中将值从一个视图控制器传递到另一个视图控制器

更新时间:2023-11-27 18:41:04

这是一个有两个假设的一般解决方案。首先,UserId不是UILabel。其次,你打算使用在第二行实例化的 view ,而不是使用 secondViewController

Here's a general solution with two assumptions. First, UserId is not a UILabel. Second, you meant to use view which was instantiated in the second line, instead of using secondViewController

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var view: Dashboard = self.storyboard?.instantiateViewControllerWithIdentifier("Dashboard") as Dashboard

    self.navigationController?.pushViewController(view, animated: true)
    view.UserId = employeesId[indexPath.row]
}

这是Dashboard的样子:

Here's what Dashboard looks like:

class Dashboard: UIViewController {
    var UserId: String!
    @IBOutlet var userIDLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        userIDLabel.text = UserId
    }

    ...
}