views:

321

answers:

2

Hey there,

Is there a way to load a view modally without seeing it? I need to access methods on the modal view controller which in turn set properties of UI components in a XIB. I want to set those properties, but I don't want to see the view right away. I want to reveal it when the user rotates the phone.

Everything is working, except the solution involves presenting the modal view as soon as the app loads.

Thanks, Howie

A: 

If you have another view, could you insert this view under the existing view using

(void)insertSubview:(UIView *)view atIndex:(NSInteger)index

then when the device rotates, switch the views?

justin
That sounds like an interesting solution, but I'm trying to do it with a modal view so I'm not sure that would work. Would it?
Ward
A: 

In a view controller you can create an outlet for another controller. All you need to do is something like this in the same nib file in interface builder:

MainController         UIViewController
->UIview               UIView

ModalController        UIViewController
->ModalControllerView  UIView

in MainController.h, create an outlet for the view controller:

@property(nonatomic, retain) IBOutlet ModalController *ModalController

(and of course you need the ivar and include also)

In Interface Builder, connect ModalController mainController to the ModalController view using option drag or however you want.

When the nib loads, you'll get modalController set to an instantiated ModalController, then methods in MainController can access modalController and do whatever they want to it. Then you just have to use presentModalView to present it.

I think the more typical way to do this, though, is this:

- (IBAction)showInfo {    

    FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
    controller.delegate = self;

    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];

    [controller release];
}

(That's directly from the Utility Application project template in Xcode.)

In other words, create the modal controller and view in it's own nib file then load the nib file manually as needed. This is more memory efficient since it doesn't instantiate the object until it's actually needed.

There's also no reason why you can't just do:

    modalController = [[ModalViewController alloc] initWithNibName:@"ModalView" bundle:nil];

in MainController's viewDidLoad or initWithCoder method or something, then proceed to configure it however you want.

Nimrod
Cool thanks! I appreciate it.
Ward