【问题标题】:iphone alertview with textfield带有文本字段的 iphone alertview
【发布时间】:2011-04-07 04:35:56
【问题描述】:

我有一个UIAlertView,里面有一个UITextField。我想在UIAlertViewok按钮中输入ma​​il id并提交,但是UIAlertView中的UITextField没有反应,请帮帮我。

谢谢

【问题讨论】:

    标签: iphone objective-c cocoa-touch uialertview


    【解决方案1】:

    从 iOS 5 开始,不再需要上述方法。只需将alertViewStyle 属性设置为适当的样式(UIAlertViewStyleSecureTextInputUIAlertViewStylePlainTextInputUIAlertViewStyleLoginAndPasswordInput)。

    例子:

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Email" message:@"Enter your email:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alertView show];
    

    你可以得到回复

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        UITextField *emailTextField = [alertView textFieldAtIndex:0];
        NSLog(@"%@",emailTextField.text);
    }
    

    【讨论】:

      【解决方案2】:

      UIAlertViewUITextField

      用法:

      NSString *title         = NSLocalizedString(@"Your Title","");
      NSString *placeholder   = NSLocalizedString(@"Placeholder Text","");
      NSString *message       = NSLocalizedString(@"A message to the user.","");
      NSString *cancel        = NSLocalizedString(@"Cancel","");
      NSString *okay          = NSLocalizedString(@"Continue","");
      
      prompt = [[AlertPrompt alloc] initWithTitle:title 
                         placeholder:placeholder 
                             message:message 
                            delegate:self 
                   cancelButtonTitle:cancel 
                       okButtonTitle:okay];
      [prompt show];
      [prompt release];
      

      .h

      #import <Foundation/Foundation.h>
      
      @interface AlertPrompt : UIAlertView <UITextFieldDelegate>
      {
          UITextField *textField;
          NSString *enteredText;
      }
      @property(nonatomic,retain) UITextField *textField;
      @property (nonatomic, retain, readonly) NSString *enteredText;
      - (id)initWithTitle:(NSString *)title placeholder:(NSString *)placeholder message:(NSString *)message delegate:(id)delegate 
        cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle;
      @end
      

      .m

      #import ".h"
      
      @implementation AlertPrompt
      
      @synthesize textField;
      @synthesize enteredText;
      
      #pragma mark -
      #pragma mark AlertPrompt Delegates
      
      - (id)initWithTitle:(NSString *)title placeholder:(NSString *)placeholder message:(NSString *)message delegate:(id)delegate 
        cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle {
      
          if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
          {
              self.title      = title;
              self.message      = [NSString stringWithFormat:@"%@\n\n\n",message];
              self.delegate    = delegate;
      
              UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 75.0f, 260.0, 30.0)]; 
              [theTextField setBackgroundColor:[UIColor clearColor]]; 
              theTextField.borderStyle = UITextBorderStyleRoundedRect;
              theTextField.textColor = [UIColor blackColor];
              theTextField.font = [UIFont systemFontOfSize:18.0];
              theTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
              theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
              theTextField.returnKeyType = UIReturnKeyDone;
              theTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
              theTextField.placeholder = placeholder;
              theTextField.delegate = self;
              [self insertSubview:theTextField atIndex:0];
              self.textField = theTextField;
              [theTextField release];
              CGAffineTransform translate = CGAffineTransformMakeTranslation(0, -10); 
              [self setTransform:translate];
          }
          return self;
      }
      
      - (void)show{
          [textField becomeFirstResponder];
          [super show];
      }
      
      - (NSString *)enteredText{
          return textField.text;
      }
      
      - (void)dealloc{
          [textField resignFirstResponder];
          [textField release];
          [super dealloc];
      }
      @end
      

      代表:

      -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
      {
          if([alertView.title isEqualToString:@"Your Title"])
          {    
              if(buttonIndex == 1)
              {
                  /*  get the user iputted text  */
                  NSString *inputValue = [(AlertPrompt *)alertView enteredText];
                  NSLog(@"User Input: %@",inputValue);
              {
          }
      }
      

      【讨论】:

      • 这是一个非常解释性的答案+1
      • +1 但文本字段的使用不是私有的吗?或者至少没有证件?至少在 3.x 中。
      • @Jordan 不,这个例子没有任何“未记录”的地方。实际上,我在 iTunes App Store 中有 4 个应用程序使用了这段代码,并且在批准方面没有任何问题。
      • @WrightsCS - 我没有说它不起作用,或者它不会获得批准。只是它是无证的。这意味着行为可能会在未来发生变化。
      • @Jordan,不,没有潜在的行为改变。这都是标准的东西。这个例子中的什么看起来可能会改变?我的意思是,对于 Apple,您可能会有所改变,但您会顺势而为并更新您的代码。
      【解决方案3】:
      UIAlertView *emailAlert=[[UIAlertView alloc]initWithTitle:@"Enter your Email-id" message:@"\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
          emailTextField=[[UITextField alloc]initWithFrame:CGRectMake(12,45,260,25)];
          [emailTextField becomeFirstResponder];
      
          [emailTextField setBackgroundColor:[UIColor whiteColor]];
          emailTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
              emailTextField.placeHolder=@"Email";
          [emailAlert addSubview:emailTextField];
          [emailAlert show];
          [emailAlert release];
      

      单击确定按钮时使用 UIAlertView 委托方法。

      【讨论】:

        【解决方案4】:

        Swift 版本:

        var alert = UIAlertView(title: "PostActionTitle", message: "PostActionText", delegate: self, cancelButtonTitle:"PostActionDecline")
        alert.alertViewStyle = UIAlertViewStyle.PlainTextInput
        alert.addButtonWithTitle("PostActionAccept")
        alert.show()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多