0

我已经看到了一些关于此的问题,但没有一个可以帮助我解决此问题。

我有一个 MasterViewController -> AViewController -> BViewController

文档说:

呈现视图控制器负责关闭它呈现的视图控制器。但是,如果您在呈现的视图控制器本身上调用此方法,它会自动将消息转发到呈现的视图控制器。

[self.presentingViewController dismissModalViewControllerAnimated: YES];

从我的 B VC 调用此方法,应该关闭我的 A 和 B VC。然而,它只会解雇孩子(B VC)。为什么?

- (IBAction)checkButton:(UIButton *)sender {

    NSManagedObjectContext * context = [myAppDelegate managedObjectContext];
    Work * newWork = [NSEntityDescription insertNewObjectForEntityForName:@"Work" inManagedObjectContext:context];
    [newWork setName:_workName];
    [myAppDelegate saveContext];

    NSLog(@"%@", [self.presentingViewController description]);
    [self.presentingViewController dismissModalViewControllerAnimated: YES];

}

我不知道这是否相关,但是 A 和 B VC 的呈现方式如下:

    TWWorkNameViewController *controller = (TWWorkNameViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"WorkName"];
    [self presentViewController:controller animated:YES completion:nil];
4

3 回答 3

2

这是因为您的 MasterViewController 正在呈现 AViewController 而这是呈现 BViewController 所以当您使用 [self.presentingViewControllerdismissModalViewControllerAnimated: YES]; 您正在关闭 BViewController,因为这是堆栈顶部的那个,如果您想从 BViewController 返回 MasterViewController,您可以使用 Navigtioncontroller 并使用该方法

popToRootViewControllerAnimated:“弹出堆栈上除根视图控制器之外的所有视图控制器并更新显示。” http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html

如果您不想使用导航控制器,则需要使用通知:在您的 MasterViewcontroller 视图中确实加载了这样做:

- (void)viewDidLoad
{
[super viewDidLoad];

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

-(void)dismiss
{
  [self dismissViewControllerAnimated:YES completion:nil];
}

你的方法里面的 BViewController 只是说

- (IBAction)checkButton:(UIButton *)sender {
 NSNotification * notification = [NSNotification notificationWithName:@"BACKTOINDEXNOTE" object:nil];
 [[NSNotificationCenter defaultCenter] postNotification:notification];
}

因此,您将关闭 BViewController 和 AViewController。

于 2013-07-19T00:31:09.530 回答
0

我理解它的方式,“但是,如果您在呈现的视图控制器本身上调用此方法,它会自动将消息转发到呈现的视图控制器。” 指的是呈现的视图控制器本身不呈现任何视图控制器的情况。通常,呈现视图控制器负责关闭它呈现的视图控制器,但是如果你调用dismissModalViewControllerAnimated一个没有呈现任何东西的视图控制器,有两个选项 - 要么什么都不做,要么自己关闭它。它通过将该消息转发到其呈现的视图控制器来消除自己是有道理的。如果你打电话dismissModalViewControllerAnimated在呈现另一个视图控制器的视图控制器上,它应该关闭该视图控制器并且不将消息转发给它正在呈现的视图控制器(如果有的话),如文档所述。

于 2013-07-19T00:15:22.960 回答
-1

I don't know if this is the correct answer, but if you have A->B->C and you are on C and want to get back to A, meaning dismiss C and B at the same time, you can use the presentingViewController of B. I tested this with iOS 8. Both B and C are dismissed at the same time, in one animations.

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
于 2015-04-16T00:57:06.370 回答