开发者

how to dismiss the keyboard, when you finish typing in iphone?

开发者 https://www.devze.com 2023-02-08 16:27 出处:网络
I am writing a simple code, as below,,,, - (void)viewDidLoad { [super viewDidLoad]; restaurant_name = [[UILabel alloc] initWithFrame:CGRectMake(20, 83, 135, 21)];

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 Ide开发者_如何学Pythona???


Make sure your view controller adopts the <UITextFieldDelegate> protocol. Then set the .delegate property of each of your UITextFields to self:

amount_textfield.delegate = self;

Also, EVERYTHING that you alloc and init needs to be release'ed. In this case, that should probably happen right after the addSubview: call that adds each such thing to the superview.


your .h file should have to be like this

@interface Book : UIViewController<UITextFieldDelegate> {

UITextField *amount_textfield;

}

and your implementation file should be:

    - (void)loadView {

amount_textfield = [[UITextField alloc] initWithFrame:CGRectMake(60, 120, 200, 40)];
amount_textfield.delegate=self;
[ContentView addSubview:amount_textfield];}

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消