0

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

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

只有当我在 alertView 中单击“登录”时才必须是 A 和 A 转到(模态)视图:A 但用户名和

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

和 row:1 从 row: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]; 
}
4

1 回答 1

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。

于 2013-06-18T04:46:42.917 回答