Iphone:取消视图控制器和三个主要视图

Iphone:取消视图控制器和三个主要视图,iphone,objective-c,modalviewcontroller,Iphone,Objective C,Modalviewcontroller,我有一个问题当然解决了,但我找不到答案 我有一个应用程序,它有三个主要视图。就我而言,这意味着 我有主屏幕(屏幕A),可以转到屏幕B或屏幕C。 我有(屏幕B)可以从中转到屏幕A或屏幕C。 我有屏幕C,可以从中转到屏幕A或屏幕B。 我没有故事板,我正在使用presentModalViewController。因此,每当我从“孩子”切换到其他屏幕时,我都想解除家长的职务 例如: 屏幕A->屏幕B(取消调用屏幕C屏幕A)->屏幕C->屏幕A(再次创建屏幕A并删除屏幕B) 我尝试使用[self.pres

我有一个问题当然解决了,但我找不到答案

我有一个应用程序,它有三个主要视图。就我而言,这意味着
我有主屏幕(屏幕A),可以转到屏幕B或屏幕C。
我有(屏幕B)可以从中转到屏幕A或屏幕C。
我有屏幕C,可以从中转到屏幕A或屏幕B。

我没有故事板,我正在使用presentModalViewController。因此,每当我从“孩子”切换到其他屏幕时,我都想解除家长的职务

例如:
屏幕A->屏幕B
(取消调用屏幕C屏幕A)
->屏幕C->屏幕A
(再次创建屏幕A并删除屏幕B)

我尝试使用
[self.presentingModalViewController dismissViewControllerAnimated],但它引发异常

我能做什么


谢谢

您的应用程序是基于导航的吗?试试这个:

 YourABCController *controllerObj = [[YourABCController alloc] initWithNibName:@"YourABCController" bundle:nil];

UINavigationController *navcont = [[UINavigationController alloc] initWithRootViewController:controllerObj];

[self presentModalViewController:navcont animated:YES];

[commentsViewControllerObj release];

更好的是,您可以使用三视图控制器a、B和C使其成为基于TabBar的应用程序,这样您就可以轻松地在视图之间切换

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewControllerA = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
UIViewController *viewControllerB = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];
UIViewController *viewControllerC = [[ThirdViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewControllerA, viewControllerB,viewControllerC, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}

您可以这样做的一种方法是使用navigationController并按、按或按,然后根据需要按

或者,您可以使用NSNotifications通知父视图控制器关闭其子视图:

[[NSNotificationCenter defaultCenter] postNotificationName:@"switchToViewB" object:nil userInfo:nil];
以及在收到通知时调用其方法switchToViewB的父对象:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchToViewB:) name:@"switchToViewB" object:nil];

规范说明下一步关于-(void)dismissModalViewControllerAnimated:方法:

If you call this method on the presented view controller itself,
however, it automatically forwards the message to the presenting view controller.
If you present several view controllers in succession, and thus build a stack of 
presented    view controllers, calling this method on a view controller lower in
the stack dismisses its immediate child view controller and all view controllers 
above that child on the stack.
因此,如果在您的示例中您取消ScreenA,那么ScreenB也将取消


相反,您可以逐个使用UINavigationBar、uitabar或present view控制器

,如果您也提供N激励信息,则效果更好。