2016-09-01 5 views
0

я пытаюсь создать функцию, используя uialertcontroller с текстовым полем, используя расширение uialertcontrollerИОС Swift 2: расширение - Оповещение с текстовым полем

это мой код:

extension UIAlertController{ 

class func AlertWithTextField(here: String, message1 : String) -> UIAlertController{ 

    var alertController:UIAlertController? 
    alertController = UIAlertController(title: here, 
     message: message1, 
     preferredStyle: .Alert) 

    alertController!.addTextFieldWithConfigurationHandler(
     {(textField: UITextField!) in 
      textField.placeholder = "Ex: 1" 
      textField.textAlignment = .Center 
      textField.delegate = self 
      textField.keyboardType = UIKeyboardType.NumberPad 
    }) 
    let action = UIAlertAction(title: "Submit", 
     style: UIAlertActionStyle.Default, 
     handler: {[weak self] 
      (paramAction:UIAlertAction!) in 
      if let textFields = alertController?.textFields{ 
       let theTextFields = textFields as! [UITextField] 
       let enteredText = theTextFields[0].text 
       print("\n\(enteredText)") } 
     }) 
    let action2 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) 
    alertController?.addAction(action) 
    alertController?.addAction(action2) 

}} 

нормально, у меня есть проблемы с слова «я», и я не могу найти решение для них, что может быть решением этой проблемы?

+0

Я уже задал этот вопрос. Вы можете найти его здесь: http://stackoverflow.com/questions/26567413/get-input-value-from-textfield-in-ios-alert-in-swift. :) – ntoonio

ответ

0

Для вашей первой проблемы самостоятельно, я хотел бы предложить вам сделать что-то вроде этого

class func AlertWithTextField(here: String, message1 : String, delegate:UITextFieldDelegate?) -> UIAlertController{ 

    var alertController:UIAlertController? 
    alertController = UIAlertController(title: here, 
             message: message1, 
             preferredStyle: .Alert) 

    alertController!.addTextFieldWithConfigurationHandler(
     {(textField: UITextField!) in 
      textField.placeholder = "Ex: 1" 
      textField.textAlignment = .Center 
      textField.delegate = delegate 
      textField.keyboardType = UIKeyboardType.NumberPad 
    }) 
    let action = UIAlertAction(title: "Submit", 
           style: UIAlertActionStyle.Default, 
           handler: {(paramAction:UIAlertAction!)->Void in 
           if let textFields = alertController?.textFields { 
            let theTextFields = textFields 
            let enteredText = theTextFields[0].text 
            print("\n\(enteredText)") } 
     }) 
    let action2 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) 
    alertController?.addAction(action) 
    alertController?.addAction(action2) 
    return alertController! 
} 

Вы можете принять объект UITextFieldDelegate в ваших статических методах, и назначить ей ти делегата и для второй задачи вы объявляющее слабым но не использовать его в закрытии, поэтому просто удалите это, и ваш код должен работать нормально.

 Смежные вопросы

  • Нет связанных вопросов^_^