Ios UIAlertview未调用正确的函数
我有一个UIAlertview,其中包含我试图用来调用函数的标记。显示fogotpassword警报视图,然后显示reset alertview,但当我尝试调用NSLog(@“密码”)时;通过按下reset alertview中的第一个按钮,它不会被调用。相反,重置alertview按钮会再次弹出。我会感激你给我的任何帮助Ios UIAlertview未调用正确的函数,ios,objective-c,uialertview,Ios,Objective C,Uialertview,我有一个UIAlertview,其中包含我试图用来调用函数的标记。显示fogotpassword警报视图,然后显示reset alertview,但当我尝试调用NSLog(@“密码”)时;通过按下reset alertview中的第一个按钮,它不会被调用。相反,重置alertview按钮会再次弹出。我会感激你给我的任何帮助 -(void)viewDidLoad{ forgotPassword = [[UIAlertView alloc] initWithTitle:@"Login Err
-(void)viewDidLoad{
forgotPassword = [[UIAlertView alloc] initWithTitle:@"Login Error"
message:@"Your login credentials do not match"
delegate:self
cancelButtonTitle:@"Try Again"
otherButtonTitles: @"Forgot Password",nil];
[forgotPassword show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
forgotPassword.tag = 1;
resetPassword.tag = 2;
if (buttonIndex == 1 && forgotPassword.tag ==1)
{
resetPassword = [[UIAlertView alloc] initWithTitle: @"Forgot Password"
message:@"Email" delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Reset Password", nil];
[resetPassword setAlertViewStyle:UIAlertViewStylePlainTextInput];
[resetPassword show];
NSLog(@"RESET");
}
if (buttonIndex == 1 && resetPassword.tag == 2) {
NSLog(@"Password");
}
}
不需要使用标签
这样试试
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView==forgotPassword)
NSLog(@"forgotPassword alert");
}
不需要使用标签
这样试试
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView==forgotPassword)
NSLog(@"forgotPassword alert");
}
你的逻辑全乱了。您设置了两个标记,因此这两个标记都将始终为真。由于两个不同的警报视图似乎都有IVAR,请去掉标记并执行以下操作:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView == forgotPassword) {
if (buttonIndex == alertView.firstOtherButtonIndex) {
// show other alert
}
else if (alertView == resetPassword) {
if (buttonIndex == alertView.firstOtherButtonIndex) {
// reset
}
}
}
你的逻辑全乱了。您设置了两个标记,因此这两个标记都将始终为真。由于两个不同的警报视图似乎都有IVAR,请去掉标记并执行以下操作:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView == forgotPassword) {
if (buttonIndex == alertView.firstOtherButtonIndex) {
// show other alert
}
else if (alertView == resetPassword) {
if (buttonIndex == alertView.firstOtherButtonIndex) {
// reset
}
}
}
你的代码很混乱。您创建一个警报视图“forgotPassword”并显示它,而不设置它的标记。然后,在alertView:ClickedButtonIndex:method中,显式地在两个警报视图上设置标记,然后询问这些警报视图以查看它们的标记。这些标签永远不会改变 相反,您应该在显示每个警报视图之前在其上设置标记,然后检查传入alertView:ClickedButtonIndex:方法的UIAlertView的标记。大概是这样的:
-(void)viewDidLoad{
forgotPassword = [[UIAlertView alloc] initWithTitle:@"Login Error"
message:@"Your login credentials do not match"
delegate:self
cancelButtonTitle:@"Try Again"
otherButtonTitles: @"Forgot Password",nil];
forgotPassword.tag = 1;
[forgotPassword show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1 && alertView.tag ==1)
{
resetPassword = [[UIAlertView alloc] initWithTitle: @"Forgot Password"
message:@"Email" delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Reset Password", nil];
[resetPassword setAlertViewStyle:UIAlertViewStylePlainTextInput];
resetPassword.tag = 2;
[resetPassword show];
NSLog(@"RESET");
}
if (buttonIndex == 1 && alertView.tag == 2) {
NSLog(@"Password");
}
}您的代码很混乱。您创建一个警报视图“forgotPassword”并显示它,而不设置它的标记。然后,在alertView:ClickedButtonIndex:method中,显式地在两个警报视图上设置标记,然后询问这些警报视图以查看它们的标记。这些标签永远不会改变 相反,您应该在显示每个警报视图之前在其上设置标记,然后检查传入alertView:ClickedButtonIndex:方法的UIAlertView的标记。大概是这样的:
-(void)viewDidLoad{
forgotPassword = [[UIAlertView alloc] initWithTitle:@"Login Error"
message:@"Your login credentials do not match"
delegate:self
cancelButtonTitle:@"Try Again"
otherButtonTitles: @"Forgot Password",nil];
forgotPassword.tag = 1;
[forgotPassword show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1 && alertView.tag ==1)
{
resetPassword = [[UIAlertView alloc] initWithTitle: @"Forgot Password"
message:@"Email" delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Reset Password", nil];
[resetPassword setAlertViewStyle:UIAlertViewStylePlainTextInput];
resetPassword.tag = 2;
[resetPassword show];
NSLog(@"RESET");
}
if (buttonIndex == 1 && alertView.tag == 2) {
NSLog(@"Password");
}
}您应该在方法中使用alertView.tag==1和alertView.tag==2。正如您现在看到的,只要buttonIndex==1,这两个if语句都将始终为真,因为您显式地设置了forgotPassword.tag=1和resetPassword.tag=2,然后检查每个项 此外,您应该在viewDidLoad中设置标记,除非您有理由在每次按下alertView按钮时继续重置它们的值 或者,一个更简单的方法就是阿南德·K所说的
-Stephen您应该在方法中使用alertView.tag==1和alertView.tag==2。正如您现在看到的,只要buttonIndex==1,这两个if语句都将始终为真,因为您显式地设置了forgotPassword.tag=1和resetPassword.tag=2,然后检查每个项 此外,您应该在viewDidLoad中设置标记,除非您有理由在每次按下alertView按钮时继续重置它们的值 或者,一个更简单的方法就是阿南德·K所说的
-Stephen您的代码是多余的,在任何情况下都没有意义。。 在这种情况下,不需要将UIAlertView用作属性。。 使用以下命令:
-(void)viewDidLoad{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Login Error"
message:@"Your login credentials do not match"
delegate:self
cancelButtonTitle:@"Try Again"
otherButtonTitles: @"Forgot Password",nil];
alertView.tag = 1;
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
if(alertView.tag == 1)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: @"Forgot Password"
message:@"Email" delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Reset Password", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
alertView.tag = 2;
[alertView show];
NSLog(@"RESET");
}
} else if (alertView.tag == 2) {
NSLog(@"Password");
}
}
此代码是干净的(请使用它;) 您的代码是多余的,在任何情况下都没有意义。。 在这种情况下,不需要将UIAlertView用作属性。。 使用以下命令:
-(void)viewDidLoad{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Login Error"
message:@"Your login credentials do not match"
delegate:self
cancelButtonTitle:@"Try Again"
otherButtonTitles: @"Forgot Password",nil];
alertView.tag = 1;
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
if(alertView.tag == 1)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: @"Forgot Password"
message:@"Email" delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Reset Password", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
alertView.tag = 2;
[alertView show];
NSLog(@"RESET");
}
} else if (alertView.tag == 2) {
NSLog(@"Password");
}
}
此代码是干净的(请使用它;) 谢谢,我会在期限一到就给你打勾。我只是遵循另一个stackoverflow的答案,我想这不是最好的方法。请记住,在警报视图中使用标记而不是IVAR也很好。您只需要在创建警报视图时设置标记,而不是在委派方法中。谢谢,一旦时间限制到期,将立即给您一个复选标记。我只是遵循另一个stackoverflow的答案,我想这不是最好的方法。请记住,在警报视图中使用标记而不是IVAR也很好。您只需要在创建警报视图时设置标记,而不是在委托方法中。为什么要复制Duncan的答案?我没有复制它。我的代码更干净。我不使用UIAlertView作为属性…而且在Duncun的响应中有2次“buttonIndex==1”…我的代码更有效..你有48k分..也许你可以很容易地看出重要的区别..你为什么要复制Duncan的答案?我没有复制它..我的代码更干净..我不使用UIAlertView作为属性..而且Duncun的响应中有2次“buttonIndex==1”…我的代码效率更高..你有48k分..也许你可以很容易地找出重要的区别。。