2011-05-30 18 views
11

को छिपाने के लिए एक बटन जोड़ें एक UITextView पर कीबोर्ड छिपाने के लिए, वहाँ विधि है:कुंजीपटल

... 
    textfield.returnKeyType = UIReturnKeyDone; 
    textfield.delegate = self; 
.... 

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

} 

लेकिन मैं "वापसी" करने के लिए बटन "किया" छोड़ने के लिए और के लिए एक बटन जोड़ना चाहते हैं कीबोर्ड छुपाएं, मैं कैसे करूँ?

उत्तर

34

आप एक बटन के साथ टूलबार असाइन कर सकते हैं जो कुंजीपटल को टेक्स्ट फ़ील्ड के inputAccessoryView के रूप में खारिज कर देता है। एक त्वरित उदाहरण होगा,

UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:textField action:@selector(resignFirstResponder)] autorelease]; 
UIToolbar *toolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease]; 
toolbar.items = [NSArray arrayWithObject:barButton]; 

textField.inputAccessoryView = toolbar; 
+0

धन्यवाद, लेकिन मैं भी एक प्रमुख विधि से कॉल करना चाहते हैं तो क्या होगा? – Vins

+0

मुझे आपको नहीं मिला। आप क्या कॉल करना चाहते हैं? –

+0

मेरे पास एक तरीका है जो पाठ को डेटाबेस [सेव सेवस्ट्रिंग] में सहेजता है; – Vins

3

यह आसान तरीके से किया जा सकता है!

मैं आईबी में एक कस्टम दृश्य बना दिया, मेरी viewController.h में मैं सिर्फ एक IBOutlet UIView *accessoryView; बनाया है, उन्हें जुड़ा हुआ है और एक - (IBAction)dismissKeyboard;

मैं एक किया बटन के साथ एक उपकरण पट्टी दृश्य में नहीं रखा, IBAction से कनेक्शन बनाया एक ने लिखा है: [textView resignFirstResponder] और

- (void)viewDidLoad 
{ 
    textView.inputAccessoryView = accessoryView; 
    [super viewDidLoad]; 
} 

लेकिन असल में यह है कि थोड़ा अजीब और गैर सेब शैली लग रहा है ... एक विचार है?

4

स्विफ्ट 2.0 संस्करण:

//Declared at top of view controller 
var accessoryDoneButton: UIBarButtonItem! 
let accessoryToolBar = UIToolbar(frame: CGRectMake(0,0,UIScreen.mainScreen().bounds.width, 44)) 
//Could also be an IBOutlet, I just happened to have it like this 
let codeInput = UITextField() 

//Configured in viewDidLoad() 
self.accessoryDoneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: #selector(self.donePressed(_:))) 
self.accessoryToolBar.items = [self.accessoryDoneButton] 
self.codeInput.inputAccessoryView = self.accessoryToolBar 

स्विफ्ट 4:

//Declared at top of view controller 
var accessoryDoneButton: UIBarButtonItem! 
let accessoryToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44)) 
//Could also be an IBOutlet, I just happened to have it like this 
let codeInput = UITextField() 

//Configured in viewDidLoad() 
self.accessoryDoneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.donePressed)) 
self.accessoryToolBar.items = [self.accessoryDoneButton] 
self.codeInput.inputAccessoryView = self.accessoryToolBar 

func donePressed() { 
    //Causes the view (or one of its embedded text fields) to resign the first responder status. 
    view.endEditing(true) 
} 

UIToolBar Documentation

'inputAccessoryView' documentation

+0

कहाँ 'donePressed' विधि है? – rmaddy

+0

' donePressed' में सिर्फ एक समारोह है अपने ' UIViewController'। मैं आमतौर पर पहले उत्तरदाता के सक्रिय कीबोर्ड से इस्तीफा देता हूं और फिर यदि मैं चाहूं तो नेविगेशन स्टैक से व्यू कंट्रोलर को पॉप करता हूं। –

संबंधित मुद्दे