我有一个关于自定义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
方法。