开发者

iPhone 3.1 SDK: UIViewController category is affecting ALL ViewControllers

开发者 https://www.devze.com 2022-12-31 07:25 出处:网络
iPhone SDK question for you. I\'ve created a UIViewController category to shift a view up when UITextFields are being edited and the keyboard appears, blocking the fields. I found the code online and

iPhone SDK question for you.

I've created a UIViewController category to shift a view up when UITextFields are being edited and the keyboard appears, blocking the fields. I found the code online and no doubt you iPhone gurus are familiar with it. The interface looks like this:

@interface UIViewController (Shiftable) 

      - (IBAction) textFieldDidBeginEditing:(UITextField *)textField;

      - (IBAction) textFieldDidEndEditing:(UITextField *)textField;

      - (void) animateTextField: (UITextField *)textField up:(BOOL)up;

@end

My problem is this-- every one of my UIViewControllers is affected by this category! I thought this would only affect UIViewControllers that import the category as follows:

 #import "UIViewController Shiftable.h"

But even UIViewControllers that do not import this category have their views shifted up when UITextFields are edited, and of course some of my views do not need to be shifted when the keyboard appears as the keyboard does not hi开发者_运维百科de the fields.

Is my understanding of categories incorrect? Can anyone shed any light on this?

Thanks.


No, that's the way categories work - if you add some methods to a class then all those changes will be available for all instances of that class during runtime.


The important point is that in the file where you #import "UIViewController Shiftable.h", all instances of UIViewController will have the new methods. If you don't import your category file, none of your UIViewControllers will.

If you need some view controllers to have these new/modified methods but other view controllers not, consider subclassing UIViewController.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号