iphone - 当你在 iPhone 上输入完文字后,如何关闭键盘?

标签 iphone objective-c keyboard uitextfield

我正在写一个简单的代码,如下,,,,

- (void)viewDidLoad {
    [super viewDidLoad];


    restaurant_name = [[UILabel alloc] initWithFrame:CGRectMake(20, 83, 135, 21)];
    [restaurant_name setBackgroundColor:[UIColor clearColor]];
    [restaurant_name setText: @"Restaurant Name"];
    [self.view addSubview:restaurant_name];

    restaurant_name_textfield = [[UITextField alloc] initWithFrame:CGRectMake(160, 80, 150, 31)];
    [restaurant_name_textfield setBackgroundColor:[UIColor clearColor]];
    [restaurant_name_textfield setBorderStyle:UITextBorderStyleRoundedRect];
    [restaurant_name_textfield resignFirstResponder];
    [self.view addSubview:restaurant_name_textfield];

    restaurant_name_save = restaurant_name_textfield.text;
    //NSLog(restaurant_name_save);

    picker_delivery = [[UILabel alloc] initWithFrame:CGRectMake(115, 113, 110, 25)];
    [picker_delivery setBackgroundColor:[UIColor clearColor]];
    [picker_delivery setFont:[UIFont fontWithName:@"Arial" size:18]];
    [picker_delivery setText: @"Pick/Delivery"];

    [self.view addSubview:picker_delivery];

    amount = [[UILabel alloc] initWithFrame:CGRectMake(20, 153, 150, 20)];
    [amount setText: @"Amount"];
    [amount setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:amount];

    amount_textfield = [[UITextField alloc] initWithFrame:CGRectMake(160,150 , 150, 31)];
    [amount_textfield setBackgroundColor:[UIColor clearColor]];
    [amount_textfield setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:amount_textfield];

    ready_in = [[UILabel alloc] initWithFrame:CGRectMake(20, 188, 150, 20)];
    [ready_in setText:@"Ready in"];
    [ready_in setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:ready_in];

    ready_in_textfield = [[UITextField alloc] initWithFrame:CGRectMake(160, 185, 150, 31)];
    [ready_in_textfield setBackgroundColor:[UIColor clearColor]];
    [ready_in_textfield setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:ready_in_textfield]; 

    reminder = [[UILabel alloc] initWithFrame:CGRectMake(20, 230, 150, 20)];
    [reminder setText: @"Reminder"];
    [reminder setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:reminder];

    mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(220, 230, 50, 50)];
    [self.view addSubview:mySwitch];



    start_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [start_button setFrame:CGRectMake(100, 300, 100, 50)];
    [start_button setTitle:@"Start" forState:UIControlStateNormal];
    //[myButton setImage:myButtonImage forState:UIControlStateNormal];

    [start_button addTarget:self action:@selector(saveData) 
       forControlEvents:UIControlEventTouchUpInside];



        [self.view addSubview:start_button];


}



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

这是 RestaurantViewController.m 的编码 但当我按 Return 键时什么也没有发生,

有什么想法吗???

最佳答案

确保您的 View Controller 采用 <UITextFieldDelegate>协议(protocol)。然后设置.delegate每个 UITextFields 的属性为 self :

amount_textfield.delegate = self;

此外,您分配和初始化的所有内容都必须是 release编辑。在这种情况下,这可能应该发生在addSubview:之后。调用将每个这样的东西添加到 super View 中。

关于iphone - 当你在 iPhone 上输入完文字后,如何关闭键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4874805/

相关文章:

iphone - 是否可以使用 NSLog 语句提交 iPhone 应用程序二进制文件?

iphone - iPhone内存崩溃:信号0

keyboard - 接受更多的同时键盘输入

events - iOS 7 硬件键盘事件

iphone - 如何根据时间取消 UILocalNotification

iphone - UITableView 编辑/完成按钮

ios - 使用 iOS SDK 获取具有特定扩展名的文件

objective-c - OCMock 和 block

ios - POST json 对象作为 iOS 6 中 POST 中的变量名称 json

ios - 是否可以在 UISearchBar 键盘中添加键盘预测选项