[英]How to add custom UIbutton into UIAlertView?
I have a question about customize UIAlertView
我对自定义UIAlertView
有疑问
I am customize a UIAlertView
from even the background and button. 我甚至从背景和按钮自定义UIAlertView
。 I add the custom button into it, however I can't trigger the action of the button. 我在其中添加了自定义按钮,但是无法触发按钮的操作。 The custom buttons are added into UIAlertView
, but how can't I do add action to them? 自定义按钮已添加到UIAlertView
,但是如何为它们添加动作呢? Please help me out. 请帮帮我。 Is it because UIAlertView
can't let us have custom buttons? 是因为UIAlertView
不能让我们拥有自定义按钮吗?
UIButton *firstButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 120, 90, 30)];
UIButton *secondButton = [[UIButton alloc] initWithFrame:CGRectMake(180, 120, 90, 30)];
UIImage *btnImage = [UIImage imageNamed:kButtonBlackImage];
[firstButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[firstButton setTitle:@"Don't Favorite" forState:UIControlStateNormal];
firstButton.titleLabel.font = BOLDFONTSMALL;
[firstButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[firstButton addTarget:self action:@selector(cancelFavorite:) forControlEvents:UIControlEventTouchUpInside];
[secondButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[secondButton setTitle:@"Save Favorite" forState:UIControlStateNormal];
[secondButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
secondButton.titleLabel.font = BOLDFONTSMALL;
[secondButton addTarget:nil action:@selector(addFavorite:) forControlEvents:UIControlEventTouchUpInside];
[alert addSubview:firstButton];
[alert addSubview:secondButton];
[firstButton addTarget:self action:@selector(cancelFavorite:) forControlEvents:UIControlEventTouchUpInside];
Add that buttons properly
You can use custom view for UIAlertView
. 您可以将自定义视图用于UIAlertView
。 If there is a problem with your secondButton
its a problem maybe because you set the target as nil
, it should be self
如果您的secondButton
存在问题,则可能是因为您将目标设置为nil
,所以它应该是self
[secondButton addTarget:self action:@selector(addFavorite:) forControlEvents:UIControlEventTouchUpInside];
Just tried and got it working with : 刚刚尝试过并使其与:
UIAlertView*testAlert=[[UIAlertView alloc] initWithFrame:CGRectMake(0, 0, 140, 140)];
[testAlert show];
UIButton*testButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[testButton setFrame:CGRectMake(0, 0, 40, 40)];
[testButton addTarget:self action:@selector(someAction) forControlEvents:UIControlEventTouchUpInside];
[testAlert addSubview:testButton];
I'm able to use my someAction
method. 我可以使用我的someAction
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.