尝试这样的事情:
- (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。