0

別のボタンがクリックされたときに 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 から使用されていない「自己を返す」ことに関係していると思いますか?

4

1 に答える 1

2

はい、そうです。クラス clustersがあるため、実際には の戻り値をinitインスタンスに割り当てる必要があります。

customButton *newButton = [[customButton alloc] initWithFrame:f];

また、慣習的にクラス名は大文字で始まります。クラスに名前を付けますCustomButton

編集:別の問題は、initWithFrame:メソッドでは、プロパティを設定するのではselfなく、別のインスタンス ( button) に設定することです。明らかに、それは の状態には影響しませんself

于 2013-05-21T04:48:38.657 に答える