首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能让UITextFieldDelegate工作

不能让UITextFieldDelegate工作
EN

Stack Overflow用户
提问于 2013-12-13 13:03:59
回答 3查看 679关注 0票数 0

我编写了一个自定义视图CustomViewA : UIView<UITextFieldDelegate>并实现了委托的方法。

在CustomViewA的init中,我写道:

代码语言:javascript
复制
- (id)initWithFrame:(CGRect)frame {
    self = [[[NSBundle mainBundle] loadNibNamed:@"CustomViewA" owner:self options:nil] objectAtIndex:0];
    if (self) {
        //the txtfieldA is a IBOutlet property linked to the nib file.
        self.txtfieldA.delegate = self; //not work
    }
    return  self;
}

在这个xib文件中包含一个UITextField控件,当我运行它并编辑textfield时,我在init method.But中设置了它的委托,委托的方法是而不是 called.Can,有人告诉我为什么吗?我怎样才能修好它?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-12-13 13:55:57

要从xib文件加载一个UIView子类的视图,可以这样做:

代码语言:javascript
复制
// Reusable method to load a class which subclass UIView from a xib.
+ (id)loadNibNamed:(NSString *)nibName ofClass:(Class)objClass andOwner:(id)owner {
    if (nibName && objClass) {
        NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName owner:owner options:nil];

        for (id currentObject in objects ){
            if ([currentObject isKindOfClass:objClass])
                return currentObject;
        }
    }

    return nil;
}

然后在控制器中添加视图:

代码语言:javascript
复制
myCustomViewA = [self loadNibNamed:@"customviewA" ofClass:[CustomViewA class] andOwner:self];

myCustomViewA.delegate = self; // Or do this in the xib file
[self.view addSubview:myCustomViewA];
票数 0
EN

Stack Overflow用户

发布于 2013-12-13 13:21:18

确保UITextField被正确地连接到IBOutlet of CustomViewA。否则,尝试在init中设置委托将一无所获,

代码语言:javascript
复制
//if self.myTextField is nil, then this does nothing
self.myTextField.delegate = self;
票数 0
EN

Stack Overflow用户

发布于 2013-12-13 13:22:35

只要从xib文件中输入视图,就不要使用-(id)init。

并在- (void)awakeFromNib.中设置UITextField的委托方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20567251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档