2014-06-17 44 views
7

我想向「重試」按鈕添加一個動作,但是當用戶按下「重試」按鈕時,什麼都不會發生。如何在Swift中添加動作到UIAlertView(iOS 7)

var createAccountErrorAlert: UIAlertView = UIAlertView() 

createAccountErrorAlert.delegate = self     

createAccountErrorAlert.title = "Oops" 
createAccountErrorAlert.message = "Could not create account!" 
createAccountErrorAlert.addButtonWithTitle("Dismiss") 
createAccountErrorAlert.addButtonWithTitle("Retry") 

createAccountErrorAlert.show() 

按下按鈕索引的功能?

func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){ 

    switch buttonIndex{ 

    case 1: 
     createAccount() 

    default: 
     //Some code here.. 

    } 
} 
+0

你如何試圖調試呢?你有沒有在函數內部放置一個斷點來判斷它是否被觸發?你有沒有把日誌聲明看看'buttonIndex'是什麼?這些是我需要做的第一件事情來複制你遇到的錯誤。 – wjl

回答

22

我測試你的代碼,它工作正常,我將打印相應的結果:

func showAlert(){ 
    var createAccountErrorAlert: UIAlertView = UIAlertView() 

    createAccountErrorAlert.delegate = self 

    createAccountErrorAlert.title = "Oops" 
    createAccountErrorAlert.message = "Could not create account!" 
    createAccountErrorAlert.addButtonWithTitle("Dismiss") 
    createAccountErrorAlert.addButtonWithTitle("Retry") 

    createAccountErrorAlert.show() 
} 

func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){ 

    switch buttonIndex{ 

    case 1: 
     NSLog("Retry"); 
    break; 
    case 0: 
     NSLog("Dismiss"); 
     break; 
    default: 
     NSLog("Default"); 
     break; 
     //Some code here.. 

    } 
} 

它打印關閉時,我點擊解僱按鈕。

+0

對於那些可能有問題的人:如果使用「var createAccountErrorAlert = UIAlertView()」,則不起作用 – kommradHomer

0

那是因爲你的重試按鈕的指數是1,不爲0的釋放按鈕的索引爲0

如果你把下面的代碼在操場上,你會真正看到的索引按鈕(如addButtonWithTitle方法中記錄的):

var createAccountErrorAlert: UIAlertView = UIAlertView() 
createAccountErrorAlert.title = "Oops" 
createAccountErrorAlert.message = "Could not create account!" 
createAccountErrorAlert.addButtonWithTitle("Dismiss") //Prints 0 
createAccountErrorAlert.addButtonWithTitle("Retry") //Prints 1 
+0

它不適用於索引1. –

+0

它會在我嘗試您的代碼時執行。你還可以在你的createAccount()方法中發送代碼嗎?或者試試@wjl建議的內容,在委託方法中放置一個斷點,然後檢查索引值。或者在代理方法中放置一個「println(buttonIndex)」,並在點擊重試按鈕時查看控制檯上打印的內容。 –

0

索引錯誤。以下(基於iOS單一視圖默認應用程序模板)適用於我(我連接了一個按鈕以觸發@IBAction buttonPressed)。不要忘記,讓您的視圖控制器UIAlertViewDelegate(雖然沒有這樣做了以下工作):

import UIKit 

class ViewController: UIViewController, UIAlertViewDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func buttonPressed(sender : AnyObject) { 
     var createAccountErrorAlert: UIAlertView = UIAlertView() 

     createAccountErrorAlert.delegate = self 

     createAccountErrorAlert.title = "Oops" 
     createAccountErrorAlert.message = "Could not create account!" 
     createAccountErrorAlert.addButtonWithTitle("Dismiss") 
     createAccountErrorAlert.addButtonWithTitle("Retry") 

     createAccountErrorAlert.show() 
    } 

    func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) { 

     switch buttonIndex { 

     default: 
      println("alertView \(buttonIndex) clicked") 

     } 
    } 
} 
0

繼承人的方式我做到了,用更短的代碼行:

func showAlert(){ 
    var alert = UIAlertView(title: "Choose", message: "Which_pill", delegate: self, cancelButtonTitle:"Red") 
    alert.addButtonWithTitle("Blue") 
    alert.show() 
} 

func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){ 
    switch buttonIndex{ 
    case 1: println("Blue") 
    case 0: println("Red") 
    default: println("Is this part even possible?") 
    } 
} 
相關問題