Navigating to another view controller without a navigation controller

11,823

Solution 1

in the first view controller you need this:

- (IBAction)pushWithoutViewController:(id)selector {
    NextNavigationController *page = [[NextNavigationController alloc] initWithNibName:NextNavigationController bundle:nil];
    CGRect theFrame = page.view.frame;
    theFrame.origin = CGPointMake(self.view.frame.size.width, 0);
    page.view.frame = theFrame;
    theFrame.origin = CGPointMake(0,0);
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.8f];
    page.view.frame = theFrame;
    [UIView commitAnimations];
    [self.view addSubview:page.view];
    [page release];
}

and then link it to the button in nib. :)

Solution 2

try :

SecondView *sv=[[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
[self presentModalViewController:sv animated:YES];
Share:
11,823
Veeru
Author by

Veeru

Updated on June 04, 2022

Comments

  • Veeru
    Veeru about 2 years

    As you guess am still a newbie, getting my head around iphone development. I am just trying out basic view loading on demand, which i cant get to work

    I have an app, with 2 view controllers, each view controller connected a different nib file. I am trying to switch between view manually; there is no navigation control involved.

    How can i manually push the second view to the first view? self.navigationController pushViewController wont work since there is no navigation controller. How else can I push the second view on top of the first view and destroy the first view; and ofcourse vice versa? I have done this in the first view's button action:

    SecondView *sv=[[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
    [self.navigationController pushViewController:sv animated:YES];
    

    obviously, it didn't work.

    window addSubView didn't work either, because the first view controller is the root view controller (not sure i said that right). In other words, when i run the app, the first view is what I see with a button that is supposed to load the second view.

    I have spent hours searching for a simple example, and I couldn't find any.

    Any suggestions?

  • Veeru
    Veeru over 13 years
    Great, Gyani & thomas, your solutions work! Gyani's solution only gives one animation, sliding it up, is there any option in the presentModalViewController where i can set the animation from right to left? Thomas solution is much more elaborate and i can control the animation whichever way and speed i want.
  • Veeru
    Veeru over 13 years
    hi gyani, is it possible to add a different kind of animation with that same statement?
  • Gyani
    Gyani over 13 years
    @Veeru Yes it is possible to add different kind of animation e.g SecondView *sv=[[SecondView alloc] initWithNibName:@"SecondView" bundle:nil]; sv.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [self presentModalViewController:sv animated:YES];
  • Veeru
    Veeru over 13 years
    Thanks a bunch gyani and thomas as well. I am giong with gyani's method as it involves less code. THanks once again :)
  • Raptor
    Raptor about 13 years
    this solution only replaces the view, instead of view controller. also, initWithNibName accepts first argument as NSString, instead of class. Therefore, it should be written as... initWithNibName:@"NextNavigationController"...
  • Thomas Clayson
    Thomas Clayson about 13 years
    Ah well spotted on the initWithNibName front... forgot my @""... :p You don't ever "replace" a view controller. Its only an object. All you're doing is owning the object instead of giving it to the navigation controller to control.