我在viewDidLoad方法中可以看到两个以编程方式创建的按钮。在模态窗口中,我有一个通过委托调用cancelSearch方法的按钮。当我在cancelSearch方法上放置一个断点时,它被命中,所以我知道我的委托设置正确,但即使它调用了这一行[self dismissViewControllerAnimated:YES completion:nil];它实际上并没有关闭模态窗口。
以下代码全部来自我的主控制器视图。
- (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];
}
答案 0 :(得分:4)
你会使用类似于:
的东西来解雇模态视图[self dismissModalViewControllerAnimated:YES];
这将取消使用类似于:
的东西加载的模态视图[self presentModalViewController:search animated:YES];
然而,查看您的代码段,搜索视图控制器似乎正在使用以下行推送到导航堆栈:
[self.navigationController pushViewController:search animated:YES];
所以我可能需要从导航堆栈弹出视图,而不是试图将其视为模态视图:
[self.navigationController popViewControllerAnimated:YES];
答案 1 :(得分:0)
如果你的视图控制器是模态呈现的,你应该使用它:
[self.presentingViewController dismissModalViewControllerAnimated:YES];
presentationViewController属性仅在iOS 5中可用。因此,如果您的目标是旧版本的iOS,则必须使用self.parentViewController(对每个iOS版本使用适当的版本,您必须处理此问题)。
如果您在父/呈现视图控制器中进行此控制,则只需调用它:
[self dismissModalViewControllerAnimated:YES];