0

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!

Simon M
  • 2,931
  • 2
  • 22
  • 37

1 Answers1

0

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.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
  • It's actually a Bar button on a Navigation Controller, does that make any difference? It appears on the view fine when compiled, i just don't know how to make it show the modalviewcontroller when clicked. – Simon M Mar 16 '11 at 09:25
  • @Simon: are you creating button through coding or through XIB? – Parth Bhatt Mar 16 '11 at 09:39
  • You can put your method `add` into your AppDelegate.m file. – Parth Bhatt Mar 16 '11 at 09:45
  • @Simon: If you are doing it through XIB then you need to select that button in XIB file and open Inspector-> Connections tab -> Connect selector outlet to (which is below File's Owner & First Responder) which would show your method `add` in a popup. Select your method `add` and connect it as selector to your Navigationbar button. Hope this helps you. – Parth Bhatt Mar 16 '11 at 09:48
  • I'm creating the button in the XIB, and I've tried that, but it refuses to show the modalviewcontroller, it just crashes. The code doesn't give an error, but it does give a warning: MyAppNameAppDelegate might not respond to presentModalViewcontroller: animated: – Simon M Mar 16 '11 at 22:26
  • I figured it out, as described above. Thanks for your help! – Simon M Mar 17 '11 at 01:56