Search code examples
iosobjective-cxibuitextviewdelegate

How do I call delegate methods for a UITextField within a XIB?


I have a XIB ProfileView contained within a custom UIViewController class Alarm. ProfileView has a subview UITextView, and I want to call UITextView delegate methods from my custom class Alarm instead of ProfileView because all of my logic concerning the text of the UITextView is in my Alarm class. So my question is: Can I call UITextView delegate methods from my Alarm class if the UITextView is a subview of ProfileView? In other words, can I set the UITextView delegate to Alarm instead of self?

enter image description here


Solution

    • Connect and make UITextView as a public property of ProfileView

      @interface ProfileView : UIView
      @property (weak, nonatomic) IBOutlet UITextView *textView;
      @end
      
    • In your Alarm class, set self.profileView.textView.delegate = self and implement functions you need or if profileView isn't a property of Alarm, set delegate after you initialize it.

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