2014-09-19 137 views
0

我是IOS中的新成員。當我點擊以下按鈕登錄:從ios中的另一個彈出按鈕執行彈出式菜單

enter image description here

我使用這個代碼來生成一個彈出:

- (UIView *)createLoginView 
{ 
    UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 130)]; 

    UITextField *email = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 280, 50)]; 
    email.tag=420; 
    email.layer.cornerRadius = 5; 
    [email setTag:99]; 
    [email setTextAlignment:NSTextAlignmentCenter]; 
    [email setKeyboardType:UIKeyboardTypeEmailAddress]; 
    [email setPlaceholder:@"Email"]; 
    [email setBackgroundColor:[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0]]; 
    [email setReturnKeyType:UIReturnKeyNext]; 
    [email setAutocorrectionType:UITextAutocorrectionTypeNo]; 

    UITextField *pass = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, 280, 50)]; 
    pass.layer.cornerRadius = 5; 
    [pass setTag:100]; 
    [pass setTextAlignment:NSTextAlignmentCenter]; 
    [pass setSecureTextEntry:YES]; 
    [pass setPlaceholder:@"Password"]; 
    [pass setBackgroundColor:[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0]]; 
    [pass setReturnKeyType:UIReturnKeyGo]; 

    [alertView addSubview:email]; 
    [alertView addSubview:pass]; 
    return alertView; 
} 

enter image description here

登錄彈出是否工作正常,但當我點擊'忘記'?彈出的按鈕,忘記密碼彈出窗口不顯示。這裏是忘記密碼彈出的代碼。

- (UIView *)createForgotPasswordView 
{ 
    UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 130)]; 

    UITextField *email = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 280, 50)]; 

    email.layer.cornerRadius = 5; 
    [email setTag:22]; 
    [email setTextAlignment:NSTextAlignmentCenter]; 
    [email setKeyboardType:UIKeyboardTypeEmailAddress]; 
    [email setPlaceholder:@"Email"]; 
    [email setBackgroundColor:[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0]]; 
    [email setReturnKeyType:UIReturnKeyNext]; 
    [email setAutocorrectionType:UITextAutocorrectionTypeNo]; 

    [alertView addSubview:email]; 
    return alertView; 
} 

而且我使用它像:

- (IBAction)LogInClick:(UIButton *)sender { 
    IOS7AlertView *applyMsg = [[IOS7AlertView alloc] init]; 
    [applyMsg setContainerView:[self createLoginView]]; 

    [applyMsg setButtonTitles:[NSMutableArray arrayWithObjects:@"Forgot?", @"Login", nil]]; 


    [applyMsg setOnButtonTouchUpInside:^(IOS7AlertView *alertView, int buttonIndex) { 

     if(buttonIndex==0) 
     { 

      [alertView close]; 

      NSLog(@"Forgot Section"); 

      IOS7AlertView *applyPop = [[IOS7AlertView alloc] init]; 
      [applyPop setParentView:[self createForgotPasswordView]]; 


      [applyPop setButtonTitles:[NSMutableArray arrayWithObjects:@"Next", @"Cancel", nil]]; 

     } 
}]; 

但問題是,在下一個彈出顯示不出來?問題是什麼?請幫忙。

+3

請注意,通過iOS 7,你不應該再添加子視圖'UIAlertView'。從文檔:'UIAlertView'類旨在按原樣使用,不支持子類。該類的視圖層次結構是私有的,不能修改。 – rckoenes 2014-09-19 08:59:18

+1

@rckoenes我不得不說'UIAlertView'我在哪裏是'UIAlertView'的子類,我更關心'IOS7AlertView'背後的代碼。我實際上看不到這個代碼的目的,因爲他們沒有做任何'UIAlertView'已經爲他們做的事情。 – Popeye 2014-09-19 09:16:43

+0

@Popeye你是完全正確的,沒有必要自己添加'UITextField'。我的評論更多地指向文檔中聲明de視圖層次是私有的部分。因此插入你自己的觀點可能會搞砸了。 – rckoenes 2014-09-19 09:27:13

回答

2

已經UIalertview層次結構有電子郵件和密碼選項。

試試這個,這是本地

- (IBAction)LogInClick:(UIButton *)sender { 
UIAlertView *alert = [[UIAlertView alloc] 
         initWithTitle:@"Login" 
         message:nil 
         delegate:self 
         cancelButtonTitle:@"Signup" 
         otherButtonTitles:@"Login",@"Forgot password", nil]; 

alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput; 

[[alert textFieldAtIndex:0]setKeyboardType:UIKeyboardTypeEmailAddress]; 
[[alert textFieldAtIndex:0]setPlaceholder:@"Email address"]; 
[[alert textFieldAtIndex:1]setPlaceholder:@"Password"]; 
alert.tag=10; 
[alert show]; 
} 

alert view delegate方法是

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 

    if (alertView.tag==65) 
    { 
    UIAlertView *alert = [[UIAlertView alloc] 
         initWithTitle:@"Login" 
         message:nil 
         delegate:self 
         cancelButtonTitle:@"Signup" 
         otherButtonTitles:@"Login",@"Forgot password", nil]; 

alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput; 

[[alert textFieldAtIndex:0]setKeyboardType:UIKeyboardTypeEmailAddress]; 
[[alert textFieldAtIndex:0]setPlaceholder:@"Email address"]; 
[[alert textFieldAtIndex:1]setPlaceholder:@"Password"]; 
alert.tag=10; 
[alert show]; 
} 


if (alertView.tag==10) 
{ 
    if (buttonIndex == 0) { 

     NSLog(@"signup"); 
     //Navigate to Signup page 



    } 
    else if (buttonIndex == 2) { 
     NSLog(@"Forgot password"); 

     // //Navigate to Forgot password page 
    } 
    else if (buttonIndex == 1) { 

     NSLog(@"YES"); 

     NSString *usernameInput=[alertView textFieldAtIndex:0].text; 

     NSString *passwordInput=[alertView textFieldAtIndex:1].text; 

     //pass the email id , password 

     NSLog(@"1 %@", usernameInput); 
     NSLog(@"2 %@", passwordInput); 

     NSString *emailReg = @"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 
     NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailReg]; 


     // COMMENT: check if input information is valid or not 
     if([usernameInput isEqualToString:@""]) 
     { 
      UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert!!!" message:@"You must fill in Email address" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
      alert.tag=65; 
      [alert show]; 


     } 
     else if([emailTest evaluateWithObject:usernameInput] == NO) 
     { 

      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert!!" message:@"Please Enter Valid Email Address." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      alert.tag=65; 
      [alert show]; 

     } 
     else if([passwordInput isEqualToString:@""]) 
     { 
      UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert!!!" message:@"You must fill in Password" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
      alert.tag=65; 
      [alert show]; 


     } 
     else 
     { 
      //pass teh data to server or login process 
      } 

    } 
     } 
+0

這是簡單的方法,不要忘記在你的頭文件中分配 2014-09-19 09:09:52

+0

@ Anbu.Kartik:其實我的登錄彈出窗口正在工作,但忘記密碼後點擊'foget?登錄彈出按鈕不會來。 – Poles 2014-09-19 09:35:23

+0

哦,如果您單擊忘記按鈕彈出窗口不出現,請更正 – 2014-09-19 09:42:07