0

I have 2 view controllers, VC1 displays all the users on my apps basic info, when you press a more info button in a certain cell it segues to a new VC that displays the users full profile that was in the cell that you previously pressed.

With help I have everything working except for how to display the results on VC2. I am trying to set a property in the VC2. I also tried for

This code is returning no errors but displays nothing on the VC2.

I've tried set, get willSet, everything and can't crack it:

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() {

I tried this one but it returns nothing but an empty 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()
    }

I have researched didSet and willSet but I am still at a loss. Any help would be greatly appreciated. What is the next step can someone point me to a tutorial or advise me please.

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
}
}
Community
  • 1
  • 1
Grace
  • 377
  • 3
  • 19
  • Try an [exception breakpoint](https://developer.apple.com/library/ios/recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html) it might help you figure out where the problem is – Leonardo Nov 06 '15 at 18:01
  • can you show the code where you set `userToShowDetail` when pushing from vc1 to vc2 please? – André Slotta Nov 06 '15 at 18:06
  • @AndreSlotta sorry I updated my code there. – Grace Nov 06 '15 at 18:15

1 Answers1

0

This is a common mistake when you are newer to swift.
- When you segue to VC2, and pass a value to "userToShowDetail".The "didSet" method of "userToShowDetail" will be called.
- Unfortunately, at this point, your VC2's views are still nil.So you will get this error.
- The solution is simple,add a "?" after each view in your "didSet" func. And don't forget to call "didSet" func in VC2's viewDidload.

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
}
wj2061
  • 6,778
  • 3
  • 36
  • 62
  • I don't think I get you. Is my code correct above in my didSet method? Where do I add the "?" in my didSet code above? Sorry complete novice at didSet etc. – Grace Nov 06 '15 at 18:39
  • @Grace I added some snippet. – wj2061 Nov 06 '15 at 18:47
  • Now it is getting an error on "if let label = self.userName?" The error is on the ? inserted. The error is: '?' must be followed by a call, member lookup, or subscript – Grace Nov 06 '15 at 19:00
  • change this line of code to "if let label == self.userName?" – wj2061 Nov 06 '15 at 19:09
  • New error "variable binding in a condition requires an initializer" on the { after "if let label == self.userName?" – Grace Nov 06 '15 at 19:26
  • In your case you should just get rid of this line of code.func configureView() { if let userToShowDetail: PFUser = self.userToShowDetail { userName?.text = userToShowDetail["name"] as! String?//add "?" to VC2's subviews } } – wj2061 Nov 06 '15 at 19:29
  • Okay so now I have no errors, it doesn't crash or return any errors but it still is not updating the label called 'username' with the user info? – Grace Nov 06 '15 at 19:37
  • Did you add configureView() to viewDidload() ?? – wj2061 Nov 06 '15 at 19:42