【问题标题】:Hide the keyboard when Enter is pressed in a UITextField在 UITextField 中按下 Enter 时隐藏键盘
【发布时间】:2011-05-03 06:12:30
【问题描述】:

我是 iPhone 开发的新手。

我有一个 UITextField,当我按下回车键时,我需要隐藏键盘。

有什么方法可以做到这一点?

【问题讨论】:

    标签: iphone iphone-sdk-3.0


    【解决方案1】:
    First if the textfield global object is myTextField then <br>
    
    In .h file <br> 
    
    @interface MyScreen : UIViewController <**UITextFieldDelegate**>
    {
         UITextField *myTextField;
    }
    @property (nonatomic,retain) IBOutlet UITextField ***myTextField**;<br>
    
    In .xib <br>
    
    Attach myTextField to the control.<br>
    
    In .m File <br>
    
    
    
    - (void) viewDidLoad<br>
    {
    
           [super viewDidLoad];
           myTextField.delegate = self;
    }
    
    If you want to return the keyboard on particular return button of keyboard press: <br>
    
    #pragma mark  UITextFieldDelegate
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [textField resignFirstResponder];
        return YES;
    }
    
    If you want to return on your particular button <br>
    
    - (IBAction) myButtonPressed:(id)sender
    {
            [self.myTextField resignFirstResponder];
    }
    
    
    Apologise for my bad english.
    
    Thanks & Regards,
    Gautam Tiwari
    Ios Application Developer
    

    【讨论】:

      【解决方案2】:
      • (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; 返回是; }

      【讨论】:

        【解决方案3】:

        实现UITextFieldDelegate协议的textFieldShouldReturn:方法,根据传入的对象返回YES

        - (BOOL) textFieldShouldReturn:(UITextField *) textField {
           [textField resignFirstResponder];
           return (textField == someTextField);
        }
        

        【讨论】:

          【解决方案4】:
          - (BOOL)textFieldShouldReturn:(UITextField *)textField
          {
              [textField resignFirstResponder];
              return YES;
          }
          

          【讨论】:

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