开发者

presentModalViewController and navigationController

开发者 https://www.devze.com 2023-02-28 02:18 出处:网络
I want to know the differences between the [self presentModalViewController:controller animated:YES];

I want to know the differences between the

[self presentModalViewController:controller animated:YES];

and

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

I开发者_高级运维 have used both but still do not know or noticed the difference. when should use one of them ?

Thanks..


Basic difference :

pushViewController only works in navigation controllers

presentModalViewController works for all view controllers

navigationController is the instance of your UINavigationController, which is used by all the controller in your navigation stack (UIViewController).


Presenting a modal view is presenting a view on top of another view. You perform those typically for "tasks" that need to be started and completed in a self contained way. Read further on modal views on the apple developer guides.

Pushing a view on to the navigation controller is different where there is a logical need for navigation in the app. Say a drill down table as in the setting app of the iDevices, where there are main settings then you drill down to sub settings etc.

Whatever your questions are, if they are conceptual and generic as this I'd strongly advise you to google up "X programming guide" which will take you to the proper Apple programming guide :) X = view controller in your case

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html


if base class has it's own NavigationController then you can write:

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

if your base class has only UIViewController then use:

MyViewController * objMyViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController:objMyViewController];
navController.navigationItem.leftBarButtonItem  = nil;  // make nil if you want
                                                        // to use it in next View
[self presentModalViewController:navController animated:YES];

now, MyViewController has navigation so you can -- Push -- another viewController by writing function as bellow in MyViewController.

-(IBAction)btnNext_click {
    SecondViewController * objSecondViewController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
    [self.navigationController pushViewController:objSecondViewController animated:YES];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号