@implementation MyViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view setBackgroundColor:[UIColor whiteColor]];
currentTF = [[UITextField alloc]init];
[self createLoginUI];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
}
-(void)createLoginUI
{
//For My Label
UILabel *MyLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 340, 20)];
MyLabel.text = @"My Login Screen";
MyLabel.textAlignment = UITextAlignmentCenter;
MyLabel.textColor = [UIColor blackColor];
MyLabel.backgroundColor=[UIColor cyanColor];
MyLabel.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
[self.view addSubview:MyLabel];
//For Email Label
UILabel *emailLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 40, 50, 20)];
emailLabel.text = @"Email:";
emailLabel.textColor = [UIColor blackColor];
emailLabel.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
[self.view addSubview:emailLabel];
//For Email UITextField
emailTF = [[UITextField alloc]initWithFrame:CGRectMake(85, 40, 200, 30)];
[emailTF setBorderStyle:UITextBorderStyleRoundedRect];
emailTF.delegate = self;
emailTF.tag = 101;
[self.view addSubview:emailTF];
//For Password Label
UILabel *passwordLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 80, 100, 20)];
passwordLabel.text = @"Password:";
passwordLabel.textColor = [UIColor blackColor];
passwordLabel.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
[self.view addSubview:passwordLabel];
//For Password UITextField
passwordTF = [[UITextField alloc]initWithFrame:CGRectMake(105, 80, 180, 30)];
passwordTF.secureTextEntry = YES;
passwordTF.delegate = self;
passwordTF.tag = 102;
[passwordTF setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:passwordTF];
//For Login Button
UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[loginBtn setTitle:@"Login" forState:UIControlStateNormal];
loginBtn.frame = CGRectMake(120, 130, 50, 20);
loginBtn.backgroundColor = [UIColor greenColor];
[loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:loginBtn];
//For Forgot Password Button
UIButton *forgotPasswdBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[forgotPasswdBtn setTitle:@"Forgot Password" forState:UIControlStateNormal];
forgotPasswdBtn.frame = CGRectMake(30, 170, 120, 20);
forgotPasswdBtn.backgroundColor = [UIColor greenColor];
[forgotPasswdBtn addTarget:self action:@selector(forgotPasswordBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:forgotPasswdBtn];
//For sign up Button
UIButton *signUpBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[signUpBtn setTitle:@"SignUp" forState:UIControlStateNormal];
signUpBtn.frame = CGRectMake(170, 170, 60, 20);
signUpBtn.backgroundColor = [UIColor greenColor];
[signUpBtn addTarget:self action:@selector(signUpBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:signUpBtn];
//For remember Me
UIButton *remBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[remBtn setImage:[UIImage imageNamed:@"uncheck.jpeg"] forState:UIControlStateNormal];
remBtn.frame = CGRectMake(90, 130, 20, 20);
[remBtn addTarget:self action:@selector(remBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:remBtn];
}
-(void)loginBtnClick
{
[currentTF resignFirstResponder];
if ([self validateLoginScreen])
{
//To clear textFields
emailTF.text= @"";
passwordTF.text = @"";
//For Home View
homeViewObj = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[homeViewObj setBackgroundColor:[UIColor purpleColor]];
[self.view addSubview:homeViewObj];
//For Logout Button
UIButton *logOutBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[logOutBtn setTitle:@"Logout" forState:UIControlStateNormal];
logOutBtn.frame = CGRectMake(170, 170, 60, 20);
// logOutBtn.backgroundColor = [UIColor greenColor];
[logOutBtn addTarget:self action:@selector(logOutBtnClick) forControlEvents:UIControlEventTouchUpInside];
[homeViewObj addSubview:logOutBtn];
}
}
-(void)forgotPasswordBtnClick
{
UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"Forgot Password" message:@"please enter sequrity qustion" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
alertview.alertViewStyle=UIAlertViewStylePlainTextInput;
[alertview textFieldAtIndex:0].delegate=self;
[alertview show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==1)
{
UIAlertView *alertview2=[[UIAlertView alloc]initWithTitle:@"Reset password" message:@"new password is send to your mail id" delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];
[alertview2 show];
}
}
-(void)signUpBtnClick
{
//[currentTF resignFirstResponder];
viewobjsignup=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[viewobjsignup setBackgroundColor:[UIColor grayColor]];
[self.view addSubview:viewobjsignup];
UILabel *MyLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 340, 20)];
MyLabel.text = @"Register Here";
MyLabel.textAlignment = UITextAlignmentCenter;
MyLabel.textColor = [UIColor blackColor];
MyLabel.backgroundColor=[UIColor cyanColor];
MyLabel.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
[viewobjsignup addSubview:MyLabel];
//username
UILabel *usernmobj=[[UILabel alloc]initWithFrame:CGRectMake(20, 40, 100, 20)];
[email protected]"UserName";
usernmobj.textAlignment = UITextAlignmentCenter;
usernmobj.textColor = [UIColor blackColor];
usernmobj.backgroundColor=[UIColor cyanColor];
usernmobj.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
[viewobjsignup addSubview:usernmobj];
userTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 40, 150, 30)];
//userTF.secureTextEntry = YES;
userTF.delegate = self;
// userTF.tag = 102;
[userTF setBorderStyle:UITextBorderStyleRoundedRect];
[viewobjsignup addSubview:userTF];
userTF.returnKeyType=UIReturnKeyDone;
//email id
UILabel *Emailnmobj=[[UILabel alloc]initWithFrame:CGRectMake(20, 80, 80, 20)];
[email protected]"Email Id";
Emailnmobj.textAlignment = UITextAlignmentCenter;
Emailnmobj.textColor = [UIColor blackColor];
Emailnmobj.backgroundColor=[UIColor cyanColor];
Emailnmobj.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
[viewobjsignup addSubview:Emailnmobj];
EmailTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 80, 150, 30)];
//EmailTF.secureTextEntry = YES;
EmailTF.delegate = self;
// EmailTF.tag = 102;
[EmailTF setBorderStyle:UITextBorderStyleRoundedRect];
[viewobjsignup addSubview:EmailTF];
EmailTF.returnKeyType=UIReturnKeyDone;
//password
UILabel *passnmobj=[[UILabel alloc]initWithFrame:CGRectMake(20,120,100,20)];
[email protected]"Password";
passnmobj.textAlignment = UITextAlignmentCenter;
passnmobj.textColor = [UIColor blackColor];
passnmobj.backgroundColor=[UIColor cyanColor];
passnmobj.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
[viewobjsignup addSubview:passnmobj];
pwdTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 120, 170, 30)];
pwdTF.secureTextEntry = YES;
pwdTF.delegate = self;
// pwdTF.tag = 102;
[pwdTF setBorderStyle:UITextBorderStyleRoundedRect];
[viewobjsignup addSubview:pwdTF];
pwdTF.returnKeyType=UIReturnKeyDone;
//conform password
UILabel *comfobj=[[UILabel alloc]initWithFrame:CGRectMake(20,160,100,20)];
[email protected]"Confpass";
//comfobj.textAlignment = UITextAlignmentCenter;
comfobj.textColor = [UIColor blackColor];
comfobj.backgroundColor=[UIColor cyanColor];
comfobj.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
[viewobjsignup addSubview:comfobj];
comfTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 160, 180, 30)];
comfTF.secureTextEntry = YES;
comfTF.delegate = self;
// comfTF.tag = 102;
[comfTF setBorderStyle:UITextBorderStyleRoundedRect];
[viewobjsignup addSubview:comfTF];
comfTF.returnKeyType=UIReturnKeyDone;
//for dob
UILabel *Dboobj=[[UILabel alloc]initWithFrame:CGRectMake(20, 190, 140, 20)];
[email protected]"DOB";
Dboobj.textAlignment = UITextAlignmentCenter;
Dboobj.textColor = [UIColor blackColor];
Dboobj.backgroundColor=[UIColor cyanColor];
Dboobj.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
[viewobjsignup addSubview:Dboobj];
//textfiels for date
UITextField *dateobj=[[UITextField alloc]initWithFrame:CGRectMake(20, 210, 90, 30)];
[dateobj setBorderStyle:UITextBorderStyleRoundedRect];
[email protected]"Date";
[dateobj setKeyboardType:UIKeyboardTypeNumberPad];
dateobj.tag=105;
dateobj.delegate=self;
[viewobjsignup addSubview:dateobj];
dateobj.returnKeyType=UIReturnKeyDone;
// //txtfield for DOB_date
// UITextField * dob_dateLab=[[UITextField alloc]initWithFrame:CGRectMake(20, 270, 90, 30)];
// [dob_dateLab setBorderStyle:UITextBorderStyleRoundedRect];
// [email protected]"Date";
// [dob_dateLab setKeyboardType:UIKeyboardTypeNumberPad];
// dob_dateLab.tag=104;
// dob_dateLab.delegate = self;
// [viewobjsignup addSubview:dob_dateLab];
//txtfield for month
UITextField * monthobj=[[UITextField alloc]initWithFrame:CGRectMake(110, 210, 90, 30)];
[monthobj setBorderStyle:UITextBorderStyleRoundedRect];
monthobj.delegate = self;
[monthobj setKeyboardType:UIKeyboardTypeNumberPad];
monthobj.tag=106;
[email protected]"Month";
[viewobjsignup addSubview:monthobj];
monthobj.returnKeyType=UIReturnKeyDone;
//txtfield for year
UITextField * yearobj=[[UITextField alloc]initWithFrame:CGRectMake(200, 210, 90, 30)];
[yearobj setBorderStyle:UITextBorderStyleRoundedRect];
yearobj.delegate = self;
[yearobj setKeyboardType:UIKeyboardTypeNumberPad];
yearobj.tag=107;
[email protected]"Year";
[viewobjsignup addSubview:yearobj];
yearobj.returnKeyType=UIReturnKeyDone;
//for label gender
UILabel *genderobj=[[UILabel alloc]initWithFrame:CGRectMake(20, 240, 80, 20)];
[email protected]"Gender";
genderobj.textAlignment = UITextAlignmentCenter;
genderobj.textColor = [UIColor blackColor];
genderobj.backgroundColor=[UIColor cyanColor];
genderobj.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
[viewobjsignup addSubview:genderobj];
//for male
UILabel *maleobj=[[UILabel alloc]initWithFrame:CGRectMake(120, 240, 40, 20)];
[email protected]"male";
maleobj.textAlignment = UITextAlignmentCenter;
maleobj.textColor = [UIColor blackColor];
maleobj.backgroundColor=[UIColor cyanColor];
maleobj.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
[viewobjsignup addSubview:maleobj];
//for female
UILabel *femaleobj=[[UILabel alloc]initWithFrame:CGRectMake(200, 240, 80, 20)];
[email protected]"Female";
femaleobj.textAlignment = UITextAlignmentCenter;
femaleobj.textColor = [UIColor blackColor];
femaleobj.backgroundColor=[UIColor cyanColor];
femaleobj.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
[viewobjsignup addSubview:femaleobj];
//for male radio but
malebut=[UIButton buttonWithType:UIButtonTypeCustom];
[malebut setImage:[UIImage imageNamed:@"radio_check.jpeg"] forState:UIControlStateNormal];
//[malebut setImage:[UIImage imageNamed:@"radiobutton-checked.png"] forState:UIControlStateNormal];
malebut.frame = CGRectMake(160, 240, 20, 20);
[malebut addTarget:self action:@selector(radioBtnClick_male) forControlEvents:UIControlEventTouchUpInside];
[viewobjsignup addSubview:malebut];
//For female radio but
femalebut = [UIButton buttonWithType:UIButtonTypeCustom];
[ femalebut setImage:[UIImage imageNamed:@"radio_uncheck.jpeg"] forState:UIControlStateNormal];
femalebut.frame = CGRectMake(270, 240, 20, 20);
[ femalebut addTarget:self action:@selector(radioBtnClick_female) forControlEvents:UIControlEventTouchUpInside];
[viewobjsignup addSubview:femalebut];
//For Save button
UIButton * savebut = [UIButton buttonWithType:UIButtonTypeCustom];
[savebut setTitle:@"Save" forState:UIControlStateNormal];
savebut.titleLabel.backgroundColor=[UIColor blackColor];
savebut.frame = CGRectMake(90, 430, 70, 20);
[savebut addTarget:self action:@selector(saveBtnClick) forControlEvents:UIControlEventTouchUpInside];
[viewobjsignup addSubview:savebut];
//For cancel button
UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
cancelBtn.frame = CGRectMake(150, 430, 60, 20);
[cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];
cancelBtn.titleLabel.backgroundColor=[UIColor blackColor];
cancelBtn.backgroundColor=[UIColor blackColor];
[cancelBtn addTarget:self action:@selector(cancelBtnClick1) forControlEvents:UIControlEventTouchUpInside];
[viewobjsignup addSubview:cancelBtn];
//register button
// UIButton *registerbut=[UIButton buttonWithType:UIButtonTypeRoundedRect];
// [registerbut setTitle:@"Register" forState:UIControlStateNormal];
// [registerbut setFrame:CGRectMake(20,200,160,20)];
// [registerbut addTarget:self action:@selector(Registerclick) forControlEvents:UIControlEventTouchUpInside];
// [viewobjsignup addSubview:registerbut];
// //labal
// myleb=[[UILabel alloc]initWithFrame:CGRectMake(20,160,140,20)];
// //[email protected]" ConfermPassword";
// myleb.textAlignment = UITextAlignmentCenter;
// myleb.textColor = [UIColor blackColor];
// myleb.backgroundColor=[UIColor cyanColor];
// myleb.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
// [self.view addSubview: myleb];
}
-(void)radioBtnClick_female
{
[femalebut setImage:[UIImage imageNamed:@"radio_check.jpeg"] forState:UIControlStateNormal];
[malebut setImage:[UIImage imageNamed:@"radio_uncheck.jpeg"] forState:UIControlStateNormal];
}
-(void)radioBtnClick_male
{
[femalebut setImage:[UIImage imageNamed:@"radio_uncheck.jpeg"] forState:UIControlStateNormal];
[malebut setImage:[UIImage imageNamed:@"radio_check.jpeg"] forState:UIControlStateNormal];
}
-(void)logOutBtnClick
{
[homeViewObj removeFromSuperview];
}
-(void)remBtnClick
{
}
-(void)cancelBtnClick1
{
[viewobjsignup removeFromSuperview];
}
-(void)saveBtnClick
{
[currentTF resignFirstResponder];
[self validregister];
}
-(void)validregister
{
if(userTF.text.length==00||pwdTF.text.length==00||comfTF.text.length==00)
{
[self showAlertMessage:@"Please enter all nesessary field"];
}
else if([self emailValidation:EmailTF.text]==NO)
{
[self showAlertMessage:@"Please enter valid email address"];
[email protected]"";
}
else if([pwdTF.text isEqual:comfTF.text]==NO)
{
[self showAlertMessage:@"password mismatch"];
// [email protected]"";
[email protected]"";
}
else
{
[email protected]"";
[email protected]"";
[user setObject:EmailTF.text forKey:@"username plist"];
[user setObject:pwdTF.text forKey:@"password plist"];
[user synchronize];
[self showAlertMessage:@"You are REGISTERED now"];
[viewobjsignup removeFromSuperview];
}
}
//-(void)Registerclick
//{
// UIAlertView *alertview1 = [[UIAlertView alloc]initWithTitle:@"Welcome" message:@"Your registration is successful..!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
// [alertview1 show];
// [viewobjsignup removeFromSuperview];
//}
//
#pragma mark Validation Method
-(BOOL)validateLoginScreen
{
if (emailTF.text.length==0)
{
[self showAlertMessage:@"Please enter email address"];
return NO;
}
else if ([self emailValidation:emailTF.text]==NO)
{
[self showAlertMessage:@"Please enter valid email address"];
return NO;
}
else if (passwordTF.text.length==0)
{
[self showAlertMessage:@"Please enter password "];
return NO;
}
return YES;
}
-(void)showAlertMessage:(NSString *)msg
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Note" message:msg delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
-(BOOL) emailValidation:(NSString *)emailTxt
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:emailTxt];
}
#pragma mark TextField Delegates
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
// if(textField.tag==101)
// {
// [passwordTF becomeFirstResponder];
// }
// else if(textField.tag==102)
// {
// [passwordTF resignFirstResponder];
// }
[currentTF resignFirstResponder];
return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[currentTF resignFirstResponder];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
currentTF = textField;
[self animateTextField:textField up:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
currentTF = textField;
[self animateTextField:textField up:NO];
}
-(void)animateTextField:(UITextField*)textField up:(BOOL)up
{
int movementDistance = 0; // tweak as needed
if (textField.tag==105||textField.tag
==106||textField.tag==107)
{
movementDistance = -130;
}
else
{
movementDistance = 00;
}
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? movementDistance : -movementDistance);
[UIView beginAnimations: @"animateTextField" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Có một giới thiệu về các chủ đề của việc tạo ra của ui xibless và một số tiếp theo vào liên kết trên http://blog.xamarin.com/creating-imperative-uis- in-c/ – Stuart
Nếu bạn cần đăng nhập bằng biểu mẫu, tôi khuyên bạn nên xem trang này, http://docs.xamarin.com/recipes/ios/content_controls/tables/create_a_login_window_using_monotouch.dialog Nơi bạn có thể tìm thấy hướng dẫn tốt để làm chính xác những gì bạn đang tìm kiếm, cũng có mã mẫu để tải xuống. Và có, nó hoàn toàn tốt để sử dụng trong sản xuất, miễn là bạn xử lý bảo mật chính mình và không sử dụng tên người dùng mật khẩu cứng/mật khẩu mà họ làm trong ví dụ. ;-) – Nordis
Hướng dẫn hay. Cảm ơn bạn đã viết mã cứng. ;) – Meryovi