0

I have added a UIBarButtonItem in the main screen and created a b.xib, b.h and b.m files for it. when i click on the button it should open the respected screen but it doesn't and opens blank screen.

I am very new to iOS programming....

below is the code in A.m file where I added button and when click on it it should open b.xib.

UIBarButtonItem *bButton = [[UIBarButtonItem alloc]
                               initWithTitle:@"B"
                               style:UIBarButtonItemStyleBordered
                               target:self
                               action:@selector(openB:)];
self.navigationItem.leftBarButtonItem = bButton;
bButton.TintColor = [UIColor colorWithRed:0.0 green:0.31 blue:0.56 alpha:1.0];
[bButton release];

- (void)openB:(id)sender {
BViewController *b = [[BViewController alloc] init];
// ttod.defViewC = self; //Bookmarks from the definition view
[self presentModalViewController:b animated:YES];
[b release];
}

any idea...how can i make it work perfectly...

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
Anjali
  • 1,623
  • 5
  • 30
  • 50
  • check here...http://stackoverflow.com/questions/15621619/presentviewcontroller-shows-black-page – Bhavin Bhadani Jun 19 '15 at 08:18
  • you have to enter `nib` name – Bhavin Bhadani Jun 19 '15 at 08:19
  • 1
    Filenames **a**, **b** .... my head hurts. Firstly, instead of `presentModalViewController:` use `presentViewController:animated:` since the former was deprecated versions ago. You are not using ARC? Finally, when it's a XIB file you want to load, use `initWithNibName:` instead of `init`. – n00bProgrammer Jun 19 '15 at 08:22

3 Answers3

2
BViewController *b = [[BViewController alloc] init];

If you create a nib for the ViewController, you should use initWithNibName:bundle: instead.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
EES
  • 1,584
  • 3
  • 20
  • 38
  • added initWithNibName:@"B" bundle: nil....still same ....not displaying xib file... – Anjali Jun 19 '15 at 08:43
  • when i click on the botton...it gives "Presenting view controllers on detached view controllers is discouraged " message....what is this? – Anjali Jun 19 '15 at 09:12
1

Try this:

XIBs

BViewController *bViewController = [[BViewController alloc] initWithNibName:@"Myview" bundle:nil];

[self.view.window.rootViewController presentViewController: bViewController 
                                                      animated: YES 
                                                    completion: nil];

Storyboard

// Here BViewController should be the name in storyboard scene then,  
BViewController *bViewController  = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"BViewController"]

[self.view.window.rootViewController presentViewController: bViewController 
                                                      animated: YES 
                                                    completion: nil];

Hope this will help.

Note: Try to give some meaningful names to your view controllers rather using AViewController and BViewController

Mrunal
  • 13,982
  • 6
  • 52
  • 96
  • Are you using xibs still? – Mrunal Jun 19 '15 at 09:23
  • didn't work....now this is the error ::: setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key navBar. not using navBar anywhere.... – Anjali Jun 19 '15 at 09:30
  • Seems you have not set controller name in file owner option in xib file. Search about that. – Mrunal Jun 19 '15 at 09:32
  • And for your info, xibs are now no more in use, try to migrate in storyboards if you are developing new app. – Mrunal Jun 19 '15 at 09:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80987/discussion-between-mrunal-and-anjali). – Mrunal Jun 19 '15 at 09:33
1

you have to specify your nib name while presenting view like this

BViewController *b = [[BViewController alloc] initWithNibName:@"your_nib_name" bundle:nil];
[self presentModalViewController:b animated:YES];
Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39