当你在 iPhone 上输入完文字后,如何关闭键盘?
我正在写一个简单的代码,如下,,,,
- (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;
}
这是Coding of RestaurantViewController.m 但当我按回车键时什么也没有发生,
有什么想法吗???
I am writing a simple code, as below,,,,
- (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;
}
THis is Coding oF RestaurantViewController.m
but nothing is happening when I press Return key,
any Idea???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保您的视图控制器采用
协议。然后将每个 UITextField 的.delegate
属性设置为self
:此外,您分配和初始化的所有内容都需要进行
release
处理。在这种情况下,这可能应该发生在将每个此类内容添加到超级视图的addSubview:
调用之后。Make sure your view controller adopts the
<UITextFieldDelegate>
protocol. Then set the.delegate
property of each of your UITextFields toself
:Also, EVERYTHING that you alloc and init needs to be
release
'ed. In this case, that should probably happen right after theaddSubview:
call that adds each such thing to the superview.你的 .h 文件应该像这样
UITextField *amount_textfield;
你的实现文件
应该是:
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
返回是;
}
your .h file should have to be like this
UITextField *amount_textfield;
}
and your implementation file should be:
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return Yes;
}
您是否已将文本字段的代表连接到控制器?例如,
restaurant_name.delegate = self
?您必须对所有需要在按 Return 时关闭键盘的文本字段执行此操作。Have you connected the delegates of the text fields to the controller? E.g.,
restaurant_name.delegate = self
? You have to do this for all the text fields that require the keyboard to be dismissed when you press Return.