在UITextField或UITextView开始编辑时收到通知

时间:2015-09-27 13:06:48

标签: ios swift uikit

我正在构建自定义UIView,它接受输入字段VOID InitializeTitleSpecificHooks(PLDR_DATA_TABLE_ENTRY ModuleHandle) UITextField的初始化数组。我需要在输入字段开始编辑时通知此自定义视图,而不设置代理

  1. UITextView中,我通过添加UITextField属性的观察者提出了一个想法。你有其他想法吗?
  2. 我在editing找不到editing所以我应该怎么做。
  3. 我的想法是,我需要将此自定义视图作为独立视图,让用户可以在UITextView

    中自由设置代理

3 个答案:

答案 0 :(得分:2)

另一种方法是观察use App\UserMailer as Mailer ; class TestController extends Controller { protected $mailer; public function __construct(Mailer $mailer) { $this->mailer; } public function testMailer() { $this->mailer->welcome()->sendTo(1); return 'done'; } 的通知UITextViewUITextViewTextDidBeginEditingNotification的通知UITextField

答案 1 :(得分:0)

这是一种方法,可以允许向此自定义UIView发送通知,而无需将其设置为委托:

import UIKit


class SelfAwareTextField : UITextField, UITextFieldDelegate {

//
// Other delegate for this textfield
//
var otherDelegate: UITextFieldDelegate? = nil;

//
// Set weak reference to parent UIView
//
weak var parentView: UIView!


//
// Set the initializer
//
init(parentView pv: UIView, frame: CGRect) {

    parentView = pv;
    super.init(frame: frame);

    //
    // Do some custom initialization...
    //

    // Make this textfield a delegate to itself
    self.delegate = self;
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setDelegate( delegate _otherDelegate: UITextFieldDelegate ){
    otherDelegate = _otherDelegate;
}



//
// Write the textFieldDidBeginEditing method such that it
// also calls the otherDelegate in the event it exists
// and has this method defined
//
func textFieldDidBeginEditing(_ textField: UITextField){
    //
    // Do stuff based on this textfield knowing it is starting to be used
    //
    // ....
    //


    //
    // Do stuff for parent UIView
    // ...
    // (parentView as! CustomView).notified() // or something like this
    //


    //
    // Do stuff for the other delegate
    //
    otherDelegate?.textFieldDidBeginEditing?(textField);
}


// Do similar thing as above for other methods in UITextFieldDelegate protocol

}

我们的想法是,您将此文本字段作为自己的委托,以便知道何时调用每个委托方法。然后在每次调用这些委托方法时,您可以使用自定义UIView和您希望与此文本字段关联的任何其他委托执行某些操作。

答案 2 :(得分:-1)

我提出了一个解决方案,我使用UIKeyboardWillShowNotification在键盘即将显示时接收通知,然后遍历我拥有的字段并检查isFirstResponder()

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeShown:", name: UIKeyboardWillShowNotification, object: nil)

然后:

@objc private func keyboardWillBeShown(notification:NSNotification){

    for field in fields{

        if field.isFirstResponder(){

            // handle begin editing here
        }
    }

}