2015-01-22 4 views
0

AlertView에서 데이터를 수집하고 싶습니다. AlertController의 기능AlertView with textbox, 입력 된 텍스트 상자에서 데이터를 수집하는 방법

@IBAction func showAlertTapped(sender: AnyObject) { 
     //Create the AlertController 
     let actionSheetController: UIAlertController = UIAlertController(title: "Add User", message: "Enter username", preferredStyle: .Alert) 

     //Create and add the Cancel action 
     let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in 
      //Do some stuff 
     } 
     actionSheetController.addAction(cancelAction) 
     //Create and an option action 
     let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in 
      //Do some other stuff 
     } 
     actionSheetController.addAction(saveAction) 
     //Add a text field 
     actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in 
      //TextField configuration 
      textField.textColor = UIColor.blueColor() 
     } 

     //Present the AlertController 
     self.presentViewController(actionSheetController, animated: true, completion: nil) 
    } 

임 모르고, 그래서 예에 entred 텍스트를 가지고 어디 몰라 : 내가 www가에서 발견 한 것은 이것이다 간단한 변수? UIAlertController가 textFields 속성이 같은

+0

참고가 유형을 정의하고 있기 때문에 또한'... 그것은'하자 saveAction = UIAlertAction (즉 추론 할 수 – Jawwad

+0

을 두지 않을보다 간결한 코드를 만들 수 있습니다 '.ActionSheet' 스타일이 아닌'.Alert' 스타일을 사용하고 있습니다. 여러분의 변수의 이름을'actionSheetController'가 아닌 다른 것으로 바꿀 수도 있습니다. – Jawwad

답변

0

외모와 당신은 단지 하나 개의 텍스트 필드가 있기 때문에 당신은 당신은 로컬 변수에 할당하여 텍스트 필드의 문자열에 액세스 할 수 있습니다

let textField = actionSheetController.textFields?.first as UITextField 
0

다음과 같은 방법으로 액세스 할 수 있어야합니다 구성 핸들러에서.

//Create a local variable 
    var alertTextField:UITextField? 

    //Create the AlertController 
    let actionSheetController: UIAlertController = UIAlertController(title: "Add User", message: "Enter username", preferredStyle: .Alert) 

    //Create and add the Cancel action 
    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in 
     //Do some stuff 
    } 
    actionSheetController.addAction(cancelAction) 

    //Create and add save action 
    let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in 
     //Unwrap your local variable and access your textfield 
     if let textField = alertTextField { 
      println("\(textField.text)") 
     } 
    } 
    actionSheetController.addAction(saveAction) 
    //Add a text field 

    actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in 
     //TextField configuration 
     textField.textColor = UIColor.blueColor() 

     //Assign your UIAlertController textField to your local variable 
     alertTextField = textField 

    } 

    //Present the AlertController 
    self.presentViewController(actionSheetController, animated: true, completion: nil) 
0

스위프트 3 버전 :

//Create a local variable 
    var alertTextField:UITextField? 

    //Create the AlertController 
    let actionSheetController: UIAlertController = UIAlertController(title: "Add User", message: "Enter username", preferredStyle: .alert) 

    //Create and add the Cancel action 
    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in 
     //Do some stuff 
    } 
    actionSheetController.addAction(cancelAction) 

    //Create and add save action 
    let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .default) { action -> Void in 
     //Unwrap your local variable and access your textfield 
     if let textField = alertTextField { 
      print(textField.text!) 
     } 
    } 
    actionSheetController.addAction(saveAction) 
    //Add a text field 

    actionSheetController.addTextField { textField -> Void in 
     //TextField configuration 
     textField.textColor = UIColor.blue 

     //Assign your UIAlertController textField to your local variable 
     alertTextField = textField 

    } 

    //Present the AlertController 
    self.present(actionSheetController, animated: true, completion: nil) 
관련 문제