ios - 设置一个属性来更新 VC swift 上显示的信息

标签 ios swift parse-platform properties swift2

我有 2 个 View Controller ,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

查看 Controller 1 转场:

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
}
}

最佳答案

当你刚接触 swift 时,这是一个常见的错误。
- 当您转到VC2,并将值传递给“userToShowDetail”时。“userToShowDetail”的“didSet”方法将被调用。
- 不幸的是,此时,你的 VC2 的 View 仍然是 nil。所以你会得到这个错误。
- 解决办法很简单,加一个“?”在“didSet”函数中的每个 View 之后。 并且不要忘记在 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
}

关于ios - 设置一个属性来更新 VC swift 上显示的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33572660/

相关文章:

ios - 在 Xcode 7.2 beta 之后,项目无法在 7.1 上运行

ios - Swift 对象初始化(类工厂方法、默认初始化、便捷初始化)

ios - UITableView 不显示内容,除非滚动或触摸单元格

ios - 如何重新发送电子邮件验证 parse.com

ios - iOS6 中带有 UISegmentedControl : good in iOS7, 的导航 Controller 中的自动布局错误

ios - 如何在循环内对 UIView 进行动画 n 次并跟踪每个动画事件

ios - 在进行演示时在场?在使用 parse 登录 facebook 后尝试显示新 View 。

ios - 如何将手指拖过 2 个 View 并始终知道它在哪个 View 上

swift - map 中的 SF 符号不会应用颜色

javascript - 是通过云代码从 REST 删除/放置对象的唯一方法吗? (Cors问题)