2

I am new to iPhone programming. what I am trying is I have one screen with a button. And I want to change the view controller not only the view when I click that button (I know how to add subview) because from that 2nd view controler, I have to go to the third view which is not possible if I add subview in first place. Can anybody please help me with that? Is this possible? and if yes, how? All the views and view controller are created programmaticaly. I am not using IB.

EDIT: here is the relevant code that fires when clicking button

-(id)showCurrentLoc:(id)sender { 
 locationController = [currentLocController alloc]; 
 [entry removeFromSuperview]; 
 [newLoc removeFromSuperview]; 
 [currentLoc removeFromSuperview]; 
 [self.view setBackgroundColor:[UIColor clearColor]]; //[self.view addSubview: [locationController view]]; 
 [self.navigationController pushViewController:locationController animated:YES];  [locationController release]; 
 return 0; 
} //Location Controller is the tableViewController

Thanks Vik

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
Vik
  • 488
  • 2
  • 10
  • 19

5 Answers5

4

You can do something like this

        YourViewController *objYourViewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
        [self.navigationController pushViewController:objYourViewController animated:YES];
        [YourViewController release];
Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
  • that is what I amtrying but the thing is I am trying to lad tableViewController as my second viewController but this is not working for that. If I simply add [self.view addSubView:[xyztableview view]]. then i can see my table view. I don't know whats wrong in the first approach? – Vik Apr 28 '11 at 06:59
  • @Vik how do u define tableViewController i.e. declaration ? Can you please post code for creating object for your tableViewController ? – Janak Nirmal Apr 28 '11 at 12:21
  • @Jennie- Thanks I am able to change the view controller using navigation controller after declaring it in delegate. – Vik Apr 28 '11 at 12:50
3

Usually, you use a navigation controller for this sort of thing so that the user can easily go back to the previous view. Your view controller would then do something like this:

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

If you want to manage the view controllers yourself, you can always just change the window's rootViewController property. Please read View Controller Programming Guide for the complete picture.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • @Caleb- that is what I amtrying but the thing is I am trying to lad tableViewController as my second viewController but this is not working for that. If I simply add [self.view addSubView:[xyztableview view]]. then i can see my table view. I don't know whats wrong in the first approach? – Vik Apr 28 '11 at 07:00
  • @Vik Sorry -- I can't make sense of your comment. Since you're the OP, I suggest that you edit your question to explain the actual problem more clearly. Before you do that, however, please take a careful look at the document I referenced. It's required reading for iOS developers, and it definitely explains how to move from one controller to another. – Caleb Apr 28 '11 at 07:08
  • -(id)showCurrentLoc:(id)sender { locationController = [currentLocController alloc]; [entry removeFromSuperview]; [newLoc removeFromSuperview]; [currentLoc removeFromSuperview]; [self.view setBackgroundColor:[UIColor clearColor]]; //[self.view addSubview:[locationController view]]; [self.navigationController pushViewController:locationController animated:YES]; [locationController release]; return 0; } //Location Controller is the tableViewController – Vik Apr 28 '11 at 07:12
  • 1
    For starters, you're allocating but not initializing locationController. Next, if you're using a nav controller, you shouldn't need all that addSubview/removeFromSuperview stuff; just push the new view controller onto the navigation stack. – Caleb Apr 28 '11 at 13:04
  • @Caleb Please can you tell me if the viewcontroller that I want to push is uitabbarcontroller? I am also confused regarding switching from one VC to another. 99% people said use uinavigationcontroller it automatically handle switching between VCs. I want to custom manage switching between VCs. – S.J Jul 10 '13 at 11:35
  • @S.J I don't know what you're trying to do, but the term "push" w.r.t. view controllers *comes from* UINavigationController's stack-like organization. Also, you never push a tab controller onto a navigation stack -- tab controllers can contain navigation controllers, but not the other way around. Please first read [View Controller Programming Guide](http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/) and then post a question if you're still stuck. – Caleb Jul 10 '13 at 13:07
  • @Caleb I want to switch from one view controller to another, lots of people says using uinavigationcontroller is the best option. Ok I have uinavigationcontroller init with rootview which is subclass of uiviewcontroller named view1 now I want to push another viewcontroller but its a uitabbarcontroller, now what? how to manage this situation? yes I know very well uitabbarcontrller cannot be pushed on uinavcon. Or is there any manual way to manage this viewcontroller switching? – S.J Jul 11 '13 at 04:26
  • @S.J You should really start your own question rather than using comments here. Search first, though, because it sounds like a duplicate of a number of existing questions like [this one](http://stackoverflow.com/q/15552722/643383). – Caleb Jul 11 '13 at 07:09
  • @Caleb Please see what I am trying to ask, I am not asking how to push tabbarcon in uinavcon. – S.J Jul 11 '13 at 07:32
  • @S.J Whatever you're trying to ask, it would be best done in a separate question, not in comments here. – Caleb Jul 11 '13 at 07:35
2

UINavigationController is what you need. It manages a stack of UIViewControllers and if you want to add new UIViewController just push it into this navigation stack. It automates back button behavior for you, and you can pop your current UIViewController from stack whenever you are done with it.

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • @Krishnabhadra- that is what I amtrying but the thing is I am trying to lad tableViewController as my second viewController but this is not working for that. If I simply add [self.view addSubView:[xyztableview view]]. then i can see my table view. I don't know whats wrong in the first approach? – Vik Apr 28 '11 at 07:00
  • It should work..Can you post relevant code so that we can cross check – Krishnabhadra Apr 28 '11 at 07:01
  • here is the relevant code that fires when clicking button -(id)showCurrentLoc:(id)sender { locationController = [currentLocController alloc]; [entry removeFromSuperview]; [newLoc removeFromSuperview]; [currentLoc removeFromSuperview]; [self.view setBackgroundColor:[UIColor clearColor]]; //[self.view addSubview:[locationController view]]; [self.navigationController pushViewController:locationController animated:YES]; [locationController release]; return 0; } //Location Controller is the tableViewController – Vik Apr 28 '11 at 07:13
  • @Vik..dont put code in comment..You can edit your original question and put code there.. – Krishnabhadra Apr 28 '11 at 07:17
  • @Barry Removed the link to tutorial. Anyway it was not needed since I answered orignal question, which was later edited as user used UINavigationController. – Krishnabhadra Jan 17 '13 at 12:07
  • @Krishnabhadra No need to justify, I just was curious about the blog article. Thanks for cleaning up. –  Jan 19 '13 at 07:20
0

You could work with a UINavigationController. Adding your first UIViewController like this in the init method:

        [self setViewControllers:[NSArray arrayWithObject:viewController]];

And then when a button is click or a choice is made, your push the second few controller with (in the first viewController):

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

This way you will also get an automatic (back button). Basicly you create a stack of UIViewControllers that you can push and pop like with a normal stack.

I hope this helps. Look into the following: UINavigationController Class Reference

ophychius
  • 2,623
  • 2
  • 27
  • 49
0
- (void) loadViewAtIndex:(NSInteger)index {

    [self unloadViewAtIndex:activeViewIndex];

    switch (index) {

        case 1:
        {
            if (viewController1 == nil)
            {
                viewController1 = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil];
            }

            viewController1.view.frame = CGRectMake(0.0, 0.0, viewController1.view.frame.size.width, viewController1.view.frame.size.height);

            [window addSubview:viewController1.view];
        }
            break;

        case 2:
        {
            if (viewController2 == nil)
            {
                viewController2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
            }

            viewController2.view.frame = CGRectMake(0.0, 0.0, viewController2.view.frame.size.width, viewController2.view.frame.size.height);

            [window addSubview:viewController2.view];
        }
            break;

        default:
            break;
    }

    activeViewIndex = index;
}

- (void) unloadViewAtIndex:(NSInteger)index {
    switch (index) {

        case 1:
        {
            if (viewController1 != nil)
            {
                [viewController1.view removeFromSuperview];
                [viewController1 release];
                viewController1 = nil;
            }
        }
            break;

        case 2:
        {
            if (viewController2 != nil)
            {
                [viewController2.view removeFromSuperview];
                [viewController2 release];
                viewController2 = nil;
            }
        }
            break;

        default:
            break;
    }
}
Chetan Bhalara
  • 10,326
  • 6
  • 32
  • 51