2016-11-30 119 views
0

我正在用UITextfield呈現警報,但其委託方法沒有被調用。我可能會做什麼錯誤。我正在使用下面的代碼來顯示帶有文本字段的警報。不調用UITextField委託方法

func takePasscodeToEnableTouch(){ 
    self.passcodeInputOperationType = .EnableTouchID 

    alertControllerPassCodeEntry = UIAlertController(title: "", message: "Enter Passcode to enable the Touch Id.", preferredStyle: UIAlertControllerStyle.Alert) 

    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (action) -> Void in 
    } 
    alertControllerPassCodeEntry!.addAction(cancelAction) 

    alertControllerPassCodeEntry!.addTextFieldWithConfigurationHandler { (txtField) -> Void in 
     txtField.placeholder = "Enter passcode" 
     txtField.delegate = self 
     txtField.tag = TextFieldTag.EnterPassCode 
     txtField.keyboardType = UIKeyboardType.NumbersAndPunctuation 
     txtField.accessibilityIdentifier = "PassCode" 
     txtField.secureTextEntry = true 
     txtField.addTarget(self, action:"textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged) 
    } 

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
    appDelegate.window?.rootViewController?.presentViewController(alertControllerPassCodeEntry!, animated: true, completion: nil) 
} 

而且文本框的委託方法:

func textFieldShouldBeginEditing(textField: UITextField) -> Bool 
{ 
    return true 
} 

func textFieldDidBeginEditing(textField: UITextField) // became first responder 
{ 

} 

func textFieldShouldEndEditing(textField: UITextField) -> Bool 
{ 
    return true 
} 

func textFieldDidEndEditing(textField: UITextField) 
{ 

} 

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
{ 
    var isLimitExist: Bool 
    var accessIndentifier: String 
    if let str = textField.accessibilityIdentifier 
    { 
     accessIndentifier = str 
    } 
    else 
    { 
     accessIndentifier = "" 
    } 

    //checkFieldLimit function is used to check the limit of text and restrict 
    isLimitExist = UIUtils.checkFieldLimit(accessIndentifier, stringToMatch: textField.text!, rangeLength: range.length, stringLength: string.characters.count) 

    if !isLimitExist 
    { 
     return false 
    } 
    return true 
} 
+0

文本字段委託方法在哪裏?使用未被調用的委託方法更新您的問題。 – rmaddy

+1

爲什麼你的代碼中包含所有'!'? – rmaddy

+0

我保留警報控制器的參考。我做了選擇。 –

回答

1

好了,從評論的信息,現在一切都似乎很清楚。總括來說,你調用顯示這樣的警報的方法:

@IBAction func swtchTouchAction(sender: UISwitch) { 
    if sender.on { 
     let passCodeManager = PasscodeManager() 
     passCodeManager.delegate = self 
     passCodeManager.takePasscodeToEnableTouch() 
    } else { 
     let passCodeManager = PasscodeManager() 
     passCodeManager.delegate = self 
     passCodeManager.authenticatePasscodeToDisalbeTouch() 
    } 
} 

現在,你不保留(意思是 - 分配到一個強大的屬性)的passCodeManager在這裏的任何地方。這意味着,在該方法結束時,該對象被破壞(感謝ARC - Automatic Reference Counting)。有人可能會認爲它會被保留,因爲您將其指定爲文本字段的delegate,但代表的時間爲99.99%,這意味着它們不會撞到分配給它們的對象的保留計數。

解決您的眼前問題,你應該在你的類中,你必須swtchTouchAction方法的屬性,並更改你這樣的代碼:

var passCodeManager: PasscodeManager? 

@IBAction func swtchTouchAction(sender: UISwitch) { 
    if sender.on { 
     self.passCodeManager = PasscodeManager() 
     self.passCodeManager?.delegate = self 
     self.passCodeManager?.takePasscodeToEnableTouch() 
    } else { 
     self.passCodeManager = PasscodeManager() 
     self.passCodeManager?.delegate = self 
     self.passCodeManager?.authenticatePasscodeToDisalbeTouch() 
    } 
} 

這將足以保留您的密碼管理器。我建議你閱讀memory management is done in Swift

+0

非常感謝你Losiowaty。 –