2016-05-13 5 views
0

に送信されるメッセージ:[UITextFieldの保持]:私はこのメッセージを取得割り当て解除インスタンス

[UITextField retain]: message sent to deallocated instance.

は、私がメッセージを理解するが、私は「メッセージ」が送信されたかわからないとどのように起きてからそれを停止することを... 。

次の作品私は、コントローラを却下する前に遅らせるために持っている理由「罰金」私はわからないながら

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

このコードは、エラーを生成します10

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) 


     } 

      } 

EDIT:(ほぼ完全なコード):locationIsSelected =真

@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

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

おそらくこれを再現するサンプルprojを作成できますか?ギフトボックスにアップロードして – Shubhank

答えて

1

この問題は、dismissViewControllerAnimatedを呼び出したために発生します。このVCが保持するすべてのオブジェクトのVCライフサイクルのために、割り当てが解除されます。つまり、却下後のUIでの操作はすべてメモリーセーフではありません。ドキュメントから

endEditingが徹底的階層を探していた間、私が推測するよう

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を呼び出す必要がありますか?単にこの呼び出しなしでVCを終了するだけです。あなたがロジックを持っているなら、それはendEditingに依存します - それを分離し、endEditingの代わりに呼び出します。お役に立てれば。

UPDATE:

はviewWillDisappearでendEditingを呼び出すようにしてください - これは却下される直前にファーストレスポンダを辞任するビューをトリガーします。

+0

私はそうしないとクラッシュするので、私はendEditingを呼び出します...私は、resignFirstResponderへのビューにgestureRecognizerとしてそれを追加してendEditingを呼び出すと、アプリケーションは正常に動作します。だから私はそれを追加してもクラッシュしないだろうと思った。しかし、もし私が物事を「遅らせる」のでなければクラッシュする。(意味があるといいが) –

+0

@GeorgeAsda、呼び出し終了?もう1つ - viewWillDisappearまたはviewDidDisAppearのどこかでendEditingを呼び出そうとしています – Doro

+0

あなたは私のヒーローです... viewWillDisappearでendEditingを呼び出しました! –

関連する問題