2016-03-30 174 views
1

我在從UIAlertController獲取文本字段值時遇到問題。我得到的價值,但它似乎是以錯誤的順序執行我的代碼。在下面的函數中,我期望函數調用返回一個字符串,但該值在函數返回時未設置。從警報框中獲取文本值

@IBAction func btnSaveSession(sender: AnyObject) { 

    //Prompt user to enter session name 

    var sessionName: String = "" 

    sessionName = promptUserToEnterSessionName("Save Session", message: "Please enter the name of your custom session below."); 

    print("session Name: " + sessionName) 

    //Save session to firebase. 

    //redirect to create session controller. 
} 

func promptUserToEnterSessionName(title: String, message: String) -> String{ 

    var sessionName: String = "" 

    //1. Create the alert controller. 
    let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert) 

    //2. Add the text field. You can configure it however you need. 
    alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in 
     textField.text = "Enter session name." 
    }) 

    //3. Grab the value from the text field, and print it when the user clicks OK. 
    alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in 
     let textField = alert.textFields![0] as UITextField 
     print("Text field: \(textField.text)") 
     sessionName = textField.text! 
    })) 

    // 4. Present the alert. 
    self.presentViewController(alert, animated: true, completion: nil) 

    return sessionName 

} 

如果有人在理解我的問題時遇到問題,請發表評論,我會盡我所能解釋。

回答

4

當您點擊確定按鈕時,您的好動作處理程序(關閉)將會調用。所以不是給本地變量sessionName賦值,而是在類中創建一個sessionName變量。

class YourClass { 
var sessionName: String? 

... 

@IBAction func btnSaveSession(sender: AnyObject) { 

//Prompt user to enter session name 

promptUserToEnterSessionName("Save Session", message: "Please enter the name of your custom session below."); 

} 


func promptUserToEnterSessionName(title: String, message: String) { 


//1. Create the alert controller. 
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert) 

//2. Add the text field. You can configure it however you need. 
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in 
    textField.text = "Enter session name." 
}) 

//3. Grab the value from the text field, and print it when the user clicks OK. 
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in 
    let textField = alert.textFields![0] as UITextField 
    print("Text field: \(textField.text)") 
    self.sessionName = textField.text! 
    self.testDemo() 
})) 

// 4. Present the alert. 
self.presentViewController(alert, animated: true, completion: nil) 


} 

fun TestDemo() { 
print("session Name: " + self.sessionName) 

//Save session to firebase. 

//redirect to create session controller. 

} 
} 
+0

這仍然給我同樣的問題。在分配值之前,會打印sessionName。 – JunkJovis

+0

是的,它會在點擊之前打印會話值。你可以從addAction調用函數並在那裏做你的工作。 – Sahil

+0

或者你可以在testDemo中傳遞直接值,所以你不需要在類中創建var。例如:alert.addAction(UIAlertAction(title:「OK」,style:.Default,handler:{(action) - > let textField = alert.textFields![0] as UITextField print(「Text field:\ (的TextField.text)「) self.testDemo(的TextField.text) })) 或可在testDemo得到這個 FUNC testDemo(SESSIONNAME:字符串){您的幫助 } – Sahil

0

此代碼:

let textField = alert.textFields![0] as UITextField 
print("Text field: \(textField.text)") 
sessionName = textField.text! 

只會在對應於規定動作的按鈕被按下的瞬間被調用。因此,在創建UIAlertController時不會調用它。

閱讀關於Swift中的塊,它應該更清晰。

+0

那麼我該如何解決我的問題? @wujo – JunkJovis

1

您可以使用一個回調來獲取值,就像這樣:

func AlertPrompt(vista:UIViewController, titulo:String , mensaje:String, aceptar:String, completion: @escaping (_ resultado: String)->()) { 
     let miAlerta = UIAlertController(title: titulo, message: mensaje, preferredStyle: UIAlertControllerStyle.alert) 
     miAlerta.addTextField(configurationHandler: { 
      (textField) -> Void in 
     }) 
     let okBoton = UIAlertAction(title: aceptar, style: UIAlertActionStyle.default, handler: { 
      (action) -> Void in 
      let result = miAlerta.textFields![0] as UITextField 
      // You return the value here 
      completion(result.text! as String) 
     }) 
     miAlerta.addAction(okBoton) 
     let cancelBoton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil) 
     miAlerta.addAction(cancelBoton) 
     vista.present(miAlerta, animated: true, completion: nil) 
} 

而且這樣調用該函數:

AlertPrompt(vista: self, titulo: "Title", mensaje: "some message", aceptar: "Acept"){(resultado)->() in 
    print(resultado) 
} 

這對我的作品完美,希望這有助於