我试图一个接一个地解雇两个视图控制器, 在我的第一个视图控制器中,我一直保持上传功能,当完成当前视图控制器时,该功能被解除。
现在,在上传ViewController后,我来到了之前的viewcontroller。我也想解雇“previousViewController”。
关闭“uploadViewController”后,我在“PreviousViewController”上调用方法
以下是我尝试这样做的方法:
在UploadViewController中:
[self dismissViewControllerAnimated:YES completion:^{
[previousView dismissME];
}];
现在当我来到“PreviousViewController”
时以下是dismissME
方法:
-(void)dismissME {
if (![[self presentingViewController] isBeingDismissed]) {
NSLog(@"Dismiss ME");
[self dismissViewControllerAnimated:YES completion:NULL];
}
}
我得到了“Dismiss ME”日志,但是viewController没有被解雇。
任何想法......?如何实现这个目标..?
答案 0 :(得分:5)
此代码有助于解除前两个viewcontrollers。
[[[self presentingViewController] presentingViewController] dismissModalViewControllerAnimated:YES];
答案 1 :(得分:3)
1)在 uploadViewController.h 中创建一个名为parent的变量,如下所示:
@property (nonatomic, strong) UIViewController *parent;
2)设置 uploadViewController self的父级,当它从 PreviousViewController 呈现时,如下所示:
uploadViewController *controller = [[uploadViewController alloc] initWithNibName:@"uploadViewController" bundle:nil];
controller.parent = self;
[self presentViewController:controller animated:YES completion:nil];
3)在 PreviousViewController 中定义一个方法来解除View,如下所示:
- (void) dismissView{
NSLog(@"Dismiss");
[self dismissViewControllerAnimated:YES completion:nil];
}
4)在解雇 uploadViewController 时调用该方法,
[self dismissViewControllerAnimated:YES completion:^{
[(ParentModeViewController *)_parent dismissView];
}];
我测试了它,它对我来说很好,希望它会帮助你
答案 2 :(得分:0)
试试这个:
-(void)myDismiss {
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)dismissME {
if (![[self presentingViewController] isBeingDismissed]) {
NSLog(@"Dismiss ME");
[NSTimer scheduledTimerWithTimeInterval:0.8 target:self selector:@selector(myDismiss) userInfo:nil repeats:NO];
}
}
修改强> 尝试使用PreviousViewController的viewDidAppear方法
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[NSTimer scheduledTimerWithTimeInterval:0.8 target:self selector:@selector(myDismiss) userInfo:nil repeats:NO];
}
如果它不起作用则增加scheduledTimerWithTimeInterval。如果它工作,那么将scheduledTimerWithTimeInterval减少到0.7,0.6 ...
答案 3 :(得分:0)
你可以使用:
[self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];
答案 4 :(得分:0)
您需要首先关闭uploadViewController,然后关闭PreviousViewController。请执行以下代码:
-(void)dismissView{
UIViewController *vc = [self parentViewController];
[self dismissModalViewControllerAnimated:NO];
[vc dismissModalViewControllerAnimated:YES];
}
希望这段代码可以帮到你。
答案 5 :(得分:0)
尝试检查是否在循环中多次添加视图控制器,或者等等,以便即使您删除一个实例,另一个视图控制器实例仍然存在。