Modal窗口不被解雇
问题描述:
我有两个编程创建的按钮,您可以在我的viewDidLoad方法中看到。在模态窗口中,我有一个按钮,通过委托调用cancelSearch方法。当我在我的cancelSearch方法上放置一个断点时,它被击中,所以我知道我的委托设置正确,但即使它调用这一行[self dismissViewControllerAnimated:YES completion:nil];它实际上并没有关闭模态窗口。Modal窗口不被解雇
以下代码全部来自我的主控制器视图。
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showActionMenu:)];
actionButton.style = UIBarButtonItemStyleBordered;
UIBarButtonItem *searchButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(showSearchMenu:)];
searchButtonItem.style = UIBarButtonItemStyleBordered;
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 103.0f, 44.01f)];
NSArray* buttons = [NSArray arrayWithObjects:actionButton, searchButtonItem, nil];
[toolbar setItems:buttons animated:NO];
self.navigationItem.title = @"Census Management";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
[[RKClient sharedClient] get:@"censusmanagement" delegate:self];
}
- (IBAction)showActionMenu:(id)sender
{
[self performSegueWithIdentifier: @"CMActionSegue" sender: self];
}
- (IBAction)showSearchMenu:(id)sender
{
ehrxCMSearchView *search = [[self storyboard] instantiateViewControllerWithIdentifier:@"cmSearch"];
search.selectedOptions = self.selectedOptions;
search.delegate = self;
[self.navigationController pushViewController:search animated:YES];
}
- (void)cancelSearch:(ehrxCMSearchView *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
答
你会使用类似的东西驳回模式视图
[self presentModalViewController:search animated:YES];
然而:其使用类似的东西装入的模态视图看您的代码段,它出现在搜索视图控制器正在使用下面的行推送到导航堆栈:
[self.navigationController pushViewController:search animated:YES];
所以我你可能需要跳出从导航堆栈的看法,而不是试图将其关闭作为模式视图:
[self.navigationController popViewControllerAnimated:YES];
答
如果您的视图控制器模态呈现,你应该这样做:
[self.presentingViewController dismissModalViewControllerAnimated:YES];
的presentingViewController属性是iOS 5中唯一可用的。所以,如果你的目标是老版本的iOS,你必须改用self.parentViewController(对每个iOS版本使用适当的版本,你必须处理这个)。
如果你能使你的父母这种控制/展示视图控制器,然后就称之为:
[self dismissModalViewControllerAnimated:YES];
这将解雇:
[self dismissModalViewControllerAnimated:YES];
我认为你是对的。我改变了它,因为我传递了它的数据。所以我这样做的方式,我想这甚至是一个模态的观点了。由于我有我的委托设置,我可以从主视图控制器吗?那里的最后一行看起来像我会从我展示的新视图中执行。 – Jhorra 2012-03-21 19:17:42
关于它的更多思考,我猜是因为我推动视图而不是将其视为模式,我应该能够将数据传回主控制器,然后手动从后者调用返回功能。 – Jhorra 2012-03-21 19:21:06
@Jhorra您可以从主视图控制器或新视图控制器调用-popViewControllerAnimated:因为它们(理论上)应该使用相同的UINavigationController。对于模态视图,您需要从呈现模态视图控制器(很可能是您的主视图控制器)的视图中调用-dismissModalViewControllerAnimated。 – 2012-03-21 19:49:51