与UIButton相关的问题

时间:2010-04-24 13:12:35

标签: iphone

我在UIButton的代码中使用了customview。

UIButton *button=  [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; 
[button addTarget:self action:@selector(checkedimage:)  forControlEvents:UIControlEventTouchUpInside];

-(IBAction)checkedimage:(id)sender
{
NSLog(@"checkedimage");

if(checkImage == NO)
{
    newImage = [UIImage imageNamed:@"a.png"];
    checkImage=YES;
}
else if(checkImage==YES)
{
    newImage = [UIImage imageNamed:@"b.png"];
    checkImage=NO;
}
}

但是当我点击UIButton时,它不会动作checkimage
为什么呢?

2 个答案:

答案 0 :(得分:1)

您没有正确设置,并没有给我们足够的信息来帮助您。

您的代码中的任何位置是否创建了IBOutlet,其类型为UIButton且名称为button?如果您已经完成了,那么您在IB中将IBOutlet分配给正确的UIButton? 如果你已经这样做了,你真的不应该在代码中声明一个具有相同名称的新UIButton。它应该已经作为您正在使用的当前类中的属性存在。

如果您还没有完成上述任何操作,则需要将新创建的按钮添加到某个地方,以便它可以显示。

我建议你阅读一些关于如何使用IB的基本教程。

答案 1 :(得分:0)

您发布的代码没有任何问题。

您是否已将此按钮添加到视图中?使用界面构建器?

我根据你发送的内容运行了这个测试代码,一切正常:

- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button= [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[button addTarget:self action:@selector(checkedimage:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(100, 100, 120, 20);
[self.view addSubview:button];
}

 -(IBAction)checkedimage:(id)sender { 
 NSLog(@"checkedimage");
}