Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
12/16/14 01:00 PM (9 years ago)

Question on switching views in Xcode

I'm finally getting my head around xcode development, but having a strange issue that I'm hoping someone else will know exactly why. When I switch views by loading an XIB (or storyboard... same behavior), I save some data to NSUserDefaults, then load the data into labels from the next view. Everything works great, but for some reason when I return to the original view, instead of loading the correct data, it's filling the 4 fields with different data. This only happens if I return to the view from a different view. If I stay in the original view, exit the app, then reload it, the data stays like it should. In my example, I have 4 hidden labels that either have a 1 or a 0 in them (poor mans boolean flags basically). If I set labels one and two to have a "1", save the values in NSUserDefaults, then exit the app and reload it, the values store and load perfectly fine. However, if I change to a different XIB or storyboard, then go back into the original, all 4 labels have "1" for their data, even though I didn't set it... only the first two should have 1, and the last two should show "0". I've tried playing around with code in the viewwillappear, and even tried [self viewDidLoad] in the viewWillAppear method, so that it reloads everything like its the first time the view is being loaded, but it still doesn't work. Has anyone else experienced something similar that could point me in the right direction?
 
Niraj
buzztouch Evangelist
Profile
Posts: 2943
Reg: Jul 11, 2012
Cerritos
37,930
like
12/16/14 03:18 PM (9 years ago)
Without my NSUserDefaults code in front of me: 1. Did you initialize the values for the labels? Is the initializer ion happening at the right time? 2. Did you synchronize? 3. Try putting break points to follow the flow of the code and to inspect the values of the variables. -- Niraj
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
12/16/14 03:20 PM (9 years ago)
Thanks Niraj, Yep, everything is initialized and synchronized. I'll try the breakpoint method and see what I can track down.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
12/16/14 03:52 PM (9 years ago)
I tend to put little debugger statements in every method I can when building, so I can keep track of where the code is at during execution. It helps when it comes time to track things down. Cheers! -- Smug
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
12/16/14 03:56 PM (9 years ago)
I do that quite a bit with NsLog statements, but didn't help me this time around. The funny thing is everything works perfect until I load a different xib and then return to the previous one. Even if I exit the app and start it back up, everything is as it should be. Really strange.
 
Dusko
Veteran developer
Profile
Posts: 998
Reg: Oct 13, 2012
Beograd
22,680
like
12/17/14 03:34 AM (9 years ago)
>exit the app and start it back up, everything is as it should be So, the initialization is not done properly, somewhere.
 
NCbuzz
Code is Art
Profile
Posts: 575
Reg: Sep 11, 2013
Lillington, NC
11,100
like
12/19/14 12:54 PM (9 years ago)
Is it possible that you are loading your values in the viewDidLoad and might need to add similar calls to read data when switching. XIB. May not be 'loading'.
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
12/19/14 01:04 PM (9 years ago)
That was my first thought as well. I added some duplicate calls later down the line and same issue. I even made a copy of viewDidLoad (called viewDidLoadAgain) and called it manually, same result. I think it was because I was mixing XIB and storyboard files. Unfortunately my main view is in Storyboard format, and I have so many objects on that screen, which are layered in a specific way, that copying them over to an XIB and reconnecting all of the individual outlets would be virtually impossible. Looks like I'll have to either cram everything into one view and show/hide things as needed, or find a way to convert a storyboard to an XIB (which I don't think is possible). Tried duplicating the .storyboard, renaming it to .xib and scanning the XML for any obvious changes that needed to be made, similar to how you can do that for different screen sizes on XIB files, but no luck there.
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.