gpt4 book ai didi

ios - 使按钮成为 UIAlertView 执行 Segue

转载 作者:可可西里 更新时间:2023-11-01 03:08:15 25 4
gpt4 key购买 nike

我为一个操作创建了一个 UIAlertView,它给了我 2 个选项。我希望用户能够单击按钮并让它执行 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"]) {



}
}

最佳答案

是的,一开始不是很明显,您需要创建一个手动转场。

enter image description here

选择将进行推送的 ViewController(我是推送者),然后将手册连接到推送的 View Controller (推送的 View Controller )。

enter image description here

iOS 8+ 和 Swift

选择新创建的 segue,并为其命名(在我的例子中是 "segue.push.alert",日志的长名称),并在警报,例如:

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)

[unowned self] 应该小心处理,如果 View Controller 可以在操作发生时释放,你最好使用 [weak self] 和然后执行 self?.performSegue... 如果可以发生释放。

旧答案

现在,在您的情况下,您可以从 View Controller 简单地调用 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];
}
}

以这种方式进行转场(而不是直接编码)的优势在于,您仍然可以很好地了解您的应用程序流程,混合编码的转场和 Storyboard加载的转场有点违背了目的。

关于ios - 使按钮成为 UIAlertView 执行 Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17730481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com