簡體   English   中英

為 UIAlertAction 編寫處理程序

[英]Writing handler for UIAlertAction

我正在向用戶展示UIAlertView ,但我不知道如何編寫處理程序。 這是我的嘗試:

let alert = UIAlertController(title: "Title",
                            message: "Message",
                     preferredStyle: UIAlertControllerStyle.Alert)

alert.addAction(UIAlertAction(title: "Okay",
                              style: UIAlertActionStyle.Default,
                            handler: {self in println("Foo")})

我在 Xcode 中遇到了很多問題。

文檔說convenience init(title title: String!, style style: UIAlertActionStyle, handler handler: ((UIAlertAction!) -> Void)!)

此刻,整個街區/閉包都讓我有點不知所措。 任何建議都非常感謝。

而不是 self 在您的處理程序中,放置(警報:UIAlertAction!)。 這應該使您的代碼看起來像這樣

    alert.addAction(UIAlertAction(title: "Okay",
                          style: UIAlertActionStyle.Default,
                        handler: {(alert: UIAlertAction!) in println("Foo")}))

這是在 Swift 中定義處理程序的正確方法。

正如布萊恩在下面指出的,還有更簡單的方法來定義這些處理程序。 書中討論了使用他的方法,請查看標題為 Closures 的部分

函數是 Swift 中的一等對象。 因此,如果您不想使用閉包,您也可以只定義一個具有適當簽名的函數,然后將其作為handler參數傳遞。 觀察:

func someHandler(alert: UIAlertAction!) {
    // Do something...
}

alert.addAction(UIAlertAction(title: "Okay",
                              style: UIAlertActionStyle.Default,
                              handler: someHandler))

您可以使用 swift 2 像這樣簡單地做到這一點:

let alertController = UIAlertController(title: "iOScreator", message:
        "Hello, world!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Destructive,handler: { action in
        self.pressed()
}))

func pressed()
{
    print("you pressed")
}

    **or**


let alertController = UIAlertController(title: "iOScreator", message:
        "Hello, world!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Destructive,handler: { action in
      print("pressed")
 }))

以上所有答案都是正確的,我只是展示了另一種可以完成的方法。

讓我們假設您想要一個帶有主標題、兩個操作(保存和丟棄)和取消按鈕的 UIAlertAction:

let actionSheetController = UIAlertController (title: "My Action Title", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet)

    //Add Cancel-Action
    actionSheetController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))

    //Add Save-Action
    actionSheetController.addAction(UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: { (actionSheetController) -> Void in
        print("handle Save action...")
    }))

    //Add Discard-Action
    actionSheetController.addAction(UIAlertAction(title: "Discard", style: UIAlertActionStyle.Default, handler: { (actionSheetController) -> Void in
        print("handle Discard action ...")
    }))

    //present actionSheetController
    presentViewController(actionSheetController, animated: true, completion: nil)

這適用於 swift 2(Xcode 版本 7.0 beta 3)

在 Swift 4 中:

let alert=UIAlertController(title:"someAlert", message: "someMessage", preferredStyle:UIAlertControllerStyle.alert )

alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: {
        _ in print("FOO ")
}))

present(alert, animated: true, completion: nil)

swift 3.0 中的語法更改

alert.addAction(UIAlertAction(title: "Okay",
                style: .default,
                handler: { _ in print("Foo") } ))

這就是我如何使用 xcode 7.3.1

// create function
func sayhi(){
  print("hello")
}

// 創建按鈕

let sayinghi = UIAlertAction(title: "More", style: UIAlertActionStyle.Default, handler:  { action in
            self.sayhi()})

// 將按鈕添加到警報控件

myAlert.addAction(sayhi);

// 整個代碼,這段代碼會添加2個按鈕

  @IBAction func sayhi(sender: AnyObject) {
        let myAlert = UIAlertController(title: "Alert", message:"sup", preferredStyle: UIAlertControllerStyle.Alert);
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:nil)

        let sayhi = UIAlertAction(title: "say hi", style: UIAlertActionStyle.Default, handler:  { action in
            self.sayhi()})

        // this action can add to more button
        myAlert.addAction(okAction);
        myAlert.addAction(sayhi);

        self.presentViewController(myAlert, animated: true, completion: nil)
    }

    func sayhi(){
        // move to tabbarcontroller
     print("hello")
    }

創建警報,在 xcode 9 中測試

let alert = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: self.finishAlert))
self.present(alert, animated: true, completion: nil)

和功能

func finishAlert(alert: UIAlertAction!)
{
}
  1. 在斯威夫特

    let alertController = UIAlertController(title:"Title", message: "Message", preferredStyle:.alert) let Action = UIAlertAction.init(title: "Ok", style: .default) { (UIAlertAction) in // Write Your code Here } alertController.addAction(Action) self.present(alertController, animated: true, completion: nil)
  2. 在目標 C

     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *OK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [alertController addAction:OK]; [self presentViewController:alertController animated:YES completion:nil];

在 Swift 5 中,您還可以執行以下操作:

let handler = { (action: UIAlertAction!) -> Void in
    //do tasks
}

然后,當您進行操作時,只需將其替換為如下所示:

let action = UIAlertAction(title: "Do it", style: .default, handler: handler)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM