开发者

Not calling the function when I click on UIButton?

I have an imageView and after i add it to the screen I release it. Before that I adds a button onto it with this code.

            button = [[UIButton buttonWithT开发者_运维知识库ype:UIButtonTypeCustom] retain];
            button.frame = CGRectMake(128.00, 128.00, 23.00, 40.00);
            [button setBackgroundImage:[UIImage imageNamed:@"move.png"] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(showAddress:) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:button];

            - (void) showAddress:(id)sender
            {
                NSLog(@"Working");
            }

The button appears but nothing happens when I click on it to call a function. Not even the pressing-unpressing. Is this because I am releasing the imageView?


Edited:Check if userInteractionEnabled property of your UIImageView is set to YES - it is set to NO by default and that's why UIImageView (and its subviews) does not respond to touch events. I've just set it to YES in my code and it works fine.

One more note - when you add your button to another view it takes the ownership of the button and as you retained it in your code you should also release it. (Or just not retain in this case)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜