繁体   English   中英

忘记密码:目标C

[英]Forgot Password code: Objective C

我正在构建一个iOS应用,我需要提供一个忘记密码功能。 我有一个数据库,用于存储注册用户及其详细信息(userArray)。 我已通过输入的电子邮件进行搜索,因此, 我该如何显示显示找到的密码的UIAlert? 下面是我尝试过的代码,但是如果条件错误。

[self.theuser valueForKey:@“ password”]是从数据库中检索到的密码及其字符串。

   - (IBAction)ForgotPassword:(id)sender {


    if ([[self.theuser valueForKey:@"password"] > 0 ]) {

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Success"
                                                       message: @"Your Password is:"
                                                      delegate: self
                                             cancelButtonTitle:@"Ok"
                                                        otherButtonTitles:nil];
        [alert show];
    }

您需要检查字符串的长度是否大于0,因为使用此命令:

 if ([self.theuser valueForKey:@"password"])

如果该值不是nil ,则将始终返回true ,这意味着如果服务器代码返回password的值为“” (两个引号之间没有空格),则该应用将识别出该值不是nil并显示警报中没有文字

因此,最终代码应如下所示:

- (IBAction)ForgotPassword:(id)sender {

    NSString *retrievedPassword = [self.theuser valueForKey:@"password"];

    if ( retrievedPassword.length > 0) {
        NSString *message = [NSString stringWithFormat:@"Your password is: %@", retrievedPassword];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Success"
                                                       message: message
                                                      delegate: self
                                             cancelButtonTitle:@"Ok"
                                             otherButtonTitles:nil];
        [alert show];
    }
}

更改:

@"Your Password is:"

至:

[NSString stringWithFormat:@"Your Password is: %@", [self.theuser valueForKey:@"password"]]

您还可以更改此:

 if ([[self.theuser valueForKey:@"password"] > 0 ]) {

对此:

 if ([self.theuser valueForKey:@"password"]) {

如果不存在,它将被视为“假”。 其他任何值为“ true”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM