【问题标题】:Can you stop a modal view from dismissing?你能阻止模态视图被解雇吗?
【发布时间】:2011-11-29 20:25:53
【问题描述】:

我有一个视图,它使用带有页面卷曲的模式视图来允许输入用户名。然后使用基于 Web 的服务验证此用户名,以查看其是否有效。

在您输入无效的用户名并在模式视图之外单击之前,一切都很好。这仍然会检查用户名,该用户名被报告为无效并打开UIAlertView。但是,它会返回到父视图。

在这种情况下,有什么方法可以让模态不关闭?

我试图重新加载视图,但要么它不工作,要么UIAlertView 阻止它。我的最后一个想法是将模式视图与无效用户名警报上的“确定”结合起来。有人有什么想法吗?

【问题讨论】:

  • 在模态视图上方显示警报应该没有问题。为什么您的模态视图在视图外点击时会消失?
  • 单击在关闭模式的父视图上的模式之外。解雇触发显示 UIAlert 的用户名检查。除非他们输入无效的用户名,否则一切都很好,然后模式关闭,警报显示,但我希望它重定向回模式视图。这看起来应该很简单,但我花了很多时间,但它对我不起作用。

标签: objective-c ios cocoa-touch modal-view


【解决方案1】:

如果您没有使用UINavigationController,您可以在调用模态视图的视图控制器中放置这样的内容:

-(void)dismissModalViewControllerAnimated:(BOOL)animated{
    if (_someFlagForBeingProperlyLoggedIn) [super dismissModalViewControllerAnimated:animated];
}

当您点击页面卷曲时,呈现/父视图控制器将发送dismissModalViewControllerAnimated:

由于您使用的是导航控制器,因此您的选择有限。这是因为UINavigationControllerUIViewController 的子类,并且是一个以自我为中心的子类。当您单击页面 curl 时,它正在调用 dismissModalViewControllerAnimated:。

您仍然可以选择继承UINavigationController 并实现上述方法,但这会很快变得混乱。

让 UIAlertView “直接返回”到模式登录视图非常容易。让该主视图符合UIAlertViewDelegate 协议。当您显示警报时将该实例设置为委托,并在该类中实现该方法:

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    // Enclose in if (buttonIndex == #) for selective calling
    UINavigationController* nav = (UINavigationController*)[[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"Preferences"];
    [nav setModalTransitionStyle:UIModalTransitionStylePartialCurl];
    [self.navigationController presentModalViewController:nav animated:YES]; 
}

然后,当警报视图被关闭时,它将显示“登录”视图。

【讨论】:

  • 我尝试使用简单的 NSLog 调用该函数,但在模态视图关闭时它甚至没有命中。我确实喜欢你设置一个标志的想法。这样,一旦模态视图关闭,我就可以在父级的 viewDidAppear 中检查该标志。仍然不确定为什么没有调用该函数。谢谢你的想法。
  • 当你点击页面卷曲时,我们称之为登录视图控制器,它在呈现/父视图控制器而不是登录视图控制器上调用dismissModalViewControllerAnimated:
  • 正确。我将它添加到呈现视图控制器,然后放在那里 NSLog(@"Dismissing Modal");它永远不会出现在调试器中。这是调用模态的代码。 UINavigationController* nav = (UINavigationController*)[[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"Preferences"]; [导航 setModalTransitionStyle:UIModalTransitionStylePartialCurl]; [self.navigationController presentModalViewController:nav animated:YES];
【解决方案2】:

您应该稍微延迟重新显示模态视图,大约 0.3-0.5。这是解除警报所需的时间,而这正是阻止模式视图显示的动画(解除警报视图)。

-(void)showModal{
    SomeModalViewClass* modalView = [[SomaModalViewClass alloc]init];
    [self setModalTransitionStyle:UIModalTransitionStylePartialCurl];
    [self presentModalViewController:modalView animated:YES];
    [modalView release];
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    //check the button index if needed and then
    [self performSelector:@selector(showModal) withObject:nil afterDelay:0.3]; 
}

【讨论】:

    猜你喜欢
    • 2023-01-18
    • 2012-02-25
    • 2011-06-08
    • 2017-12-02
    • 2017-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    相关资源
    最近更新 更多