简体   繁体   English

无法通过Segue传递数据

[英]Can't pass data via segue

I make app with news feed which has to open on other ViewController. 我用必须在其他ViewController上打开的新闻提要制作应用。 But can't pass data via segue. 但是不能通过segue传递数据。

Viewcontroller with newsfeed 带新闻提要的ViewController

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var titlenews = ""

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "newsfeedCell", for: indexPath) as! NewsFeedCell
    cell.newsfeed_title.text = self.news?[indexPath.item].headline
    cell.newsfeed_topic.text = self.news?[indexPath.item].topic
    cell.newsfeed_time.text = timetime(from: (self.news?[indexPath.item].time)!)
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("tableview")
    let vc = storyboard?.instantiateViewController(withIdentifier: "newsBody") as? NewsBody
    vc?.labeltext = (self.news?[indexPath.item].headline)!
    print((self.news?[indexPath.item].headline)!)
    self.navigationController?.pushViewController(vc!, animated: true)
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.news!.count
} //number of rows

@IBOutlet weak var tableview: UITableView!
var news: [Newsfeed]? = []

override func viewDidLoad() {
    super.viewDidLoad()
    getJSON()
}

func getJSON(){
 ///Here all do right 
}
}

Viewcontroller which has to receive data from news feed ViewController必须从新闻提要中接收数据

  class NewsBody: UIViewController {

    @IBOutlet weak var testLabel: UILabel!

    var labeltext = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        print(labeltext)
        testLabel.text = labeltext
    }
}

print(labeltext) shows that NewsBody receive empty value or nothing. print(labeltext) NewsBody print(labeltext)显示NewsBody接收到空值或不接收任何值。

But print((self.news?[indexPath.item].headline)!) inside of SecondViewController shows that I try to push proper value. 但是print((self.news?[indexPath.item].headline)!)的内部SecondViewController表明,我试图把应有的价值。

What I do incorrect between this actions? 我在两次操作之间做错了什么? What wrong with segue and pass of data? segue和数据传递有什么问题?

It seems that instantiateViewController(withIdentifier: "newsBody") triggers view load under the hood. 似乎instantiateViewController(withIdentifier: "newsBody")instantiateViewController(withIdentifier: "newsBody")触发了视图加载。 It should not (in theory) but it might do just that in your case. 从理论上讲,它不应该这样做,但是在您的情况下,它可以做到这一点。

This means that viewDidLoad() will be called before the vc?.labeltext = (self.news?[indexPath.item].headline)! 这意味着viewDidLoad()将在vc?.labeltext = (self.news?[indexPath.item].headline)!之前调用vc?.labeltext = (self.news?[indexPath.item].headline)! is executed. 被执行。

I'd recommend you to do the following. 我建议您执行以下操作。

class NewsBody: UIViewController {

  @IBOutlet weak var testLabel: UILabel!

  var labeltext: String? {
    didSet { updateUI() }
  }

  override func viewDidLoad() {
    super.viewDidLoad()
    updateUI()
  }

  private func updateUI() {
    testLabel.text = labeltext
  }
}

This way if you set the labeltext property after the view is loaded, it will still trigger the UI update. 这样,如果您在加载视图后设置了labeltext属性,它将仍然触发UI更新。 And if you set the labeltext property before the view is loaded, as soon as viewDidLoad() is called. 并且,如果您在加载视图之前设置了labeltext属性,则将在调用viewDidLoad()进行。

BTW, you are not using segues here. 顺便说一句,您不在这里使用segues。 But even if you do, you can easily use the same method as I proposed, because it allows you to stop thinking about whether property updates will update the UI. 但是即使您这样做,也可以轻松使用与我建议的方法相同的方法,因为它使您不必再考虑属性更新是否会更新UI。


Also please note that I made the property optional. 另外请注意,我将该属性设置为可选。 It will allow you to avoid force casts and just do 它可以让您避免强行施放

vc?.labeltext = self.news?[indexPath.item].headline

UILabel.text is also an optional String property, so they will play well together. UILabel.text也是一个可选的String属性,因此它们可以很好地配合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM