繁体   English   中英

如何将自定义UIbutton添加到UIAlertView?

[英]How to add custom UIbutton into UIAlertView?

我对自定义UIAlertView有疑问

我甚至从背景和按钮自定义UIAlertView 我在其中添加了自定义按钮,但是无法触发按钮的操作。 自定义按钮已添加到UIAlertView ,但是如何为它们添加动作呢? 请帮帮我。 是因为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 

您可以将自定义视图用于UIAlertView 如果您的secondButton存在问题,则可能是因为您将目标设置为nil ,所以它应该是self

 [secondButton addTarget:self action:@selector(addFavorite:) forControlEvents:UIControlEventTouchUpInside];

刚刚尝试过并使其与:

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

我可以使用我的someAction方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM