代码之家  ›  专栏  ›  技术社区  ›  Austin Berenyi

如何为XIB中的UITextField调用委托方法?

  •  1
  • Austin Berenyi  · 技术社区  · 6 年前

    我有一个XIB ProfileView 包含在自定义中 UIViewController Alarm . 配置文件视图 具有子视图 UITextView ,我想打电话 UITextView 从自定义类委派方法 惊恐 而不是 配置文件视图 因为我所有关于 UITextView 在我的 惊恐 班所以我的问题是:我能打电话吗 UITextView 从my委派方法 惊恐 如果 UITextView 是的子视图 配置文件视图 ?换句话说,我可以设置 UITextView 委托给 惊恐 而不是 self ?

    enter image description here

    2 回复  |  直到 6 年前
        1
  •  2
  •   trungduc 邱林和    6 年前
    • 连接并生成 UITextView 作为公共财产 ProfileView

      @interface ProfileView : UIView
      @property (weak, nonatomic) IBOutlet UITextView *textView;
      @end
      
    • 在你的 Alarm 类别,集合 self.profileView.textView.delegate = self 并实现您需要的功能 profileView 不是的属性 惊恐 ,在初始化委托后设置委托。

      @interface Alert : UIViewController <UITextViewDelegate>
      
      @property (nonatomic, strong) ProfileView* profileView;
      
      @end
      
      // In |viewDidLoad| or anywhere after you initialize |_profileView|
      self.profileView.textView.delegate = self;
      
        2
  •  2
  •   yesleon    6 年前

    是的,你可以。

    按代码输入 Alarm :

    func viewDidLoad() {
        super.viewDidLoad()
    
        // If you have the UITextField @IBOutlet in your Alarm
        textField.delegate = self
    
        // If it's owned by ProfileView
        profileView.textField.delegate = self
    }
    

    确定 惊恐 符合 UITextFieldDelegate .


    或者使用Interface Builder,首先从 UITextField 惊恐 对象如下:

    然后选择 代表 .

    enter image description here

    推荐文章