1

我有一个设置keyboard为其输入视图的类

NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:@"FormulaKeyboard" owner:self options:nil];

for (id object in bundle) {
    if ([object isKindOfClass:[FormulaKeyboard class]])
        keyboard = (FormulaKeyboard *)object;
}   

在键盘类(NSObject 的子类)的 init 方法中,我尝试设置使用 nib 创建的几个 UIButton 的标题文本。

- (id)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {

    NSArray *keyboardElements =  [delegate provideKeyElements];
    [element1 setTitle:[keyboardElements objectAtIndex:0]  forState: UIControlStateNormal];
    [element2 setTitle:[keyboardElements objectAtIndex:1]  forState: UIControlStateNormal]; 
    [element3 setTitle:[keyboardElements objectAtIndex:2]  forState: UIControlStateNormal]; 
    [element4 setTitle:[keyboardElements objectAtIndex:3]  forState: UIControlStateNormal]; 
    [element5 setTitle:[keyboardElements objectAtIndex:4]  forState: UIControlStateNormal]; 

    }
    return self;
}

但是,按钮不会收到消息。当我将代码实现到另一个方法中时,setTitle: 方法工作正常,但我希望在创建键盘后立即调用这些方法。我已经使用 NSLog 验证了调用了 initWithCoder: 方法。我也尝试将代码放入 awakeFromNib 方法中,但这也不起作用。

4

0 回答 0