Swift-无法将值成功传递给接下来显示的ViewController

时间:2019-03-25 20:02:47

标签: ios swift

我有一个ItemsController,并且我想在使用self.navigationController?.pushViewController(destination, animated: true)来呈现RepostController时将一个值传递给RepostController。我想将RepostController的itemId设置为cell.item!.getId()。但是在RepostController中,我总是从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()

}

我需要将值成功传递到RepostController。 我需要进行哪些更改?谢谢!

1 个答案:

答案 0 :(得分:0)

我要做的是在IB中创建PUSH segue。然后在prepare(forSegue :)方法中:

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