【问题标题】:How to prompt user for text input in Alert view如何在警报视图中提示用户输入文本
【发布时间】:2011-08-10 18:26:44
【问题描述】:

我正在编写一段代码,如果我可以使用类似 UIAlertView 的弹出框并提示用户输入密码等文本,那将是最好的选择。 关于这样做的优雅方式的任何指示?

【问题讨论】:

    标签: iphone objective-c ios cocoa-touch ios4


    【解决方案1】:

    iOS 5 中的事情要简单得多,只需将 alertViewStyle 属性设置为适当的样式(UIAlertViewStyleSecureTextInput、UIAlertViewStylePlainTextInput 或 UIAlertViewStyleLoginAndPasswordInput)。示例:

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Password" message:@"Enter your password:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
    UITextField *passwordTextField = [alertView textFieldAtIndex:0];
    [alertView show];
    

    【讨论】:

      【解决方案2】:

      >简单可以这样申请

      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Filename" message:@"Enter the file name:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
      alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
      UITextField *passwordTextField = [alertView textFieldAtIndex:0];
      [alertView show]
      

      【讨论】:

        【解决方案3】:

        我发现执行此操作的最佳方法是遵循本教程:http://junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html

        用于实现这一点的代码是(直接取自那个很棒的教程):

        UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:@"Server Password" message:@"\n\n\n"
        delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel",nil) otherButtonTitles:NSLocalizedString(@"OK",nil), nil];
        
        UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(12,40,260,25)];
        passwordLabel.font = [UIFont systemFontOfSize:16];
        passwordLabel.textColor = [UIColor whiteColor];
        passwordLabel.backgroundColor = [UIColor clearColor];
        passwordLabel.shadowColor = [UIColor blackColor];
        passwordLabel.shadowOffset = CGSizeMake(0,-1);
        passwordLabel.textAlignment = UITextAlignmentCenter;
        passwordLabel.text = @"Account Name";
        [passwordAlert addSubview:passwordLabel];
        
        UIImageView *passwordImage = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"passwordfield" ofType:@"png"]]];
        passwordImage.frame = CGRectMake(11,79,262,31);
        [passwordAlert addSubview:passwordImage];
        
        UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)];
        passwordField.font = [UIFont systemFontOfSize:18];
        passwordField.backgroundColor = [UIColor whiteColor];
        passwordField.secureTextEntry = YES;
        passwordField.keyboardAppearance = UIKeyboardAppearanceAlert;
        passwordField.delegate = self;
        [passwordField becomeFirstResponder];
        [passwordAlert addSubview:passwordField];
        
        [passwordAlert setTransform:CGAffineTransformMakeTranslation(0,109)];
        [passwordAlert show];
        [passwordAlert release];
        [passwordField release];
        [passwordImage release];
        [passwordLabel release];
        

        【讨论】:

        • 移除图像、字体和背景颜色分配。添加密码Field.borderStyle = UITextBorderStyleRoundedRect;
        • [passwordAlert setTransform:CGAffineTransformMakeTranslation(0,109)];已经过时了。
        • 很高兴知道可以做到这一点,但 xcoder 的答案对于 iOS 5 及更高版本要好得多...
        【解决方案4】:

        如果我的应用还没有发布一两个月,那么我会登录http://developer.apple.com,查看 iOS 5 测试版区域,看看UIAlertView 是否有什么东西可以为我们准备。

        【讨论】:

          【解决方案5】:

          我认为知道 UIAlertView 不是模态的,因此警报不会阻塞会很有帮助。

          我遇到了这个问题,我想提示用户输入然后继续,然后在代码中使用该输入。但是,[alert show] 之后的代码将首先运行,直到您到达运行循环的末尾,然后才会显示警报。

          【讨论】:

            【解决方案6】:

            优化代码:

            UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:@"Password"
                                                                    message:@"Please enter the password:\n\n\n"
                                                                   delegate:self
                                                          cancelButtonTitle:NSLocalizedString(@"Cancel",nil)
                                                          otherButtonTitles:NSLocalizedString(@"OK",nil), nil];
            
            UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)];
            passwordField.borderStyle = UITextBorderStyleRoundedRect;
            passwordField.secureTextEntry = YES;
            passwordField.keyboardAppearance = UIKeyboardAppearanceAlert;
            passwordField.delegate = self;
            [passwordField becomeFirstResponder];
            [passwordAlert addSubview:passwordField];
            
            [passwordAlert show];
            [passwordAlert release];
            [passwordField release];
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-04-23
              相关资源
              最近更新 更多