Question

I'd like to display a ModalViewController from a bar button in the MainWindow.xib file. How would I do this? The basic code I'm looking to use is this:

-(IBAction)add {
    myCustomViewController *add = [[myCustomViewController alloc] initWithNibName:@"myCustomViewController" bundle:nil];
    [self presentModalViewController:add animated:YES];
    [add release];
}

But where do I put it?

EDIT: I figured it out, in my navigation controller i put the following code in viewDidLoad:

UIBarButtonItem *addbutton = self.navigationItem.leftBarButtonItem;
[addbutton setTarget:self];
[addbutton setAction:@selector(add)];

and changed the function to:

- (void)add {
    myCustomViewController *add = [[myCustomViewController alloc] initWithNibName:@"myCustomViewController" bundle:nil];
    [self presentModalViewController:add animated:YES];
    [add release];  
}

Thanks for your help, Parth!

Was it helpful?

Solution

I fear that this is not possible.

You will have to put a viewController inside MainWindow.xib and put button on that viewController because you cannot add controls (like button in your case) on UIWindow.

It is required to be of type UIViewController or UITableViewController for you to be able to add UIControls to it.

Hope this helps you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top