2015-05-04 74 views
-1

我需要更改鍵盤上的「返回」按鈕的文本,我甚至需要更改其操作。 該行動應該是一個新的段落。 你能幫我嗎?如何在Xcode上的鍵盤上更改按鈕「返回」的操作?

+0

你需要它的'UITextField'或'UITextView'? – pteofil

+0

您可以在UITextField的Delegate方法中編寫代碼。 - (BOOL)textFieldShouldReturn;並且它有可能通過IB屬性 –

+0

更改返回按鈕的名稱是否要更改文本「return」?在鍵盤 – Sport

回答

0

的UITextField有可以實現,當你按下鍵盤上得到回報的稱這種委託方法: - (BOOL)textFieldShouldReturn:(UITextField *)textField 您可以添加一個新行字符「\ n」來TextView的文本,它會進入下一行。

UITextView假設您按回車鍵時,它會轉到新行。

+0

這是textField委託方法和您使用的textview ... –

1

不能返回按鈕重命名爲任何自定義文本

一些默認值thatThe返回鍵可以是

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

,其中默認值是「返回」別人都去,谷歌,雅虎爲是。

看起來here

以及用於捕獲返回事件,你可以使用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; 
} 
0

enter image description here

點擊這裏

[textField setReturnKeyType:UIReturnKeyDone]; 

設定關鍵字鍵盤實現類的協議,集

textfield.delegate = self; 


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

    // write you code here what you want . 

     return YES; 
    } 

如果您要UITextView How to dismiss keyboard for UITextView with return key?

+0

選擇您的文本框並進入他的屬性 – Sport

0

請參考此處以下是在鍵盤上添加自定義按鈕的方法。但是蘋果可能會拒絕你的應用,所以要小心。

- (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; 
} 
0

斯威夫特3

let addressTextField = UITextField() 
addressTextField.placeholder = "Search or enter website name" 
addressTextField.layer.borderColor = UIColor.gray.cgColor 
addressTextField.layer.borderWidth = 1.0 
addressTextField.returnKeyType = .go 
view.addSubview(addressTextField) 
addressTextField.delegate = self 

extension YourNaneController: UITextFieldDelegate { 
    func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
     textField.resignFirstResponder() 
     return true 
    } 
} 
相關問題