【问题标题】:Modal window not being dismissed模态窗口没有被关闭
【发布时间】:2012-03-21 18:54:30
【问题描述】:

我有两个以编程方式创建的按钮,您可以在我的 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];
}

【问题讨论】:

    标签: ios ios5 delegates modalviewcontroller


    【解决方案1】:

    您可以使用类似于以下内容的方式关闭模态视图:

    [self dismissModalViewControllerAnimated:YES];
    

    这将关闭使用类似于以下内容的模式视图:

    [self presentModalViewController:search animated:YES];
    

    但是查看您的代码 sn-p,似乎搜索视图控制器正在使用以下行推送到导航堆栈:

    [self.navigationController pushViewController:search animated:YES];
    

    所以我可能需要从导航堆栈中弹出视图,而不是试图将其作为模式视图关闭:

    [self.navigationController popViewControllerAnimated:YES];
    

    【讨论】:

    • 我认为你是对的。我改变了它,因为我正在传递数据。所以我这样做的方式,我想它甚至不再是一个模态视图了。既然我已经设置了我的委托,我可以从主视图控制器中进行吗?您那里的最后一行看起来我会从我显示的新视图中执行。
    • 再想一想,我想既然我正在推送视图而不是将其视为模式,我应该能够将数据传回主控制器并手动调用返回函数对吗?
    • @Jhorra 您可以从主视图控制器或新视图控制器调用 -popViewControllerAnimated:,因为它们(理论上)应该使用相同的 UINavigationController。对于模态视图,您需要从呈现模态视图控制器(很可能是您的主视图控制器)的视图中调用 -dismissModalViewControllerAnimated。
    • 我最终改变了使用 segue 并使用 prepareForSegue 传递数据的方式。
    【解决方案2】:

    如果你的视图控制器是模态显示的,你应该使用这个:

    [self.presentingViewController dismissModalViewControllerAnimated:YES];
    

    presentingViewController 属性仅在 iOS 5 中可用。因此,如果您的目标是较旧版本的 iOS,则必须改用 self.parentViewController(为每个 iOS 版本使用适当的,您必须处理此问题)。

    如果您在父/呈现视图控制器中进行此控件,则只需调用它:

    [self dismissModalViewControllerAnimated:YES];
    

    【讨论】:

    • 从其他人告诉我的情况来看,使用委托是首选方法,所以我正在尝试这样做。不过它也有帮助,因为我想将数据从模态控制器传递回主控制器。
    • 这两种方法在 iOS 6.0 中都被弃用了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 2015-12-03
    相关资源
    最近更新 更多