在一个页面之上生成并插入新页面的代码
- (UIViewController *)presentingController { if (!presentingController) { presentingController = [[ASIAutorotatingViewController alloc] initWithNibName:nil bundle:nil]; // Attach to the window, but don't interfere. UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0]; [window addSubview:[presentingController view]]; [[presentingController view] setFrame:CGRectZero]; [[presentingController view] setUserInteractionEnabled:NO]; } return presentingController; }
插入view需要有viewcontroller
下面代码是显示登录对话框
- (void)showTitle { UINavigationBar *navigationBar = [[[self view] subviews] objectAtIndex:0]; UINavigationItem *navItem = [[navigationBar items] objectAtIndex:0]; if (UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) { // Setup the title if ([self type] == ASIProxyAuthenticationType) { [navItem setPrompt:@"Login to this secure proxy server."]; } else { [navItem setPrompt:@"Login to this secure server."]; } } else { [navItem setPrompt:nil]; } [navigationBar sizeToFit]; CGRect f = [[self view] bounds]; f.origin.y = [navigationBar frame].size.height; f.size.height -= f.origin.y; [[self tableView] setFrame:f]; } - (void)show { // Remove all subviews 要显示输入密码的对话框时需要移除其他页面 UIView *v; while ((v = [[[self view] subviews] lastObject])) { [v removeFromSuperview]; } // Setup toolbar 放置导航栏及导航栏的item UINavigationBar *bar = [[[UINavigationBar alloc] init] autorelease]; [bar setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; UINavigationItem *navItem = [[[UINavigationItem alloc] init] autorelease]; bar.items = [NSArray arrayWithObject:navItem]; [[self view] addSubview:bar]; [self showTitle]; // Setup toolbar buttons if ([self type] == ASIProxyAuthenticationType) { [navItem setTitle:[[self request] proxyHost]]; } else { [navItem setTitle:[[[self request] url] host]]; } //一个是取消,另一个是登录 [navItem setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelAuthenticationFromDialog:)] autorelease]]; [navItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStyleDone target:self action:@selector(loginWithCredentialsFromDialog:)] autorelease]]; // We show the login form in a table view, similar to Safari's authentication dialog [bar sizeToFit]; //设置表格的大小 CGRect f = [[self view] bounds]; f.origin.y = [bar frame].size.height; f.size.height -= f.origin.y; //放置表格 [self setTableView:[[[UITableView alloc] initWithFrame:f style:UITableViewStyleGrouped] autorelease]]; [[self tableView] setDelegate:self]; [[self tableView] setDataSource:self]; [[self tableView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; [[self view] addSubview:[self tableView]]; // Force reload the table content, and focus the first field to show the keyboard [[self tableView] reloadData]; //第一个输入框作为firstresponse,响应输入 [[[[[self tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]].contentView subviews] objectAtIndex:0] becomeFirstResponder]; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { [self setModalPresentationStyle:UIModalPresentationFormSheet]; } [[self presentingController] presentModalViewController:self animated:YES]; }
//输入用户名和密码的表格 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //设定为可重用单元 UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; //选中时没有效果 [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; CGRect f = CGRectInset([cell bounds], 10, 10); UITextField *textField = [[[UITextField alloc] initWithFrame:f] autorelease]; [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; [textField setAutocapitalizationType:UITextAutocapitalizationTypeNone]; [textField setAutocorrectionType:UITextAutocorrectionTypeNo]; NSUInteger s = [indexPath section]; NSUInteger r = [indexPath row]; //还未输入时提示语“User”和"Password" if (s == kUsernameSection && r == kUsernameRow) { [textField setPlaceholder:@"User"]; } else if (s == kPasswordSection && r == kPasswordRow) { [textField setPlaceholder:@"Password"]; //设置为密码输入的textfield [textField setSecureTextEntry:YES]; } else if (s == kDomainSection && r == kDomainRow) { [textField setPlaceholder:@"Domain"]; } [cell.contentView addSubview:textField]; return cell; }
弹出输入用户名、密码对话框的代码
- (void)authenticationNeededForRequest:(ASIHTTPRequest *)theRequest { UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Please Login" message:[request authenticationRealm] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil] autorelease]; // These are undocumented, use at your own risk! // A better general approach would be to subclass UIAlertView, or just use ASIHTTPRequest's built-in dialog [alertView addTextFieldWithValue:@"" label:@"Username"]; [alertView addTextFieldWithValue:@"" label:@"Password"]; [alertView show]; }