【问题标题】:How to show UIKeyboard upon tapping a button?如何在点击按钮时显示键盘?
【发布时间】:2011-12-17 05:11:36
【问题描述】:

我想在点击UIButton 时显示UIKeyboard,并在UILabel 上显示文本(由用户输入)。这可能吗?

【问题讨论】:

    标签: iphone ios uikeyboard


    【解决方案1】:

    您可以创建一个隐藏的 UITextField,并将其设置为 firstResponder。当您输入任何字符时,将这些字符从隐藏的 UITextField 复制到 UILabel。

    【讨论】:

      【解决方案2】:

      为什么不用适当样式(可见)的 UITextField 替换标签?然后当用户再次点击按钮(或某处的完成按钮)时将其交换回来。

      【讨论】:

        【解决方案3】:

        这有点与您想要的相反,但是您可以将代码弄乱并让它在按钮按下时显示键盘。同样在此示例中,当用户触摸背景时会触发更改。您可以将其设置为您想要的任何按钮而不是背景。

        当用户点击背景时隐藏键盘

        #import <UIKit/UIKit.h>
        
        @interface hideKeyboardViewController : UIViewController {
                UITextField    *textField;
        }
        @property (nonatomic, retain) IBOutlet UITextField *textField;
        - (IBAction)textFieldReturn:(id)sender;
        - (IBAction)backgroundTouched:(id)sender;
        @end
        

        选择hideKeyboardViewController.m文件并通过调用我们textField对象的resignFirstResponder方法实现动作:

        #import "hideKeyboardViewController.h"
        @implementation hideKeyboardViewController
        @synthesize textField;
        -(IBAction)textFieldReturn:(id)sender
        {
                [sender resignFirstResponder];
        }
        -(IBAction)backgroundTouched:(id)sender
        {
                [textField resignFirstResponder];
        }
        .
        .
        @end
        

        More on this subject here

        【讨论】:

          猜你喜欢
          • 2012-06-23
          • 1970-01-01
          • 2014-11-24
          • 1970-01-01
          • 2015-06-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-04
          相关资源
          最近更新 更多