別のボタンがクリックされたときに UI ボタンを作成する設定があります。独自の UI Button クラスを作成することを決定するまで、最初はすべてが簡単に思えました。ボタンが画面に表示されないという事実を除いて、ほとんどは機能します。何が原因であるかについてはすでにいくつかのアイデアがありますが、コードを別の目で見てもらうと役に立ちます。
customButton.m
- (id)initWithFrame:(CGRect)frame //this function was provided
{
self = [super initWithFrame:frame];
if (self) {
//this is stuff I wrote, that could be wrong.
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(1,2,100,50);
[button setTitle:@"Test!" forState:UIControlStateNormal];
}
return self;
NSLog(@"worked?"); //this log is actually outputted! However, if I put this before "return self", it doesn't work. So I'm guessing the problem is with "return self" not being caught.
}
ビュー.m
-(IBAction)createDrawer:(id)sender {
customButton* newButton = [customButton alloc];
[newButton initWithFrame:newButton.frame];
[self.containerView addSubview:newButton];
NSLog(@"asdasdadads"); //this actually gets executed, when button is clicked
}
view.m の 3 行目で、「式の結果が未使用です」という警告が表示されます。これは、initWithFrame から使用されていない「自己を返す」ことに関係していると思いますか?