-4

I am working on single view application. On click of button i want to switch to next page(let say menu controller). Please specify what changes I have to make in appdelegate to add a navigation controller as I am completely new to iOS.

[button addTarget:select action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside]; and implement the selector as

-(void)buttonClick{
UIViewController *menu = [[UIViewController alloc] init];
[self.navigationController pushViewController:menu animated:YES];
}
aayush
  • 26
  • 1
  • 5

4 Answers4

3

Add this in the app delegate for the root view controller:

FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"view name" bundle:nil]; 
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
self.window.rootViewController = navigationController; 

Inside the button click:

SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"view name" bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
Prasanth S
  • 3,725
  • 9
  • 43
  • 75
  • 1
    Actually, there are a few mistakes, I'm noticing now... Like UIViewController shouldn't = Login . And there needs to be a bracket at the end at the end of the first line in the 2nd code block. – Lyndsey Scott Dec 09 '14 at 05:00
  • Hm... Other than the things I mentioned, I'm trying to figure out whether there's any possible scenario where `initWithNibName:` would be used to initialize a regular `UIViewController` and not a custom view controller. Not sure... – Lyndsey Scott Dec 09 '14 at 05:09
  • No, I think you need to specify two custom views, ex. FirstViewController *firstViewController = [[FirstViewController... and SecondViewController *secondViewController = [[SecondViewController... – Lyndsey Scott Dec 09 '14 at 05:17
  • i am not able to get it. firstviewcontroller would be ViewController in mycase ? and SecondViewController as menu ?? – aayush Dec 09 '14 at 06:19
  • kk...and wht about initwithnibname ? we could keep it anything ? – aayush Dec 09 '14 at 06:28
  • the thing is i am not using an xib....i created a button in ViewController.m and on click of that button.. wished to create an another view in menu.m file on viewdidload similarly !! – aayush Dec 09 '14 at 06:40
  • @S.P i manually created a button From ViewController.m ! now on click of that button i want another file to open (say) menu.m ! without the use of storyboard or xib !! – aayush Dec 09 '14 at 06:46
2

you have to set UINavigationController as Window.rootViewController like below.

FirstViewController *viewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navigationController;
Abhishek Sharma
  • 3,283
  • 1
  • 13
  • 22
1

You should subclass UIViewController and implement the view however you want. Views can be built programmatically or in interface builder.

Then you would either use segues, storyboard identifiers, or a xib file to load the view.

Might look like this: (assuming you set up the view controllers in a storyboard and connected them with appropriate segues & the identifier below)

-(void)buttonClick{
    [self performSegueWithIdentifier:@"MySegueIdentifier"];
}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString: @"MySegueIdentifier"]) {
        MyCustomViewController* vc = (MyCustomViewController*)[segue destinationViewController];
        //do something with your view controller, like set a property
    }
}

Or maybe like this

-(void) buttonClick {
    MyCustomViewController* vc = (MyCustomViewController*)[[UIStoryboard storyboardWithName: @"MyStoryboard"] instantiateViewControllerWithIdentifier: @"MyStoryboardIdentifier"];
    [self.navigationController pushViewController: vc animated: YES]; //assuming current view controller is a navigation stack. Or could also do presentViewController:animated:
}

You would add a class subclassing UIViewController to your project, and import it (#import "MyCustomViewController.h" in the .m file of the view controller you're pushing from).

Could also use a xib file, but I won't bother with those since Storyboards are much easier to work with.

Without a storyboard: Inside the app delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    navigationController = [[UINavigationController alloc] init]; //navigation controller is a property on the app delegate
    // Override point for customization after application launch.
    FirstViewController *firstViewController = [[FirstViewController alloc] init];
    [navigationController pushViewController: firstViewController animated:NO];
    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    return YES;
}

Inside your FirstViewController:

-(void) buttonClick {
    MyCustomViewController* vc = [[MyCustomViewController alloc] init]; // or maybe you have a custom init method
    [self.navigationController pushViewController: vc animated: YES];
}
shim
  • 9,289
  • 12
  • 69
  • 108
0

Very simple:

-(void)ButtonClickActionMethod
{
    [button addTarget:select action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
}

Clicked Action:

-(void)buttonClick{
    YourViewController *view = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
    [self.navigationController pushViewController:view animated:YES];
}
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90