所以,
问题
在我的 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:
- Attached touch up inside action for button which is triggering segue.
- Placed popup displaying into touch up inside action
- Placed segue logic into section of popup that corresponds "Yes" selection
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 } }
方法
shouldPerformSegueWithIdentifier
现在总是返回NO
。
这很好用,但是,我仍然想知道上面方案中描述的方式是否可以实现,所以,问题是:
问题
- 有没有办法以某种方式暂停方法的执行,以便等待用户操作?
- 可能还有另一种更好的方法来实现所需的行为吗?欢迎任何关于为什么这是好/坏的评论。
最佳答案
我不明白点击按钮后立即调用 segue 相关操作的动机(除了它都可以通过 Storyboard配置的事实)。我要做的是通过点击按钮触发警报,然后根据用户输入执行到下一个 Controller 的转换。此外,使用基于 block 的 UIAlertView 会更优雅(许多都可以在线获取)。
祝你好运!
关于ios - IOS 中带有确认对话框的 Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21181610/