2013-07-22 17 views
8

Potrzebuję utworzyć prosty formularz logowania (nazwa użytkownika/hasło) do demonstracji za pomocą Xamarin.iOS, ale wolałbym nie nurkować w XCode w tym momencie .Tworzenie prostego interfejsu użytkownika za pomocą kodu przy użyciu Xamarin.iOS

Może coś podobnego do tego, co można zrobić w innych technologiach .NET:

var panel = new Panel(); 
panel.Controls.Add(new Label("Username")); 
panel.Controls.Add(new TextBox("Password")); 

Czy istnieje sposób tworzenia prostych interfejsów w Xamarin dla iOS programowo? Czy wymagane jest projektowanie interfejsów tylko przy użyciu XCode?

+0

Jest wstępem do tematu tworzenia ui xibless-tych i kilku śledź na linki na http://blog.xamarin.com/creating-imperative-uis- in-c/ – Stuart

+0

Jeśli potrzebujesz loginu w formularzu z kodem, proponuję przyjrzeć się tej stronie, http://docs.xamarin.com/recipes/ios/content_controls/tables/create_a_login_window_using_monotouch.dialog Gdzie można znaleźć fajny samouczek, aby zrobić dokładnie to, czego szukasz, jest też przykładowy kod do pobrania. I tak, to jest zupełnie w porządku, aby używać tego w produkcji, o ile sam korzystasz z bezpieczeństwa i nie używasz zaszyfrowanej nazwy użytkownika/hasła, które robią w przykładzie. ;-) – Nordis

+0

Przyjemny samouczek. Dzięki za wskazówkę na kodowanie. ;) – Meryovi

Odpowiedz

11

Tak, możesz utworzyć interfejs użytkownika za pomocą kodu. Korzystanie z projektanta XCode jest całkowicie opcjonalne.

UIButton btn = new UIButton (UIButtonType.RoundedRect);
   
btn.SetTitle ("Hello", UIControlState.Normal); 

btn.Frame = new RectangleF (50, 50, 50, 50);
 
  

btn.TouchDown += delegate {
  
     UIAlertView alert = new UIAlertView("Hello", "Hello, Xamarin!", null, "OK"); 

  alert.Show(); 

 }; 


this.View.AddSubview (btn);
  

do wprowadzania danych UI, można również rozważyć użycie MonoTouch.Dialog.

+2

Dla układu podobnego do systemu Android (trochę jak układ Xaml) również rozważyć https://github.com/toptensoftware/XibFree – Stuart

2

Może trochę późno do gry, ale czy próbowałeś tylko przy użyciu UIAlertView a następnie robi:

yourAlertView.AlertViewStyle = UIAlertViewStyle.LoginAndPasswordInput; 

Będzie utworzyć okienko logowania z pól tekstowych już konfigurację dla Ciebie.

0
@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); 
} 
+1

Czy mógłbyś proszę [edytuj] wyjaśnienie, dlaczego/jak ten kod odpowiada na pytanie? Odpowiedzi dotyczące tylko kodu są zniechęcane, ponieważ nie są tak łatwe do nauczenia się, jak kod z wyjaśnieniem. Bez wyjaśnienia potrzeba znacznie więcej czasu i wysiłku, aby zrozumieć, co zostało zrobione, zmiany wprowadzone do kodu lub jeśli kod jest użyteczny. Wyjaśnienie jest ważne zarówno dla osób próbujących uczyć się na podstawie odpowiedzi, jak i tych, które oceniają odpowiedź, aby sprawdzić, czy jest ona ważna lub warta głosowania. – Makyen

+1

Jeśli mogę zasugerować, jakiś opis tego, co twój kod może pomóc pytającemu. –

0

Xamarin 5.10:

UIButton sampleButton1; 
    UITextField sampleTextField1; 
    UITextField sampleTextField2; 



    public void addUIButtonToView() 
      {   
       this.sampleButton1 = new UIButton (UIButtonType.RoundedRect); 
       sampleButton1.Frame = new CoreGraphics.CGRect (10f, 100f, 300f, 50f); 
       this.sampleButton1.SetTitle ("Sample Button", UIControlState.Normal); 
       this.sampleButton1.SetNeedsLayout(); 
       sampleButton1.TouchUpInside += OnSpeakButtonTapped; 

       this.View.AddSubview (sampleButton1); 
      } 

      void OnSpeakButtonTapped (object sender, EventArgs e) 
      { 
       // This is the event handler for the actual UIButton TouchUpInside event. 
       Console.WriteLine ("button tapped"); 
      } 

public void addUITextFieldToView() 
     { 
      //Initiate UITextField Delegates 
      sampleTextField1 = new UITextField(); 
      sampleTextField1.Frame = new CoreGraphics.CGRect (10f, 100f, 300f, 50f); 
      //sampleTextField1.Text = "This is Xamarin iOS TextField"; 
      sampleTextField1.BackgroundColor = UIColor.LightGray; 
      sampleTextField1.Alpha = 1.0f; 
      sampleTextField1.TextAlignment = UITextAlignment.Center; 
      sampleTextField1.Font = UIFont.FromName (name: "Arial", size: 15f); 

      sampleTextField1.Placeholder = "Enter text here"; 
      sampleTextField1.KeyboardType = UIKeyboardType.EmailAddress; 
      sampleTextField1.ReturnKeyType = UIReturnKeyType.Default; 
      sampleTextField1.MinimumFontSize = 17f; 
      sampleTextField1.AdjustsFontSizeToFitWidth = true; 

      //Additional 
      sampleTextField2 = new UITextField(); 
      sampleTextField2.Frame = new CoreGraphics.CGRect (this.View.Frame.X + 5, this.View.Frame.Y + 250, this.View.Frame.Width - 10, this.View.Frame.Height/3); 
      sampleTextField2.BackgroundColor = UIColor.FromPatternImage (UIImage.FromFile ("Real-Estate_4.jpg")); 
      sampleTextField2.AdjustsFontSizeToFitWidth = true; 
      sampleTextField2.MinimumFontSize = 12f; 

      //Setting Boarder and curved edges 
      sampleTextField2.Layer.BorderWidth = 1f; 
      sampleTextField2.Layer.CornerRadius = 5f; 
      sampleTextField2.Layer.BorderColor = UIColor.Black.CGColor; 

      //Shadows 
      sampleTextField2.Layer.ShadowColor = UIColor.DarkGray.CGColor; 
      sampleTextField2.Layer.ShadowOffset = new CoreGraphics.CGSize (3f, 3f); 
      sampleTextField2.Layer.ShadowOpacity = 0.3f; 
      sampleTextField2.Layer.ShadowRadius = 3f; 

      this.View.AddSubview (sampleTextField1); 
      this.View.AddSubview (sampleTextField2); 

      //Adding Delegate Methods 
      this.enableUITextFieldDelegateMethods(); 

     } 
void enableUITextFieldDelegateMethods() 
     { 
      this.sampleTextField1.ShouldReturn += (textField) => { 
       sampleTextField2.BecomeFirstResponder(); 
       textField.ResignFirstResponder(); 
       return true; 
      }; 

      this.sampleTextField1.ShouldBeginEditing += (textField) => { 
       //Write here 
       return true; 
      }; 

      this.sampleTextField1.ShouldEndEditing += (textField) => { 
       //Write here 
       return true; 
      }; 
      this.sampleTextField1.ShouldClear += (textField) => { 
       //Write here 
       return true; 
      }; 

      this.sampleTextField1.ShouldChangeCharacters = (UITextField txt, NSRange range, string sampleTxt) => { 
       var newLength = txt.Text.Length + sampleTxt.Length - range.Length; 
       return newLength <= 9; 

      }; 
     } 
Powiązane problemy