Wednesday 18 July 2012

Hides a Keyboard in iOS

Text Field Should Return:


.h File



@interface ViewController : UIViewController<UITextFieldDelegate>
{
}
@property (retain, nonatomic) IBOutlet UITextField *userTxtfld;
@property (retain, nonatomic) IBOutlet UITextField *passTxtfld;
@end

.m File
@synthesize selecteddate
- (void)viewDidLoad
{
    [super viewDidLoad];
    userTxtfld.delegate=self;
    passTxtfld.delegate=self;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == userTxtfld)
    {
        [textField resignFirstResponder];
    }
    else if (textField == passTxtfld)
    {
        [textField resignFirstResponder];
    }
    return textField;
}


Textview Should Return:

.h File :
@interface ViewController : UIViewController
{
}
@property (retain, nonatomic) IBOutlet UITextView *textview;

.m File
@synthesize  textview;
- (void)viewDidLoad
{
    [super viewDidLoad];
    textview.delegate=self;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if([text isEqualToString:@"\n"]) 
   {
        [textview resignFirstResponder];
        return NO;
    }
    return YES;
}

BackGround Touched Hides the Keyboard:


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [Name resignFirstResponder];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{   
    [Name resignFirstResponder];
}

No comments: