lncnb91
lncnb91

Reputation: 69

UIButton not appear

I am new to objective-c.
Can anyone tell me why the button doesn't appear when I run the code.

Here is my code:

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    button1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect]initWithFrame:CGRectMake(3, 3, 30, 30)];
    [button1 setTitle:@"haha" forState:UIControlStateNormal];
    [button1 setBackgroundColor: [UIColor blackColor]];
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:button1];
}    

Upvotes: 0

Views: 93

Answers (1)

iPatel
iPatel

Reputation: 47049

Add [self.view addSubview:button1]; line in initWithNibName before return self;

other wise

- (void)viewDidLoad
{
    [super viewDidLoad];
    button1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect]initWithFrame:CGRectMake(3, 3, 30, 30)];
    [button1 setTitle:@"haha" forState:UIControlStateNormal];
    [button1 setBackgroundColor: [UIColor blackColor]];
    [self.view addSubview:button1]; 
} 

EDIT:

Check, are you set @property and @synthesize, properly ???

Upvotes: 4

Related Questions