简体   繁体   中英

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 ?

在此处输入图片说明

  • 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; 

Yes you can.

By code in 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
}

Be sure Alarm is conformed to UITextFieldDelegate .


Or with Interface Builder, first drag from the UITextField to the Alarm object like this:

And then select delegate .

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM