shows blank screen when click on button in iOS

369 views Asked by At

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...

3

There are 3 answers

5
Mrunal On

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

0
Tejas Ardeshna On

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];
2
siutsin On
BViewController *b = [[BViewController alloc] init];

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