2015-11-06 34 views
0

我有2个视图控制器,VC1显示我的应用程序基本信息的所有用户,当你按下某个单元格中的更多信息按钮时,显示用户以前按下的单元格中的完整概要文件。设置一个属性,更新信息显示在一个VC swift

在帮助下,除了如何在VC2上显示结果之外,我已经做好了一切工作。我正在尝试在VC2中设置一个属性。我也试过

这段代码没有返回错误,但在VC2上什么也没有显示。

我试过设置,获取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

视图控制器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 
} 
} 
+0

尝试的[异常断点(https://developer.apple.com/library/ios/recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html)它可以帮助你找出问题的所在 – Leonardo

+0

你可以在从vc1推送到vc2的时候显示你设置'userToShowDetail'的代码吗? –

+0

@AndreSlotta对不起,我更新了我的代码。 – Grace

回答

0

这是当你是新来迅速一个常见的错误。
- 当您继续使用VC2并将值传递给“userToShowDetail”时,将调用“userToShowDetail”的“didSet”方法。
- 不幸的是,在这一点上,你的VC2的视图仍然是零。所以你会得到这个错误。
- 解决方法很简单,添加一个“?”在你的“didSet”函数中的每个视图之后。 并且不要忘记在VC2的viewDidload中调用“didSet”func。

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

我不认为我会帮你。我的代码在我的didSet方法中是否正确?我在哪里添加“?”在我上面的didSet代码?对不起在didSet等新手。 – Grace

+0

@Grace我添加了一些片段。 – wj2061

+0

现在它在“if let label = self.userName?”上出现错误错误在?插入。错误是:'?'必须后跟一个调用,成员查找或下标 – Grace

相关问题