在viewDidLoad部分通过编码添加两个按钮始终隐藏一个按钮

时间:2015-04-20 02:24:27

标签: ios objective-c

viewDidLoad部分通过编码添加两个按钮总是隐藏一个按钮,我不知道为什么会发生这种情况。

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //array initialization

    list=[[NSMutableArray alloc]init];

    heading=[[UILabel alloc]initWithFrame:CGRectMake(140.0f, 5.0f, 40.0f, 20.0f)];
    heading.text=@"Lists";
    heading.backgroundColor=[UIColor clearColor];

    [self.view addSubview:heading];

    UIImage *addButton=[UIImage imageNamed:@"add.png"];
    add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    add.frame = CGRectMake(190.0f, 6.0f, 20.0f, 20.0f);
    [add addTarget:self action:@selector(addList) forControlEvents:UIControlEventTouchUpInside];
    [add setImage:addButton forState:UIControlStateNormal];
    [self.view addSubview:add];

    UIImage *contact=[UIImage imageNamed:@"contacts.png"];
    UIButton *cntctBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    add.frame = CGRectMake(100.0f, 6.0f, 20.0f, 20.0f);
    [add addTarget:self action:@selector(shareContact) forControlEvents:UIControlEventTouchUpInside];
    [add setImage:contact forState:UIControlStateNormal];
    [self.view addSubview:cntctBtn];
}

2 个答案:

答案 0 :(得分:0)

这看起来像是一个复制和粘贴错误。你永远不会为第二个按钮设置框架,图像或目标动作,因为你有"添加"你应该在哪里" cntctBtn"。

答案 1 :(得分:0)

- (void)viewDidLoad {
    [super viewDidLoad];

    //array initialization

    list=[[NSMutableArray alloc]init];

    heading=[[UILabel alloc]initWithFrame:CGRectMake(140.0f, 5.0f, 40.0f, 20.0f)];
    heading.text=@"Lists";
    heading.backgroundColor=[UIColor clearColor];

    [self.view addSubview:heading];

    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    addButton.frame = CGRectMake(190.0f, 6.0f, 20.0f, 20.0f);
    [addButton addTarget:self action:@selector(addList) forControlEvents:UIControlEventTouchUpInside];
    [addButton setImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateNormal];
    [self.view addSubview: addButton];

    UIImage *contact=[UIImage imageNamed:@"contacts.png"];
    UIButton *cntctBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    cntctBtn.frame = CGRectMake(100.0f, 6.0f, 20.0f, 20.0f);
    [cntctBtn addTarget:self action:@selector(shareContact) forControlEvents:UIControlEventTouchUpInside];
    [cntctBtn setImage:contact forState:UIControlStateNormal];
    [self.view addSubview:cntctBtn];
}

它的复制粘贴错误。以上是更正后的代码。