0
votes

I'm working on my first iPhone application and have stumbled upon an unknown bug in my code. The code works perfectly fine, but the problem lies when I tap a custom UIButton I've made and when it loads the next view. I have a desired buttonPressed IBAction that is called to handle what information loads on the next view controller, but the order of the function calls is wrong. I have 5 different buttonPressed functions and 3 out of the 5 are working correctly. Note I implemented them the same exact way through Storyboard and even tried re-making the non-working ones. I'd post code but the code is basic if/else statements. I'm just not sure why 3 out of the 5 ways would work and the other 2 wouldn't. Any help would be GREATLY appreciated.

**This is the desired order for the working 3:

viewDidLoad -> Loads main screen (initial view controller AKA ROOT, On main screen, you have 5 UIButtons to choose a category, tap a button to load to a new view controller)

categoryButtonPressed -> Sets counter (seques to new view controller)

viewDidLoad -> Loads Screen to narrow down search

(Now you have 10+ Custom Buttons to Tap on, each will be handled through a buttonPressed IBAction function, and once button is tapped and function is called, a new view is loaded)

buttonPressed -> Sets counter (seques to new view controller)

viewDidLoad -> Loads correct information

This is the order for the non-working 2:

viewDidLoad -> Loads main screen (initial view controller AKA ROOT) (On main screen, you have 5 UIButtons to choose a category, tap a button to load to a new view controller)

categoryButtonPressed -> Sets counter (seques to new view controller)

viewDidLoad -> Loads Screen to narrow down search (Now you have 10+ Custom Buttons to Tap on, each will be handled through a buttonPressed IBAction function, and once button is tapped and function is called, a new view is loaded)

viewDidLoad -> Loads blank information (blank screen)

buttonPressed -> Sets counter but is to late as the view already was loaded blank

Thoughts?**

Here is an example of the categoryButtonPressed:

-(IBAction)categoryButtonPressed:(id)sender

{

if([sender tag] == 1)

    counter = 1;

else if([sender tag] == 2)

    counter = 2;

.

.

.

}

NOTE: I have each custom button tagged for a specific item to be loaded. This function is just to set a counter so another function can load a specific file.

1
@LuisOscar thanks, you are right, I should have added a comment, not an answer... my bad. Very hard to tell without more information.... Am i correct in understanding that your button press should be setting some parameter of the upcoming viewController and then transitioning to that view controller? It seems strange that the secondary view controller should load without pressing the button, which is what it looks like above. Where do you initialize that controller in your code?MobileVet
I've updated the problem, and maybe now you can get understand what I am trying to do and figure out.James Brown

1 Answers

0
votes

Use segues.

On your main view controller set up the segues by loading the desired information on the view controller when a specific segue is pressed.

You can follow this guide which is the way you should do it if you are using story boards

http://www.scott-sherwood.com/?p=219

Look for the "prepareForSegue" methods, when using storyboards, modally presented views should be pre configured this way.

On a side note, the "viewDidLoad" is usually not used to load another view, it should be used to configure that view after it has been loaded.