ios - IOS 中带有确认对话框的 Segue

标签 ios objective-c segue

所以,

问题

在我的 Storyboard中,我有一个可导致某些 View 的转场(实际上,这是一个向后转场)。同时,segue 操作正在执行一些不可逆的事情(例如某些项目删除),因此我需要添加确认对话框。添加并不难:

UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:[lang getLocalizedString:reg.language stringAtIndex:@"303"]];
[alert setMessage:[lang getLocalizedString:reg.language stringAtIndex:@"304"]];
[alert setDelegate:self];
[alert addButtonWithTitle:[lang getLocalizedString:reg.language stringAtIndex:@"305"]];
[alert addButtonWithTitle:[lang getLocalizedString:reg.language stringAtIndex:@"306"]];
[alert show];

(此处 [lang getLocalizedString: ..] 只是本地化字符串)。

但问题是:我不能使用标准的shouldPerformSegueWithIdentifier方法。所需的逻辑应该是:

+-------+     +-------+     +------------------------------------+
|  User |     |Trigger|     |Decision about segue will be made at|
|pressed| --> | segue | --> | shouldPerformSegueWithIdentifier   |
| button|     | stuff |     | so trigger alert show inside it    |
+-------+     +-------+     +------------------------------------+
                                              |
                                              V
                                      +---------------+
                                      |  Show alert,  |
                                      | Yes/No buttons|
                                      +---------------+
                                         /         \
+-------+      +---------------+        /           \        +---------------+
|Finally|      |Segue action   |     +---+         +---+     | All canceled  |
|perform|  <-- |will be made in| <-- |Yes|         | No| --> |just do nothing|
| segue |      |prepareForSegue|     +---+         +---+     |  i.e. return  |
+-------+      +---------------+                             +---------------+

I hope it's clear. But here's where tricky part is: obviously, we can't "make a decision" in shouldPerformSegueWithIdentifier based on user selection, since it will be executed asynchronously. So dialog will be shown, but method will continue it's execution, so then return YES or NO independently of dialog. There's a part of where's logic is breaking.

My approach

I was playing with it and trying different things (even went to sleep until some external pointer will be set to some value derived from user selection on dialog popup - with no success, of course). So now I can't implement logic that is in scheme above. Instead I've achieved desired behavior with following things:

  1. Attached touch up inside action for button which is triggering segue.
  2. Placed popup displaying into touch up inside action
  3. Placed segue logic into section of popup that corresponds "Yes" selection
  4. Placed programmatically launching of segue after logic. It's like:

        -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
        {
            if (buttonIndex == 0)
            {
                //"Yes" button pressed
                //...
                //Here is omitted part of segue logic
                //...
                [self performSegueWithIdentifier: @"deleteUnwindSegue" sender: self];
            }
            else if (buttonIndex == 1)
            {
                //"No" button pressed, nothing to do
            }
        }
    
  5. 方法shouldPerformSegueWithIdentifier现在总是返回NO

这很好用,但是,我仍然想知道上面方案中描述的方式是否可以实现,所以,问题是:

问题

  • 有没有办法以某种方式暂停方法的执行,以便等待用户操作?
  • 可能还有另一种更好的方法来实现所需的行为吗?欢迎任何关于为什么这是好/坏的评论。

最佳答案

我不明白点击按钮后立即调用 segue 相关操作的动机(除了它都可以通过 Storyboard配置的事实)。我要做的是通过点击按钮触发警报,然后根据用户输入执行到下一个 Controller 的转换。此外,使用基于 block 的 UIAlertView 会更优雅(许多都可以在线获取)。

祝你好运!

关于ios - IOS 中带有确认对话框的 Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21181610/

相关文章:

ios - 从 Swift 中的非静态方法访问静态变量

iphone - 大型自定义 UIView - CABackingStoreUpdate 性能

iphone - UIViewController 的高度设置为 460

iOS 多行标题栏

ios - Swift:如何在不添加 segue 的情况下切换回以前的 UITableViewController

ios - 使用 monogame 构建默认 iOS 解决方案时出现错误消息

ios - 为什么 Xcode 模拟器 View 与 View 层次结构 View 不同

ios - Swift 中的条件转场

iOS Swift Unwind segue 从 Detail 到 Master 以启动 segue 到 Detail2 无法正常工作

iphone - 防止在 iOS 中备份文件