`
hzizh
  • 浏览: 8233 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

IPhone用户登录框的一些代码

阅读更多

在一个页面之上生成并插入新页面的代码

- (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];

}
 

分享到:
评论

相关推荐

    iPhone应用开发从入门到精通代码

    基本控件1639.1 UIView和控件1639.2 常用控件1649.3 自定义控件1669.4 警告窗口和用户响应1719.5 本章小结1739.6 本章练习173提 高 篇第10章 iPhone的高级表格程序17610.1 表格程序预览17610.1.1 表格的...

    Pro iPhone Development with Swift 5, 2nd Edition

    在本文中,您将学习如何组织和调试SWIFT代码、使用Grand Central Dispatp进行多线程编程、在视图控制器之间传递数据以及为多种语言设计应用程序的技巧。这是最畅销的iPhone开发入门版SWIFT的后续工作。您还将看到...

    Horizontal Menu(iPhone源代码)

    来源:Licence:未知平台:iOS设备:iPhone / iPad作者:Mugunth Kumar  实现水平样式的菜单(Horizontal Menu),类似的效果是Three20框架的menubar。菜单位于导航条下方,或者屏幕最下方,有许多按钮在菜单...

    (0054)-iOS/iPhone/iPAD/iPod源代码-选项卡(Tab Bar)-Horizontal Menu

    实现水平样式的菜单(Horizontal Menu),类似的效果是Three20框架的menubar。菜单位于导航条下方,...整个效果虽然和Three20框架的menubar差不多,但是实现十分简单,只有不到150行的代码。 注意:请在Mac下解压使用

    PopView With Slider(iPhone源代码)

    当用户手指移动滑块时,在滑块的上方弹出一个小文字框。适用于iPad和iPhone。 小编注:Code4App之前收集了一个类似效果的代码,但是仅用于iPad,现在,热心的开发者@挺风和日丽的啊 编写了这个适用于iPhone和iPad的...

    iphone开发入门经典源码

    iPhone开发入门经典》基于Apple最新发布的iPhoneOS3.0编写,循序渐进地介绍了从事iPhoneF发所需的基本知识,包括使用Xcode、Interface Builder、objective-C和(Cocoa Touch等开发工具,设计及美化用户界面,读写和...

    《iPhone开发实战》.(Christopher Allen).pdf

    3.7 支持非iphone用户38 3.8 小结39 第4章 高级webkit和文本web应用程序40 4.1 webkit简介40 4.1.1 新的html元素41 4.1.2 新的css元素41 4.2 css变换、过渡和动画43 4.2.1 变换函数43 4.2.2 过渡...

    iPhone开发秘籍.part2.rar

    iPhone 为用户提供了多种警告方式,从弹出对话框和进度栏到音频提示和状态栏更新。第4章展示如 何将这些指示功能构建到应用程序中,并扩展用户警告词汇库。 第5章:基本表格 表格提供了在小型受限设备上获得出色运行...

    iPhone开发秘籍.part4.rar

    iPhone 为用户提供了多种警告方式,从弹出对话框和进度栏到音频提示和状态栏更新。第4章展示如 何将这些指示功能构建到应用程序中,并扩展用户警告词汇库。 第5章:基本表格 表格提供了在小型受限设备上获得出色运行...

    iPhone开发秘籍.part1.rar

    iPhone 为用户提供了多种警告方式,从弹出对话框和进度栏到音频提示和状态栏更新。第4章展示如 何将这些指示功能构建到应用程序中,并扩展用户警告词汇库。 第5章:基本表格 表格提供了在小型受限设备上获得出色运行...

    JAVA上百实例源码以及开源项目源代码

    多人聊天+用户在线 21个目标文件 摘要:JAVA源码,媒体网络,山寨QQ,Java聊天程序 Java编写的山寨QQ,多人聊天+用户在线,程序分服务端和客户端,典型C/S结构, 当用户发送第一次请求的时候,验证用户登录,创建一个该...

    xmpp for ios(iPhone源代码)

    来源:Licence:Eclipse Public License平台:iOS设备:iPhone / iPad作者:Dawn_wdf  基于xmpp的即时通讯(需要配置openfire)。包括注册新用户,连接,获取朋友列表,文字通讯和语音通讯。其中保存朋友列表和...

    iPhone开发秘籍

    iPhone开发秘籍 The iPhone Developer's Cookbook Building Applications with the iPhone SDK 本电子书共295页 Amazon超级畅销书 大量未公开的绝技,带你深入iPhone开发秘境 任务驱动,丰富的实战代码,让你...

    MMReachabilityViewController(iPhone源代码)

    如果没有网络,有两种弹出框方式提示用户,具体请看效果Gif图。 小编注:这份代码的检测网络代码用的是Apple官方的代码例子Reachability。 [优才 · Code4App]编译测试,适用环境:Xcode 4.5, iOS 6.0 以上。

    iphone3开发基础教程

    12.5 一些OpenGL ES基础知识 325 12.5.1 构建GLFun应用程序 326 12.5.2 设计Nib、添加框架、运行应用程序 335 12.6 小结 335 第13章 轻击、触摸和手势 336 13.1 多触摸术语 336 13.2 响应者链 337 13.3 多触摸体系...

    仿iphone的listview下拉更新.zip

    我们的安卓项目源码还采用了一些常用的技术和工具,以提高开发效率和代码质量。例如,我们使用了Android Studio作为开发工具,它提供了一系列的功能和工具,如代码编辑器、调试器和模拟器,使得开发人员可以快速开发...

    iPhone应用程序开发指南.中文.pdf

    iPhone OS系统上的音频单元支持 145 iPhone音频的最佳实践 145 在iPhone OS使用视频 147 录制视频 147 播放视频文件 147 设备支持 150 确定硬件支持是否存在 150 和配件进行通讯 151 配件的基础 151 声明应用程序...

Global site tag (gtag.js) - Google Analytics