uialertview的使用方法

上传人:第*** 文档编号:31774876 上传时间:2018-02-09 格式:DOCX 页数:7 大小:111.66KB
返回 下载 相关 举报
uialertview的使用方法_第1页
第1页 / 共7页
uialertview的使用方法_第2页
第2页 / 共7页
uialertview的使用方法_第3页
第3页 / 共7页
uialertview的使用方法_第4页
第4页 / 共7页
uialertview的使用方法_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《uialertview的使用方法》由会员分享,可在线阅读,更多相关《uialertview的使用方法(7页珍藏版)》请在金锄头文库上搜索。

1、 UIAlertView 的使用方法1. 最简单的用法UIAlertView*alert = UIAlertView allocinitWithTitle:提示 message:这是一个简单的警告框! delegate:nil cancelButtonTitle:确定 otherButtonTitles:nil; alert show; alert release; 2. 为 UIAlertView 添加多个按钮UIAlertView*alert = UIAlertView allocinitWithTitle:提示 message:请选择一个按钮: delegate:nil cancelBu

2、ttonTitle:取消 otherButtonTitles:按钮一, 按钮二, 按钮三,nil; alert show; alert release; 3. 如何判断用户点击的按钮UIAlertView 有一个委托 UIAlertViewDelegate ,继承该委托来实现点击事件头文件:interface MyAlertViewViewController : UIViewController - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;-(IBAction)

3、buttonPressed;end源文件:-(IBAction) buttonPressedUIAlertView*alert = UIAlertView allocinitWithTitle:提示 message:请选择一个按钮: delegate:self cancelButtonTitle:取消 otherButtonTitles:按钮一, 按钮二, 按钮三,nil; alert show; alert release; - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonInd

4、exNSString* msg = NSString alloc initWithFormat:您按下的第%d 个按钮!,buttonIndex;UIAlertView* alert = UIAlertView allocinitWithTitle:提示message:msgdelegate:nilcancelButtonTitle:确定otherButtonTitles:nil;alert show;alert release;msg release;点击“取消” ,“按钮一”,“按钮二”,“ 按钮三”的索引 buttonIndex 分别是 0,1,2,34. 手动的取消对话框alertdi

5、smissWithClickedButtonIndex:0 animated:YES;5:为 UIAlertView 添加子视图在为 UIAlertView 对象太添加子视图的过程中,有点是需要注意的地方,如果删除按钮,也就是取消 UIAlerView 视图中所有的按钮的时候,可能会导致整个显示结构失衡。按钮占用的空间不会消失,我们也可以理解为这些按钮没有真正的删除,仅仅是他不可见了而已。如果在 UIAlertview 对象中仅仅用来显示文本,那么,可以在消息的开头添加换行符(n )有助于平衡按钮底部和顶部的空间。下面的代码用来演示如何为 UIAlertview 对象添加子视图的方法。UIAl

6、ertView*alert = UIAlertView allocinitWithTitle:请等待 message:nildelegate:nil cancelButtonTitle:nil otherButtonTitles:nil; alert show;UIActivityIndicatorView*activeView = UIActivityIndicatorView allocinitWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge;activeView.center = CGPointMake(a

7、lert.bounds.size.width/2.0f, alert.bounds.size.height-40.0f); activeView startAnimating; alert addSubview:activeView; activeView release; alert release; 6. UIAlertView 默认情况下所有的 text 是居中对齐的。 那如果需要将文本向左对齐或者添加其他控件比如输入框时该怎么办呢? 不用担心, iPhone SDK 还是很灵活的, 有很多 delegate 消息供调用程序使用。 所要做的就是在- (void)willPresentAl

8、ertView:(UIAlertView *)alertView中按照自己的需要修改或添加即可, 比如需要将消息文本左对齐,下面的代码即可实现:-(void) willPresentAlertView:(UIAlertView *)alertViewfor( UIView * view in alertView.subviews )if( view isKindOfClass:UILabel class )UILabel* label = (UILabel*) view;label.textAlignment=UITextAlignmentLeft; 这段代码很简单, 就是在消息框即将弹出时,

9、遍历所有消息框对象,将其文本对齐属性修改为 UITextAlignmentLeft 即可。添加其他部件也如出一辙, 如下代码添加两个 UITextField:-(void) willPresentAlertView:(UIAlertView *)alertViewCGRect frame = alertView.frame;frame.origin.y -= 120;frame.size.height += 80;alertView.frame = frame;for( UIView * viewin alertView.subviews )if( !viewisKindOfClass:UIL

10、abelclass )CGRect btnFrame = view.frame; btnFrame.origin.y += 70;view.frame = btnFrame;UITextField* accoutName = UITextFieldalloc init;UITextField* accoutPassword = UITextFieldalloc init;accoutName.frame = CGRectMake( 10, frame.origin.y + 40,frame.size.width - 20, 30 );accoutPassword.frame = CGRectM

11、ake( 10, frame.origin.y + 80,frame.size.width -20, 30 );accoutName.placeholder = 请输入账号;accoutPassword.placeholder = 请输入密码;accoutPassword.secureTextEntry = YES;alertView addSubview:accoutPassword;alertView addSubview:accoutName;accoutName release;accoutPassword release;显示将消息框固有的 button 和 label 移位, 不然添加的 text field 会将其遮盖住。 然后添加需要的部件到相应的位置即可。对于 UIActionSheet 其实也是一样的, 在- (void)willPresentActionSheet:(UIActionSheet *)actionSheet中做同样的处理一样可以得到自己想要的界面。

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 工业设计

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号