Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

swift - Cannot pass data through NavigationController

I'm trying to pass data from a UITableView when clicking on the cell. This is what I've written from a similar flow that worked, thinking it would work as well but it doesn't pass any data.

Here is the code:

extension PastBoxesViewController: UITableViewDelegate {
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let nextVC = GiftViewController()
        nextVC.title = "Gift Number (indexPath.row)"
        self.navigationController?.pushViewController(nextVC, animated: true)
    }
}
class GiftViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = ""
        self.view.backgroundColor = #colorLiteral(red: 0.8186396956, green: 0.7955000997, blue: 1, alpha: 1)
    }
}

This method of passing data works outside a UITable, does it not work inside or am I missing something?

Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is nothing wrong except that in GiftViewController your are setting the title property to an empty string after your correctly assigned the value you want in didSelectRowAt. So you set the the property before the view is loaded and then when the view finish loading you change that value again to an empty string.

Just change your code in viewDidLoad removing that line of code and everything will be working fine:

override func viewDidLoad() {
   super.viewDidLoad()
   //self.title = "" your problem is here
   self.view.backgroundColor = #colorLiteral(red: 0.8186396956, green:  0.7955000997, blue: 1, alpha: 1)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...