开发者

A question related to UIButton

开发者 https://www.devze.com 2022-12-28 14:12 出处:网络
I have taken customview in my code for UIButton. UIButton *button=[[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

I have taken customview in my code for UIButton.

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

but when i开发者_如何转开发 click on UIButton it is not going to action checkedimage

why?


You have not set it up correctly and not really given us enough information to help you.

Have you anywhere in your code created an IBOutlet with the type of UIButton and name of button? If you have done that have you in IB assign that IBOutlet to the correct UIButton? If you have done that as well you really should not declare a NEW UIButton in the code with the same name. It should already exist as a property in the current class you are working in.

If you have not done any of the above you need to ADD your newly created button to a view somewhere for it to be visible at all.

I advise you to read some basic tutorials on how to use IB.


There is nothing wrong with the code you posted.

Have you added this button to the view? Using interface builder?

I ran this test code based on what you sent, all works just fine:

- (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");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号