如何在选择器行中定义用户名和密码

时间:2013-06-18 03:07:03

标签: ios objective-c

我有一个选择器5行(0, 1, 2, ...5),我想定义我是否选择row:0并点击按钮

“登录”将显示alertView以添加用户名和密码,其中用户名和密码在行中:0

只有当我在alertView中单击“登录”时,

必须是A和A转到(模态)查看:A但是用户名和

密码不是A和A显示alertView“用户名或密码错误”

和行:1从行开始工作:0但是用户名和密码必须是B和B而且

转到(模态)查看:B不查看:A
有什么建议吗?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    _Data = [[NSArray alloc] initWithObjects:@"Place1", @"Place2", @"Place3", @"Place4", @"Place5", @"Place6", nil]; 
}


- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
return 1; 
} 

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    return [_Data count]; 
} 

-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    return [_Data objectAtIndex:row]; 
} 

- (IBAction)ShowID:(id)sender 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please Login" message:@"Enter    Username and Password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Login", nil]; 
    [alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; 
    [alert show]; 
}

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSDictionary* dict1 = [NSDictionary dictionaryWithObjectsAndKeys: @"Place1", @"place", @"TSViewController1", @"vcclass", @"User1", @"userName", @"qwerty", @"password", nil];
    NSDictionary* dict2 = [NSDictionary dictionaryWithObjectsAndKeys: @"Place1", @"place", @"TSViewController1", @"vcclass", @"User2", @"userName", @"wwwrtt", @"password", nil];
    _Data = [[NSArray alloc] initWithObjects: dict1, dict2, nil];

}

-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [[_Data objectAtIndex: row] objectForKey: @"place"];
}

- (IBAction)ShowID:(id)sender
{
    NSInteger indexPicker = [_pickerView selectedRowInComponent: 0];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Login"
                                                    message: [NSString stringWithFormat: @"Enter Username and Password for %@", [_Data objectAtIndex: indexPicker]]
                                                   delegate: self
                                          cancelButtonTitle: @"Cancel"
                                          otherButtonTitles: @"Login", nil];

    [alert setAlertViewStyle: UIAlertViewStyleLoginAndPasswordInput];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"Login"])
    {
        NSInteger indexPicker = [_pickerView selectedRowInComponent: 0];

        NSString* username = [[_Data objectAtIndex: indexPicker] objectForKey: @"userName"];
        NSString* password = [[_Data objectAtIndex: indexPicker] objectForKey: @"password"];
        NSString* yourUsername = [alertView textFieldAtIndex:0].text;
        NSString* yourpassword = [alertView textFieldAtIndex:1].text;

        if ([username isEqualToString: yourUsername] && [yourpassword isEqualToString: password])
        {
            NSString* viewControllerClassName = [[_Data objectAtIndex: indexPicker] objectForKey: @"vcclass"];
            Class classOfViewController = NSClassFromString(viewControllerClassName);
        UIViewController* viewController = [[classOfViewController alloc] init];
            [self presentModalViewController: viewController
                                animated: YES];

            // or [self.navigationController pushViewController:viewController animated:YES];
        }
    }
}

在.h:

@interface ...
{
    NSArray* _DataLogin;
    NSArray* _Data;
    UIPickerView* _pickerView;
}

然后你应该创建自定义视图控制器。您也可以使用presentModalViewController而不是pushViewController。