How to add a UIButton at runtime

前端 未结 5 1494
予麋鹿
予麋鹿 2020-12-31 05:23

I am trying to add a UIButton at runtime however it is not visible. What am I doing wrong?

- (id)initWithFrame:(CGRect)frame {
    if (self = [s         


        
5条回答
  •  攒了一身酷
    2020-12-31 05:48

    You must release btn and remove ":" in buttonClick:

    UIButton *btn= [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    btn.frame = CGRectMake(0, 0, 100, 25);
    btn.backgroundColor = [UIColor clearColor];
    [btn setTitle:@"Play" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn]; 
    [btn release];
    

提交回复
热议问题