【问题标题】:Set a property that updates information on display on a VC swift设置更新在 VC swift 上显示的信息的属性
【发布时间】:2015-11-06 17:47:28
【问题描述】:

我有 2 个视图控制器,VC1 在我的应用程序中显示所有用户的基本信息,当您在某个单元格中按下更多信息按钮时,它会转到一个新的 VC,该 VC 显示您所在单元格中的用户完整配置文件之前按下。

在帮助下,除了如何在 VC2 上显示结果外,我一切正常。我正在尝试在 VC2 中设置一个属性。我也试过了

此代码未返回任何错误,但在 VC2 上不显示任何内容。

我试过set,get willSet,什么都破解不了:

var userToShowDetail: PFUser? {
    willSet {
        self.configureView()
    }
}

func configureView() {
    // Update the user interface for the detail item.
    if let userToShowDetail: PFUser = self.userToShowDetail {
        if let label = self.userName {
            userName.text = userToShowDetail["name"] as! String?

        }
    }
}

override func viewDidLoad() {

我试过这个,但它只返回一个空的 VC:

var userToShowDetail: PFUser?

override func viewDidLoad() {
    super.viewDidLoad()

    if let appUsers = userToShowDetail as PFUser? {

    self.userName.text = appUsers["name"] as? String

    self.userInstrument.text = appUsers["instrument"] as? String

    self.userAge.text = appUsers["age"] as? String

    self.userProfile.file = appUsers["image"] as? PFFile

    self.userProfile.layer.cornerRadius = self.userProfile.frame.size.width/2

    self.userProfile.clipsToBounds = true

    self.userProfile.loadInBackground()
    }

我研究过 didSet 和 willSet 但我仍然不知所措。任何帮助将不胜感激。有人可以指点我的教程或建议我下一步是什么。

willSet and DidSet purpose Apple properties

View Controller 1 segue:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "userProfileDetailsSegue" {
//get the index path for the row that was selected
let indexPath = self.resultsPageTableView.indexPathForSelectedRow!
//get the PFUser object for that particular row
let userToShow = self.appUsers[indexPath.row]
//create your new view controller
let newVc = segue.destinationViewController as! UserProfileDetailsViewController
//assign the new vc's property to your object
newVc.userToShowDetail = userToShow
}
}

【问题讨论】:

  • 试试exception breakpoint,它可能会帮助您找出问题所在
  • 从 vc1 推送到 vc2 时,能否显示设置 userToShowDetail 的代码?
  • @AndreSlotta 抱歉,我在那里更新了我的代码。

标签: ios swift parse-platform properties swift2


【解决方案1】:

当您刚接触 swift 时,这是一个常见的错误。
- 当你segue到VC2,并传递一个值给“userToShowDetail”。“userToShowDetail”的“didSet”方法将被调用。
- 不幸的是,此时你的 VC2 的视图仍然为零。所以你会得到这个错误。
- 解决方法很简单,加一个“?”在“didSet”函数中的每个视图之后。 并且不要忘记在 VC2 的 viewDidload 中调用“didSet”函数。

func configureView() {
if let userToShowDetail: PFUser = self.userToShowDetail {
    if let label == self.userName? {//add "?" to VC2's subviews 
        userName?.text = userToShowDetail["name"] as! String?//add "?" to VC2's subviews 
    }
  }
}

override func viewDidLoad() {
  super.viewDidLoad()
  configureView()//call mothed in didSet()
  // other thing you need to do
}

【讨论】:

  • 我不明白你的意思。我的代码在我的 didSet 方法中是否正确?我在哪里添加“?”在我上面的 didSet 代码中?对不起,didSet 等的新手。
  • @Grace 我添加了一些 sn-p。
  • 现在“if let label = self.userName?”出现错误错误在 ?插入。错误是:“?”必须后跟调用、成员查找或下标
  • 把这行代码改成“if let label == self.userName?”
  • 在“if let label == self.userName?”之后的 { 上出现新错误“条件中的变量绑定需要初始化器”
猜你喜欢
  • 2015-11-15
  • 2016-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多