gpt4 book ai didi

ios - Swift 中的 ActionSheet 操作

转载 作者:行者123 更新时间:2023-11-29 02:36:26 25 4
gpt4 key购买 nike

我对 swift 和 IOS 开发总体来说是新手,所以我在 Xcode 中制作了一个操作表,但现在我想从操作表中执行操作。我熟悉在 ObjC 中使用按钮 Index 执行此操作,但我似乎无法在 swift 中弄清楚这一点。

我还想自定义我的操作表以使文本具有不同的颜色,但这并不那么重要。如果你能帮忙,请告诉我。谢谢

这是到目前为止我的代码,但按下按钮时操作的 .tag 部分是错误的......

    @IBAction func ActionSheet(sender: UIButton) {

var sheet: UIActionSheet = UIActionSheet();
let title: String = "Action Sheet!";
sheet.title = title;
sheet.addButtonWithTitle("Cancel");
sheet.addButtonWithTitle("A course");
sheet.addButtonWithTitle("B course");
sheet.addButtonWithTitle("C course");
sheet.cancelButtonIndex = 0;
sheet.showInView(self.view);

}


func actionSheet(sheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int) {
if (actionSheet.tag == 1) {
NameLabel.text = "I am confused"

}
}

最佳答案

你需要:

  1. 使您的 View Controller 符合 UIActionSheetDelegate 协议(protocol):

    ViewController 类:UIViewController、UIActionSheetDelegate {

  2. 添加self作为委托(delegate),并将标签设置为1:

    var sheet: UIActionSheet = UIActionSheet()
    let title: String = "Action Sheet!"
    sheet.title = title
    sheet.addButtonWithTitle("Cancel")
    sheet.addButtonWithTitle("A course")
    sheet.addButtonWithTitle("B course")
    sheet.addButtonWithTitle("C course")
    sheet.cancelButtonIndex = 0
    sheet.delegate = self // new line here
    sheet.tag = 1 // another new line here
    sheet.showInView(self.view)
  3. 现在您可以使用buttonIndex:


func actionSheet(sheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int) {
if sheet.tag == 1 {
println(buttonIndex)
println(sheet.buttonTitleAtIndex(buttonIndex))
}
}

关于ios - Swift 中的 ActionSheet 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26318359/

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