Glory to Russia
Glory to Russia

Reputation: 18712

How to display a view?

I'm new to iOS development.

I have a ViewController ViewController with a button. When the user presses that button, I want to switch the view to RegisterViewController.

ViewController.m contains following code:

#import "ViewController.h"
#import "RegisterViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize registerViewButton;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)registerViewButtonClick:(id)sender {
    NSLog(@"registerViewButtonClick called");

    RegisterViewController* controller = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];    
}

@end

According the debug output, method registerViewButtonClick is actually called, when I press the button.

But the view represented by RegisterViewController doesn't appear.

The code of the application is available here.

What do I need to change in order for the RegisterViewController's view to become visible, when the button is pressed?

Upvotes: 2

Views: 95

Answers (5)

Jayaprada
Jayaprada

Reputation: 954

  • If you want to present then no need of UINavigationalController.
  • But for pushing UINavigationalController is must .
  • In Storyboard, you have to add one UINavigationalController.

enter image description here

  • In Button Actions initialise the VC correctly with nib Name.
 RegisterViewController* controller = [[RegisterViewController alloc] initWithNibName:@"RegisterViewController" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];

Upvotes: 1

GenieWanted
GenieWanted

Reputation: 4513

I ran your code and found that there is no navigation controller implemented in your code but you are trying to push the registerViewController. Try to present the viewcontroller like below:

[self presentViewController:controller animated:YES completion:nil];

instead of

[self.navigationController pushViewController:controller animated:YES]; 

The pushViewController works only when there is a UINavigationController. As per the documentation, The UINavigationController class implements a specialized view controller that manages the navigation of hierarchical content. Since, there is no UINavigationController in your code (self.navigationController is nil in this case), nothing happens when you try to push the viewController.

UINavigationController also comes handy when you want to maintain a stack of viewControllers wherein you can push or pop as per the need. This also gives you 'Back' button automatically. If your need is just to present a viewcontroller, then presentViewController: can be the right option.

Upvotes: 2

Popeye
Popeye

Reputation: 12093

If you are using storyboards then you need to get your view controller from the storyboard like

RegisterViewController *viewController = (RegisterViewController*)[[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"vc-identifier"];

Replace vc-identifier with whatever you have given as the identifier in storyboards and replace MainStoryboard with the name of your storybaord.

then pass it into your navigation controller like you are already doing, that is as long as your navigation controller isn't nil

[self.navigationController pushViewController:controller animated:YES];   

Just been told that you aren't using storyboards, I was unable to download your link because of my location so I gave a solution with storyboards. My recommendation would be to move to using storyboards.

Will leave this here as it may help others who are having the same problem but are using storyboards

Upvotes: 0

2014
2014

Reputation: 119

RegisterViewController *news=[[RegisterViewController alloc] initWithNibName:@"RegisterViewController" bundle:nil];
[self.navigationController pushViewController:news animated:YES];
[news release];

Upvotes: 0

David Xu
David Xu

Reputation: 5597

You might need to initialize the UIViewController with the nib name..

like this

RegisterViewController* controller = [[RegisterViewController alloc] initWithNibName:@"RegisterViewController" bundle:nil];

Upvotes: 0

Related Questions