2016-05-13 75 views
0

我得到這個消息的消息:[保留的UITextField]:發送到釋放實例

[UITextField retain]: message sent to deallocated instance.

我瞭解的消息,但我不知道什麼是「消息」被髮送以及如何阻止它發生...... 。

此代碼產生錯誤:

func dismissController() { 
     self.view.endEditing(true) 
      self.dismissViewControllerAnimated(false, completion: nil) 
      } 

雖然下面的作品「精」我不知道爲什麼我有駁回控制器之前延遲:

func dismissController() { 
     self.view.endEditing(true) 

     let delay = 0.75 * Double(NSEC_PER_SEC) 
     let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay)) 
     dispatch_after(time, dispatch_get_main_queue()) {() -> Void in 
      self.dismissViewControllerAnimated(false, completion: nil) 


     } 

      } 

編輯:(幾乎全碼):

@IBOutlet weak var locationTextView: UITextField! 
    @IBOutlet weak var userIDTextView: UITextField! 
    var treeLocationArray = NSMutableArray() 
    var treeUserIDArray = NSMutableArray() 

讓pickerView = UIPickerView() VAR pickerButton =的UIButton() VAR keyboardButton =的UIButton() VAR locationIsSelected =真

override func viewDidLoad() { 
     super.viewDidLoad() 

//.... 

locationTextView.delegate = self 
     userIDTextView.delegate = self 
//Get the Plist... etc 

treeLocationArray = dict?.objectForKey("Location") as! NSMutableArray 
     treeUserIDArray = dict?.objectForKey("UserID") as! NSMutableArray 

locationTextView.text = userPrefs.objectForKey("DefaultLocation") as? String 
      userIDTextView.text = userPrefs.objectForKey("DefaultName") as? String 
      locationTextView.text = userPrefs.objectForKey("DefaultLocation") as? String 

    /// Create a view for the Accessory View 
     let customView = UIView(frame: CGRectMake(0, 0, 10, 50)) 
     customView.backgroundColor = UIColor.lightGrayColor() 


     /// Setup the picker button 
     pickerButton = UIButton(frame: CGRectMake(60 , 8, 32, 32)) 
     pickerButton.setImage(UIImage(named:"dropIcon"), forState: .Normal) 
     pickerButton.tintColor = UIColor.blueColor() 
     pickerButton.addTarget(self, action: #selector(UserDefaultsViewController.pickerTapped), forControlEvents: UIControlEvents.TouchUpInside) 
     customView.addSubview(pickerButton) 

     /// Setup the keyboard button 
     keyboardButton = UIButton(frame: CGRectMake(10 , 8, 32, 32)) 
     keyboardButton.setImage(UIImage(named:"keyboardIcon"), forState: .Normal) 
     keyboardButton.tintColor = UIColor.blueColor() 
     keyboardButton.addTarget(self, action: #selector(UserDefaultsViewController.keyboardTapped), forControlEvents: UIControlEvents.TouchUpInside) 
     customView.addSubview(keyboardButton) 

     locationTextView.inputAccessoryView = customView 
     userIDTextView.inputAccessoryView = customView 

} 

func textFieldDidBeginEditing(textField: UITextField) { 

     if textField == locationTextView { 
      locationIsSelected = true 


     } 

     if textField == userIDTextView { 
      locationIsSelected = false 

     } 


     self.pickerView.reloadAllComponents() 

    } 


    func keyboardTapped(){ 

     if locationIsSelected { 

      locationTextView.resignFirstResponder() 
      locationTextView.inputView = nil 
      locationTextView.becomeFirstResponder() 

     } 

     else { 

      userIDTextView.resignFirstResponder() 
      userIDTextView.inputView = nil 
      userIDTextView.becomeFirstResponder() 

     } 


    } 

    func pickerTapped(){ 

     if locationIsSelected { 

      locationTextView.resignFirstResponder() 
      locationTextView.inputView = nil 
      locationTextView.inputView = pickerView 
      locationTextView.becomeFirstResponder() 

     } 

     else { 

      userIDTextView.resignFirstResponder() 
      userIDTextView.inputView = nil 
      userIDTextView.inputView = pickerView 
      userIDTextView.becomeFirstResponder() 

     } 

    } 

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { 
     return 1 
    } 

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 

     var numberOfComponents = Int() 

     if locationIsSelected { 
      numberOfComponents = treeLocationArray.count 
     } 

     else { 
      numberOfComponents = treeUserIDArray.count 
     } 

     return numberOfComponents 
    } 

    func pickerView(pickerView: UIPickerView, numberOfRowsInSection section: Int) -> Int { 

     var numberOfComponents = Int() 

     if locationIsSelected { 
      numberOfComponents = treeLocationArray.count 
     } 

     else { 
      numberOfComponents = treeUserIDArray.count 
     } 

     return numberOfComponents 
    } 

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 

     var componetString = String() 

     if locationIsSelected { 
      componetString = (treeLocationArray[row] as? String)! 
     } 

     else { 
      componetString = (treeUserIDArray[row] as? String)! 
     } 

     return componetString 

    } 

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 

     if locationIsSelected { 

      if treeLocationArray.count >= 1 { 
      locationTextView.text = treeLocationArray[row] as? String 

      } 
     } 

     else { 

      if treeUserIDArray.count >= 1 { 

      userIDTextView.text = treeUserIDArray[row] as? String 

      } 
     } 

    } 
+0

您是否在其他地方使用延遲,以後訪問該文本字段變量? – Shubhank

+0

沒有其他地方......我只是因爲崩潰而在這裏添加了延遲 –

+0

你能創建一個樣本程序來重現這個嗎?並在github上查看 – Shubhank

回答

1

發生此問題是因爲您有調用dismissViewControllerAnimated。由於VC生命週期所有對象,這個VC持有將被釋放。這意味着,在解散之後所有使用UI的操作都不是內存安全的。

從文檔:

This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to true, the text field is never even asked; it is forced to resign.

所以,你的VC已經同時endEditing一直在尋找徹底的釋放層次,我猜。這是唯一的原因,可能會導致記憶問題。

爲什麼在解僱前需要致電endEditing?沒有這個電話就簡單地解僱VC。如果你有邏輯,這取決於endEditing - 將它分開並調用,而不是endEditing。希望這可以幫助。

UPDATE:

試圖調用endEditing在viewWillDisappear - 這會觸發觀點辭職第一響應者將被開除權利之前。

+0

我打電話endEditing,因爲它會崩潰,如果我不......如果我通過添加它作爲gestureRecognizer on resignFirstResponder視圖endEditing然後該應用程序工作正常。所以我想如果我在那裏添加它就不會崩潰。但是如果我不「延遲」事情,我仍然會崩潰(希望它有意義) –

+0

@GeorgeAsda,可否請更新錯誤的問題, t call endEdining?還有一個 - 嘗試調用endEditing的某處在viewWillDisappear或viewDidDisappear – Doro

+0

哦,男人......你是我的英雄......在視圖中調用endEditingWillDisappear做到了! –

相關問題