Swift - 无法将值成功传递到接下来显示的视图控制器



>我有一个 ItemsController,我想在使用 self.navigationController?.pushViewController(destination, animated: true) 来呈现 RepostController 时将一个值传递给 RepostController。我想将重新发布控制器的itemId设置为cell.item!.getId()。但是在重新发布控制器中,我总是从itemId中得到一个值 0 .我也尝试打印值。它们在下面的代码中进行了注释。

我使用的是带有 Xcode 10.1 的 Swift 4。

// ITEMS CONTROLLER
override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor(red:0.30, green:0.30, blue:0.30, alpha:1.0)]
    self.navigationController?.navigationBar.tintColor = UIColor(red:0.30, green:0.30, blue:0.30, alpha:1.0)
    self.navigationController?.navigationBar.shadowImage = UIImage()

    tableView.estimatedRowHeight = 250
    tableView.rowHeight = UITableView.automaticDimension
    DispatchQueue.main.async() {
        self.refresh(sender: self)
        self.tableView.reloadData()
    }
    if #available(iOS 11.0, *) {
        self.navigationController?.navigationBar.prefersLargeTitles = true
    }
    if ((profileId == iApp.sharedInstance.getId() && pageId == Constants.ITEMS_PROFILE_PAGE) || pageId == Constants.ITEMS_FEED_PAGE || pageId == Constants.ITEMS_STREAM_PAGE) {
        let newItemButton = UIBarButtonItem(barButtonSystemItem: .add , target: self, action: #selector(newItem))
        self.navigationItem.rightBarButtonItem  = newItemButton
    }
    // add tableview delegate
    tableView.delegate = self
    tableView.dataSource = self
    self.tableView.tableFooterView = UIView()
    self.refreshControl.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged)
    self.tableView.addSubview(refreshControl)
    self.tableView.alwaysBounceVertical = true
    // prepare for loading data
    self.showLoadingScreen()
    // start loading data
    self.loadData()
}
func showRepostButton(cell: ItemCell) {
    let storyboard = UIStoryboard(name: "Content", bundle: Bundle.main)
    let destination = storyboard.instantiateViewController(withIdentifier: "RepostController") as! RepostController
    destination.itemId = cell.item!.getId()
    print (String(cell.item!.getId())) // Whatever the correct value is (such as 20)
    self.navigationController?.pushViewController(destination, animated: true)
}
// REPOST CONTROLLER
override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor(red:0.30, green:0.30, blue:0.30, alpha:1.0)]
    self.navigationController?.navigationBar.tintColor = UIColor(red:0.30, green:0.30, blue:0.30, alpha:1.0)
    textView.delegate = self
    textView.tintColor = UIColor(red:0.23, green:0.72, blue:0.65, alpha:1.0)
    print ("itemID:" + String(self.itemId))
}

@IBAction func cancelTap(_ sender: Any) {
    self.dismiss(animated: true, completion: nil)
}
@IBAction func repostTap(_ sender: Any) {
    self.message = self.textView.text
    send()
}

我需要将值成功传递给重新发布控制器。我需要进行哪些更改?谢谢!

我要做的是在 IB 中创建一个 PUSH segue。 然后在准备中(forSegue:(方法:

if let dest = segue.destination as? YourDestinationVC {
    dest.itemId = someValue
}

最新更新