2012-03-14 45 views
0

我有一个关于定制的问题UIAlertView如何将自定义UIbutton添加到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]; 
+0

什么是您的需要确切 – 2012-03-14 06:13:23

+0

它应该工作。您可以在这里添加您的alertview的屏幕截图以及您如何分配alertview? – Dee 2012-03-14 06:17:29

+0

不需要我猜测,这只是违反苹果的规则,您的应用程序很可能会被拒绝 – doNotCheckMyBlog 2012-03-14 06:41:24

回答

0
[firstButton addTarget:self action:@selector(cancelFavorite:) forControlEvents:UIControlEventTouchUpInside]; 

Add that buttons properly 
0

可以使用自定义视图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方法。

相关问题