我正在创建一个自定义委托UIAlertView的alert:buttonClickedAtIndex:
方法,但它无法正常工作。我正在为UIView创建子类,我有两个标记为0
和1
的按钮。当我去检查自定义视图的委托时,这仍然不起作用。这是我做的代码。
自定义视图
- (void) buttonTouchedWithIdentifier:(NSInteger)identifier
{
if (identifier == 0) {
[self.delegate alert:self didClickButtonWithTagIdentifier:0];
}
if (identifier == 1) {
[self.delegate alert:self didClickButtonWithTagIdentifier:1];
}
}
* in my showInViewMethod *
[self.dismissButton addTarget:self action:@selector(buttonTouchedWithIdentifier:) forControlEvents:UIControlEventTouchDown];
[self.continueButton addTarget:self action:@selector(buttonTouchedWithIdentifier:) forControlEvents:UIControlEventTouchDown];
self.dismissButton.tag = 0;
self.continueButton.tag = 1;
* in my view controller *
nextLevelAlert = [[ARAlert alloc] init];
nextLevelAlert.delegate = self;
[nextLevelAlert showInView:self.view
withMessage:[NSString stringWithFormat:@"Congratulations, you have completed level %i.\nWould you like to continue?", levelNumber]
dismissButtonTitle:@"Menu"
continueButtonTitle:@"Next Level"];
- (void)alert:(ARAlert *)alert didClickButtonWithTagIdentifier:(NSInteger)tagId
{
if (alert == nextLevelAlert) {
if (tagId == 0) {
NSLog(@"User does not want to continue.");
}
}
}
现在,nextLevelAlert将委托设置为self,并且我在视图控制器的类中声明了委托。此外,当我执行showInView ... for nextLevelAlert时,它会出现,它正在识别正在按下的按钮。
答案 0 :(得分:2)
我的猜测是你的param不是NSInteger而是按钮,你应该像这样更改buttonTouchedWithIdentifier
:
- (void) buttonTouchedWithIdentifier:(id)sender
{
UIButton *button = (UIButton*)sender;
NSLog(@"buttonTouchedWithIdentifier %@",@(button.tag));
if (button.tag == 0) {
[self.delegate alert:self didClickButtonWithTagIdentifier:0];
}
if (button.tag == 1) {
[self.delegate alert:self didClickButtonWithTagIdentifier:1];
}
}
同样,在比较两个对象时,请使用isEqual:
代替==
- (void)alert:(ARAlert *)alert didClickButtonWithTagIdentifier:(NSInteger)tagId
{
if ([alert isEqual:nextLevelAlert]) {
if (tagId == 0) {
NSLog(@"User does not want to continue.");
}
}
}