【问题标题】:Bug when trying to create view with 2 UIButtons尝试使用 2 个 UIButton 创建视图时出现错误
【发布时间】:2011-04-15 20:18:51
【问题描述】:

我正在尝试使用两个 UIButton 创建一个视图。代码编译没有任何错误,但按钮没有任何标签,并且无法单击。

-(id)initWithTabBar {

if ([self init]) {
    self.title = @"Tab1";
    self.tabBarItem.image = [UIImage imageNamed:@"promoters.png"];
    self.navigationItem.title = @"Nav 1";
}

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(facebookAuthorize) forControlEvents:UIControlEventTouchDown];
[button setFrame:CGRectMake(10, 10, 100, 100)];

[self.view addSubview:button];

[button release];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setTitle:@"Button" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(facebookLogin) forControlEvents:UIControlEventTouchDown];
[button2 setFrame:CGRectMake(110, 10, 100, 100)];

[self.view addSubview:button2];

[button2 release];

return self;
}

【问题讨论】:

    标签: objective-c cocoa-touch ios uibutton


    【解决方案1】:

    不要松开这些按钮。它们被创建为自动释放的对象。如documentation中所述:

    你只释放或自动释放 你拥有的物品。您拥有所有权 一个对象,如果你使用 名称以“alloc”开头的方法, “新”、“复制”或“可变复制”

    这些词都没有出现在 buttonWithType: 中,因此您无需承担任何责任,您可以放心地假设它是自动释放的。

    【讨论】:

    • 哦,我明白了,那么 buttonWithType: 是一个处理内存分配的静态类方法吗?
    【解决方案2】:

    如前所述,带有withType的按钮是不能命名的。使文字出现在uibutton中的代码是:

      UIButton *someButton=[[[UIButton alloc]initWithFrame:CGRectMake(140, 6, 175, 36)]autorelease];
    [someButton addTarget:self action:@selector(facebookLogin) forControlEvents:UIControlEventTouchDown];
    [someButton setTitle:@"Button" forState:UIControlStateNormal];
    [self.view addSubview:someButton];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多