Category Archives: TextField and button in objective-C

TextField and button in objective-C

//
// ViewController.h
// helloWorld2
//
// Created by USSLPS11 on 12/10/16.
// Copyright © 2016 USSLPS11. All rights reserved.
//



#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (nonatomic, strong) UITextField* userName;
@property (nonatomic, strong) UITextField* userPassword;

@property (nonatomic, copy) NSString* userText;
@property (nonatomic, copy) NSString* userPass;

@end
// azmalhossain
///
// ViewController.m
// TextFieldSample
//
// Created by USSLPS1 on 12/10/16.
// Copyright © 2016 USSLPS1. All rights reserved.
// azmal

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
}

- (void)viewDidLoad {
 [super viewDidLoad];
 
 self.userName = [[UITextField alloc] init]; // initional system
 self.userName.frame = CGRectMake(30, 100, 300, 44); // frame around size
 self.userName.delegate = self; // to attach delegate rule
 self.userName.placeholder = @"Email"; // for placeholder preview
 self.userName.backgroundColor = [UIColor greenColor];
 self.userName.returnKeyType = UIReturnKeyNext; // to change done button in keyboard
 [self.view addSubview:self.userName]; // add subview
 
 
 self.userPassword = [[UITextField alloc] init];
 self.userPassword.frame = CGRectMake(30, 150, 300, 44);
 self.userPassword.delegate = self;
 self.userPassword.placeholder = @"Password";
 self.userPassword.backgroundColor = [UIColor greenColor];
 self.userPassword.returnKeyType = UIReturnKeyDone;
 self.userPassword.secureTextEntry = YES; // to show star symble instant of number
 [self.view addSubview:self.userPassword];
 
 // beign to button works
 UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
 button.frame = CGRectMake(130, 200, 100, 18);
 [button setTitle:@"Press Me" forState:UIControlStateNormal]; // for title
 [button addTarget:self action:@selector(pressMeAction:) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:button]; // for new view on prime view
}

- (void)pressMeAction:(UIButton*) sender {
 [self.view endEditing:YES];
 [self showAuthentication];
}

#pragma mark --
#pragma mark -- UITextField delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField {
 if (textField == self.userName) {
 self.userText = @"";
 } else if (textField == self.userPassword) {
 self.userPass = @"";
 }
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
 if (textField == self.userName) {
 self.userText = textField.text;
 } else if (textField == self.userPassword) {
 self.userPass = textField.text;
 }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField { // sumbit botton
 if (textField == self.userName) {
 [textField resignFirstResponder];
 [self.userPassword becomeFirstResponder]; // atomatic moving to next after completing tying in textfield
 } else if (textField == self.userPassword) {
 [textField resignFirstResponder];
 [self showAuthentication];
 }
 return YES;
}

- (void)showAuthentication {
 if (self.userText.length > 0 && self.userPass.length > 0) { // to check the password and id name
 
 NSString* detailMessage = [NSString stringWithFormat:@"Your User Name = %@, \n and Password = %@", self.userText, self.userPass];
 
 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Authentication Success" message:detailMessage preferredStyle:UIAlertControllerStyleAlert];
 
 UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
 [alertController addAction:ok];
 
 [self presentViewController:alertController animated:YES completion:nil];
 }
}

@end