tags:

views:

49

answers:

2

I have an app that uses a number of views, and it was getting pretty awkward swapping between them so I thought it might be an idea to make some code that would automatically go to a numbered page, load the appropriate view, then perform any setup that needed doing based upon that view. I came up with the following. The test page I've set up it swaps to OK and even sets the delegate properly, but it just won't perform any setup operations on the contents of the page, such as changing the text on the labels it contains or flicking the toggle switch to ON. I've been through the program step by step and all of the commands are being executed, but the results just don't show up on the final screen.

currentMenu is a UIViewController pointer that I aim at the current ViewController in use so that I know what to dismiss later. clusterMenu is a specific view controller of type ClusterMenu that I am using as a sample page to switch to.

It also occurs to me that the resources might not release properly with this code, but that's something I can deal with later once I can work out why it's not being set up correctly in the first place.

Here is the code:

UIViewController *vanishing = currentMenu;

if (page == 3){
    clusterMenu = [[ClusterMenu alloc] initWithHeader:clusterMap.header];
    [clusterMenu setDelegate:self];
    clusterMenu.editSwitch.on = clusterMap.editMode;
    [clusterMenu changeEditStatus];
    currentMenu = clusterMenu;
}

[UIView transitionFromView:vanishing.view toView:currentMenu.view duration:0.5 
                options:UIViewAnimationOptionTransitionCurlUp 
                completion:^(BOOL done){[vanishing release];}];
A: 

Good question. Given the limited code that was posted, it's tough for me to guide you on specific changes. Instead, I'd recommend having a look at an Apple sample projet (link below) that should help you on your way. You can probably use the Apple architecture and simply shoehorn in your view controllers to get the page swapping behaviour.

http://developer.apple.com/iphone/library/samplecode/PageControl/Introduction/Intro.html

I hope this helps. Sorry I couldn't give you a one-line fix. Andrew

Andrew Little
Sadly it doesn't. I've looked at that code a number of times, and it's using a very different technique. I think the problem I am having is something to do with how and when NIB files are loaded. My suspicion is that loading happens in its own thread, so that any subsequent commands are likely to be ignored since the NIB isn't actually in memory yet. If I am right, that means I have to find some way of halting program execution until loading is complete, or of appending additional code onto the command that will execute once the NIB is fully in memory.Thanks for trying though!
Ash
interesting. Okay, I don't think you'll want to halt program execution. Instead, have a look at initWithCoder which is part of the NSCoding Protocol. This protocol is subscribed to by UIView and UIViewController, as an example. This might be a good place to put your init code. Also read through this guide for more detail: http://developer.apple.com/iphone/library/documentation/cocoa/conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW8
Andrew Little
Thanks, will do! I tell ya, the Apple database is OK for specific object reference, but it really needs more (and easier to find) tutorials.
Ash
cool! don't forget to vote up the answer if it's helpful - I love my rep pts ;-)
Andrew Little
A: 

Put your initialization commands in the view's init method, and wait for it to get called. Don't halt, as this will prevent the views from getting loaded, just exit your current method instead.

In Cocoa, you don't tell the OS what to do, you wait for it to tell you.

hotpaw2
First thing I tried. Didn't work. No idea why. I've since changed tack entirely and it's working, so I'm not going to worry too much about why. Thanks for the information though.
Ash