找到你要的答案

Q:How can i change action of the button “return” on the keyboard on Xcode?

Q:我如何能改变动作的按钮“返回”在Xcode的键盘吗?

i need to change the text of the button "return" on my keyboard,and i even need to change its action. The action should be a new paragraph. Can you help me?

i need to change the text of the button "return" on my keyboard,and i even need to change its action. The action should be a new paragraph. Can you help me?

answer1: 回答1:

UITextField has this delegate method that you can implement, and get's called when you press return on the keyboard: - (BOOL)textFieldShouldReturn:(UITextField *)textField You can add a new line char "\n" to the textView text and it would go to next line.

UITextView it's suppose to work like that where it would go to a new line when you press Return.

UITextField has this delegate method that you can implement, and get's called when you press return on the keyboard: - (BOOL)textFieldShouldReturn:(UITextField *)textField You can add a new line char "\n" to the textView text and it would go to next line.

UITextView它的假设一样,它会去到一个新的线,当你按回车。

answer2: 回答2:

You cannot rename the Return button to any custom text

Some default values thatThe return key can take are

typedef enum : NSInteger {
   UIReturnKeyDefault,
   UIReturnKeyGo,
   UIReturnKeyGoogle,
   UIReturnKeyJoin,
   UIReturnKeyNext,
   UIReturnKeyRoute,
   UIReturnKeySearch,
   UIReturnKeySend,
   UIReturnKeyYahoo,
   UIReturnKeyDone,
   UIReturnKeyEmergencyCall,
} UIReturnKeyType;

where Default is "return" others are Go, Google, Yahoo as is.

look here.

And for capturing the return Event you can use the textView Delegate method

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
     if ([text isEqualToString:@"\n"]) {
        NSLog(@"Pressed Return Key");
    } else {
       NSLog(@"Pressed Any Other Key");
       }
return YES;
}

不能将返回按钮重命名为任何自定义文本

返回键可以采取的一些默认值是

typedef enum : NSInteger {
   UIReturnKeyDefault,
   UIReturnKeyGo,
   UIReturnKeyGoogle,
   UIReturnKeyJoin,
   UIReturnKeyNext,
   UIReturnKeyRoute,
   UIReturnKeySearch,
   UIReturnKeySend,
   UIReturnKeyYahoo,
   UIReturnKeyDone,
   UIReturnKeyEmergencyCall,
} UIReturnKeyType;

默认是“返回”别人去,谷歌,雅虎是。

看这里。

为捕获返回事件可以使用TextView委托方法

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
     if ([text isEqualToString:@"\n"]) {
        NSLog(@"Pressed Return Key");
    } else {
       NSLog(@"Pressed Any Other Key");
       }
return YES;
}
answer3: 回答3:

click and set your keyword here

 [textField setReturnKeyType:UIReturnKeyDone];

for keyboard implement the protocol in your class, set

textfield.delegate = self;


  - (BOOL)textFieldShouldReturn:(UITextField *)textField {

    // write you code here what you want .

        return YES;
    }

refer this if you want to UITextView How to dismiss keyboard for UITextView with return key?

单击此处设置关键字

 [textField setReturnKeyType:UIReturnKeyDone];

用于在您的类中实现协议

textfield.delegate = self;


  - (BOOL)textFieldShouldReturn:(UITextField *)textField {

    // write you code here what you want .

        return YES;
    }

把这个如果你想如何将UITextView添加返回键UITextView键盘?

answer4: 回答4:

Here is the way to add custom button over the keyboard. But apple could reject your app so be careful.

- (void)viewWillAppear:(BOOL)animtated {

    // Register the observer for the keyboardWillShow event
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];

}

- (void)viewWillDisappear:(BOOL)animtated {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    // create custom buttom
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(self.view.frame.size.width- 100, self.view.frame.size.height - 200.0f, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton addTarget:self action:@selector(addParaGraph:) forControlEvents:UIControlEventTouchUpInside];
    [doneButton setBackgroundColor:[UIColor redColor]];
    [doneButton setTitle:@"Add Paragraph" forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(textFieldShouldReturn:) forControlEvents:UIControlEventTouchUpInside];

    // locate keyboard view
    UIWindow *tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView *keyboard;
    for (int i = 0; i < [tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard view found; add the custom button to it
        //if ([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES) {
            [keyboard addSubview:doneButton];
        //}
    }
}

- (void)addParaGraph:(id)sender{

    // Write here you code to add new paragraph
}

-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    // Set the FirstResponder of the UITextField on the layout
    [theTextField resignFirstResponder];
    return YES;
}

以下是在键盘上添加自定义按钮的方法。但苹果可能拒绝你的应用程序,所以要小心。

- (void)viewWillAppear:(BOOL)animtated {

    // Register the observer for the keyboardWillShow event
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];

}

- (void)viewWillDisappear:(BOOL)animtated {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    // create custom buttom
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(self.view.frame.size.width- 100, self.view.frame.size.height - 200.0f, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton addTarget:self action:@selector(addParaGraph:) forControlEvents:UIControlEventTouchUpInside];
    [doneButton setBackgroundColor:[UIColor redColor]];
    [doneButton setTitle:@"Add Paragraph" forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(textFieldShouldReturn:) forControlEvents:UIControlEventTouchUpInside];

    // locate keyboard view
    UIWindow *tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView *keyboard;
    for (int i = 0; i < [tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard view found; add the custom button to it
        //if ([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES) {
            [keyboard addSubview:doneButton];
        //}
    }
}

- (void)addParaGraph:(id)sender{

    // Write here you code to add new paragraph
}

-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    // Set the FirstResponder of the UITextField on the layout
    [theTextField resignFirstResponder];
    return YES;
}
ios  text  keyboard  return  action