代码之家  ›  专栏  ›  技术社区  ›  Lounges

关闭iphone键盘

  •  15
  • Lounges  · 技术社区  · 17 年前

    我正在尝试重新创建类似于safari中使用的弹出式键盘的东西。

    alt text

    我可以通过在我的视图和相应的按钮上放置一个工具栏来直观地再现它,但是一旦用户触摸了“完成”按钮,我就无法找到任何方法来关闭键盘。

    5 回复  |  直到 13 年前
        1
  •  50
  •   Niels Hansen    15 年前

    代表 文本字段的。

    如果未使用Interface Builder,请确保将textfield的委托设置为self。我还包括returnType。例如,如果textField被称为gameField:

    gameField.delegate = self;
    gameField.returnKeyType = UIReturnKeyDone;
    

    您还必须实现 UITextFieldDelegate 用于您的ViewController。

    @interface YourViewController : UIViewController <UITextFieldDelegate> 
    

    textFieldShouldReturn [textField辞职FirstResponder]

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

    您的所有文本字段都将使用相同的方法,因此您只需进行一次此设置。只要为textField设置了委托,就会为接口实现UITextFieldDelegate,添加textFieldShouldReturn方法并调用 辞去第一响应者您的设置。

        2
  •  26
  •   Frank Krueger    17 年前

    您是否尝试过:

    [viewReceivingKeys resignFirstResponder];
    

    哪里 viewReceivingKeys 是接收文本输入的UIView吗?

        3
  •  6
  •   Ryan Townshend    17 年前

    - (BOOL)textFieldShouldReturn:(UITextField *)theTextField 
    {
        NSLog(@"%@ textFieldShouldReturn", [self class]);
        [theTextField resignFirstResponder];
        // do stuff with the text
        NSLog(@"text = %@", [theTextField text]);
        return YES;
    }
    

    UITextFieldDelegate textFieldShouldReturn: in the iphone cocoa docs

        4
  •  2
  •   Alasdair Allan    14 年前

    如果你说的是将键盘从 UITextField 而不是 UITextView . 你的问题不是很清楚吗?如果是,请确保将您的类标记为 UITextFieldDelegate 在接口文件中,

    @interface MyController: UIViewController <UITextFieldDelegate> {
       UITextField *activeTextField;
    
       // ...remainder of code not show ...
    }
    

    然后您应该实现以下两种委托方法,

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        activeTextField = textField;!    
        return YES;
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        activeTextField = nil; 
        [textField resignFirstResponder];
        return YES;
    }
    

    UITextView 然后事情就复杂了一点。这个 UITextViewDelegate textFieldShouldReturn: 方法,这大概是因为我们不应该期望Return键是用户希望在多行文本输入对话框中停止编辑文本的信号(毕竟,用户可能希望通过按Return键插入换行符)。

    然而,有几种方法可以解决这个问题 辞去键盘急救员的职务。通常的方法是放置一个 当 显示弹出式键盘。当点击此按钮时,该按钮要求文本视图辞去第一响应者的职务,然后它将关闭键盘。

    但是,根据您对界面的规划,您可能需要 UITextView 它本身要做到这一点,您需要子类 UIView

    创建一个新类,

    #import <UIKit/UIKit.h>
    
    @interface CustomView : UIView {
       IBOutlet UITextView *textView;
    }
    
    @end
    

    然后,在实现中,实现 touchesEnded:withEvent: UITextView 辞去第一响应者的职务。

    #import "CustomView.h"
    
    @implementation CustomView
    
    - (id)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
            // Initialization code
        }
        return self;
    }
    
    - (void) awakeFromNib {
        self.multipleTouchEnabled = YES;
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"touches began count %d, %@", [touches count], touches);
    
        [textView resignFirstResponder];
        [self.nextResponder touchesEnded:touches withEvent:event];
    }
    
    @end
    

    添加类后,需要保存所有更改,然后进入Interface Builder并单击视图。在实用程序面板中打开Identity inspector,并更改视图的类型 笔尖 文件将成为您的 CustomView 而不是违约 班然后在连接检查器中,拖动 textView 出口到 UITextView UIView 如果在其他UI元素后面进行子类化,这些元素将在触摸到达UIView层之前拦截触摸。因此,虽然这个解决方案很优雅,但它只能在某些情况下使用。在许多情况下,您必须采用暴力方法添加 多恩

        5
  •  0
  •   frankodwyer    17 年前

    使用导航控制器并在完成后弹出视图?

    例如,我使用如下代码将“关于”框滑入:

    [[self navigationController] presentModalViewController:modalViewController animated:YES];
    

    [self.navigationController dismissModalViewControllerAnimated:YES];
    

    在我的例子中,about框占据了整个屏幕,但我认为这不一定要起作用。

    推荐文章