2

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?

Michael Kohne
  • 11,888
  • 3
  • 47
  • 79
Glory to Russia
  • 17,289
  • 56
  • 182
  • 325
  • 1
    Do you actually have a navigation controller? In other words: Is `self.navigationController` not `nil`? – herzbube Jun 13 '14 at 12:54

5 Answers5

2

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.

GenieWanted
  • 4,473
  • 4
  • 24
  • 35
  • If you feel generous you could edit your answer and tell the OP why messages sent to `nil` don't do anything. – herzbube Jun 13 '14 at 12:59
  • @GenieWanted Many thanks for your answer. Forgive me a dumb question, but why does a call to a `nil` object not cause a `NullReferenceExeption` or something similar (like it would in Java and C#) ? – Glory to Russia Jun 13 '14 at 13:24
  • 1
    This is infact an interesting question.. This is greatly explained here http://stackoverflow.com/questions/2696891/calling-a-method-on-an-unitialized-object-null-pointer ... It's all about how Objective C looks at it. – GenieWanted Jun 13 '14 at 13:31
1
  • 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];
Jayaprada
  • 944
  • 8
  • 11
0

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

like this

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

David Xu
  • 5,555
  • 3
  • 28
  • 50
0
RegisterViewController *news=[[RegisterViewController alloc] initWithNibName:@"RegisterViewController" bundle:nil];
[self.navigationController pushViewController:news animated:YES];
[news release];
2014
  • 119
  • 1
  • 1
  • 13
0

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

Popeye
  • 11,839
  • 9
  • 58
  • 91