Oma*_*mar 8 uialertview ios uistoryboard segue uistoryboardsegue
我创建了UIAlertView一个动作,它给了我两个选项.我希望用户能够点击按钮并让它执行Segue.
这是我到目前为止的代码:
- (IBAction)switchView:(id)sender
{
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:@"Please Note"
message:@"Hello this is my message"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:@"Option 1", @"Option 2", nil];
[myAlert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"Option 1"]) {
}
}
Run Code Online (Sandbox Code Playgroud)
是的,一开始并不是很明显,你需要创建一个手动的segue.

选择将执行推送的ViewController(我是推动者),并将手动连接到推送的视图控制器(推送视图控制器).

选择新创建的segue,并为其命名(在我的例子中"segue.push.alert",为日志记录的长名称),并在警报的操作中调用perform segue,如:
let alert = UIAlertController(title: "My Alert", message: "Be Alerted. This will trigger a segue.", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Segue", style: .Default, handler:
{
[unowned self] (action) -> Void in
self.performSegueWithIdentifier("segue.push.alert", sender: self)
}))
presentViewController(alert)
Run Code Online (Sandbox Code Playgroud)
[unowned self]应该小心处理,如果视图控制器可以在动作发生时解除分配,那么你最好[weak self]再做,然后再做self?.performSegue...分配.
现在,performSegueWithIdentifier:sender:在您的情况下,您可以从视图控制器中轻松调用
// Using enums is entirely optional, it just keeps the code neat.
enum AlertButtonIndex : NSInteger
{
AlertButtonAccept,
AlertButtonCancel
};
// The callback method for an alertView
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)index
{
if (index == AlertButtonAccept)
{
[self performSegueWithIdentifier:@"segue.push.alert" sender:self];
}
}
Run Code Online (Sandbox Code Playgroud)
以这种方式获得segues(而不是直接编码)的优点是,您仍然可以对应用程序流有一个很好的概述,具有混合编码段和加载故事板的segues有点失败的目的.
如果你在故事板中给你的 segue 一个标识符,你可以这样做:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"Option 1"]) {
[self performSegueWithIdentifier:@"foo" sender:nil];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5929 次 |
| 最近记录: |