jdl
jdl

Reputation: 6323

Adding a UIButton as a subview, but it does not display.

My button won't display. Does anyone know why?

#import "ViewController.h"


@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];


    CTrial *tt = [CTrial alloc];
    [tt hellothere:self]; 
}


/////////////////////////////
#import "CTrial.h"
@implementation CTrial


- (void) hellothere: (UIViewController*) ss
{
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn2 setTag:200];
    [btn2 addTarget:self action:@selector(menuSetup:) forControlEvents:UIControlEventTouchUpInside];
    [btn2 setFrame:CGRectMake(0,0,300,200)];
    [ss.view addSubview:btn2];
}

I have tried "id"

- (id) hellothere: (UIViewController*) ss

with the return but still nothing

#import <UIKit/UIKit.h>
#import "CButton.h"

@interface CTrial : //UIView

-(void) menuSetup:(UIButton*) btn;
- (id) hellothere: (UIViewController*) ss;
//- (void) hellothere: (UIViewController*) ss;
@end

Upvotes: 0

Views: 152

Answers (1)

Manlio
Manlio

Reputation: 10865

Use

UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

You usually use UIButtonTypeCustom if you want to display a custom button, for example displaying an image instead of the classical button, but since you are not assigning any image to the button it's simply transparent, that is why you cannot see it.

Besides use

CTrial *tt = [[CTrial alloc] init];

alloc simply allocate the memory, does not initialize your object

Upvotes: 1

Related Questions