添加UIButton作为子视图,但它不显示



我的按钮不会显示。有人知道为什么吗?

#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];
}

我试过"id"

- (id) hellothere: (UIViewController*) ss

有退货但仍然没有

#import <UIKit/UIKit.h>
#import "CButton.h"
@interface CTrial : //UIView
-(void) menuSetup:(UIButton*) btn;
- (id) hellothere: (UIViewController*) ss;
//- (void) hellothere: (UIViewController*) ss;
@end

使用

UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

如果你想显示自定义按钮,通常使用UIButtonTypeCustom,例如显示一个图像而不是经典按钮,但由于你没有为按钮分配任何图像,它只是透明的,这就是你看不到它的原因。

除了使用

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

alloc只是分配内存,不初始化对象

最新更新