当有多个警报视图时检测按下的按钮
我在一个视图中有多个警报视图,我使用此代码来检测按下了哪个按钮:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:@"OK"]) {
//for one alert view
[passCode becomeFirstResponder];
} else if ([title isEqualToString:@" OK "]) {
//for another alert view, had to change "OK" to " OK "
[passCodeConfirm becomeFirstResponder];
}
}
现在,由于一个视图中有多个警报视图执行不同的操作,我必须欺骗用户认为“确定”和“确定” “是同一件事。它工作起来并且看起来不错,但感觉有点混乱。当然还有另一种方法可以做到这一点,例如使其特定于警报视图,然后使其特定于另一个警报视图。你知道我会怎么做吗?谢谢!
I have multiple alert views in one view, and I use this code to detect which button was pressed:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:@"OK"]) {
//for one alert view
[passCode becomeFirstResponder];
} else if ([title isEqualToString:@" OK "]) {
//for another alert view, had to change "OK" to " OK "
[passCodeConfirm becomeFirstResponder];
}
}
Now since there are multiple alert views in one view that do different things, I have to trick the user into thinking "OK" and " OK " are the same thing. It works and looks fine, but it feels kind of messy. Surely there is another way to do this, such as making this specific to an alert view, and then making it specific to another. Do you know how I would do this? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(5)
在您的视图中,为每个警报视图添加一个属性。
UIAlertView *myAlertType1;
UIAlertView *myAlertType2;
@property (nonatomic, retain) UIAlertView *myAlertType1;
@property (nonatomic, retain) UIAlertView *myAlertType2;
使用这些属性创建警报
self.myAlertType1 = [[[UIAlertView alloc] initWithTitle: ... etc] autorelease];
[self.myAlertType1 show];
然后在您的委托方法中:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView == myAlertType1) {
// check the button types and add behaviour for this type of alert
} else if (alertView == myAlertType2 {
// check the button types and add behaviour for the second type of alert
}
}
编辑:虽然上面的方法有效,但 iApple 使用标签的建议似乎更干净/更简单。
//in your .h file
UIAlertView* alert1;
UIAlertView* alert2;
//in your .m file
// when you are showing your alerts, use
[alert1 show]; //or
[alert2 show];
//and just check your alertview in the below method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView == alert1)
{
//check its buttons
}
else //check other alert's btns
}
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
为单独的 UIAlertView 设置唯一的 标签 并在其委托方法中识别和访问它会更具技术性,也更好。
例如,
It would be more technical as well better that set unique tag for separate UIAlertView and identify it and access in its delegate method.
For example,