Tuesday, 16 February 2016

First Responder & Keyboard Handling

First Responder & Keyboard Handling



Hi!

In IOS the First Responder has play a small role. But it is very important to handle the applications where keyboard is shown.

// Import basic UIKit Package
import UIKit

// Implement UITextFieldDelegate
class Sample_VC: UIViewController, UITextFieldDelegate 
{

// create UITextfield
@IBOutlet weak var et_un: UITextField!
    
@IBOutlet weak var et_pass: UITextField!

override func viewDidLoad() {
        super.viewDidLoad()

// Should fix delegate for self
et_un.delegate = self
et_pass.delegate = self

// Methods for Navigate UITextfield Up & Down on Main view while Typing 
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);


}

    func keyboardWillShow(sender: NSNotification) {
        self.view.frame.origin.y -= 150
    }
    
    func keyboardWillHide(sender: NSNotification) {
        self.view.frame.origin.y += 150
    }


// Implementation Method for UITextField Handl
func textFieldShouldReturn(textField: UITextField) -> Bool {
        if(textField == et_un)
        {
// Move to next UITextField
            et_pass.becomeFirstResponder()
        }
        else if(textField == et_pass)
        {
// Resign the FirstResponder
            et_pass.resignFirstResponder()
        }
        return false
    }


}

Thank You..,

No comments:

Post a Comment