1. 最简单的用法
UIAlertVIEw*alert = [[UIAlertVIEw alloc]initWithTitle:@"提示"
message:@"这是一个简单的警告框!"
delegate:nil
cancelbuttonTitle:@"确定"
otherbuttonTitles:nil];
[alert show];
[alert release];
2. 为UIAlertVIEw添加多个按钮
message:@"请选择一个按钮:"
delegate:nil
cancelbuttonTitle:@"取消"
otherbuttonTitles:@"按钮一", @"按钮二", @"按钮三",nil];
3. 如何判断用户点击的按钮
UIAlertVIEw有一个委托UIAlertVIEwDelegate,继承该委托来实现点击事件
头文件:
@interface MyAlertVIEwVIEwController : UIVIEwController<UIAlertVIEwDelegate> {
}
- (voID)alertVIEw:(UIAlertVIEw *)alertVIEw clickedbuttonAtIndex:(NSInteger)buttonIndex;
-(IBAction) buttonpressed;
@end
源文件:
-(IBAction) buttonpressed
{
message:@"请选择一个按钮:"
delegate:self
cancelbuttonTitle:@"取消"
otherbuttonTitles:@"按钮一",68); Font-family:Arial; Font-size:14px">- (voID)alertVIEw:(UIAlertVIEw *)alertVIEw clickedbuttonAtIndex:(NSInteger)buttonIndex
Nsstring* msg = [[Nsstring alloc] initWithFormat:@"您按下的第%d个按钮!",buttonIndex];
UIAlertVIEw* alert = [[UIAlertVIEw alloc]initWithTitle:@"提示"
message:msg
delegate:nil
cancelbuttonTitle:@"确定"
otherbuttonTitles:nil];
[alert show];
[alert release];
[msg release];
点击“取消”,“按钮一”,“按钮二”,“按钮三”的索引buttonIndex分别是0,1,2,3
4. 手动的取消对话框
[alertdismissWithClickedbuttonIndex:0 animated:YES];
5:为UIAlertVIEw添加子视图
在为UIAlertVIEw对象太添加子视图的过程中,有点是需要注意的地方,如果删除按钮,也就是取消UIAlerVIEw视图中所有的按钮的时候,可能会导致整个显示结构失衡。按钮占用的空间不会消失,我们也可以理解为这些按钮没有真正的删除,仅仅是他不可见了而已。如果在UIAlertvIEw对象中仅仅用来显示文本,那么,可以在消息的开头添加换行符(@"\n)有助于平衡按钮底部和顶部的空间。
下面的代码用来演示如何为UIAlertvIEw对象添加子视图的方法。
UIAlertVIEw*alert = [[UIAlertVIEw alloc]initWithTitle:@"请等待"
message:nil
delegate:nil
cancelbuttonTitle:nil
otherbuttonTitles:nil];
UIActivityIndicatorVIEw*activeVIEw = [[UIActivityIndicatorVIEw alloc]initWithActivityIndicatorStyle:UIActivityIndicatorVIEwStyleWhiteLarge];
activeVIEw.center = CGPointMake(alert.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)willPresentAlertVIEw:(UIAlertVIEw *)alertVIEw中按照自己的需要修改或添加即可, 比如需要将消息文本左对齐,下面的代码即可实现:
-(voID) willPresentAlertVIEw:(UIAlertVIEw *)alertVIEw
for( UIVIEw * vIEw in alertVIEw.subvIEws )
{
if( [vIEw isKindOfClass:[UILabel class]] )
{
UILabel* label = (UILabel*) vIEw;
label.textAlignment=UITextAlignmentleft;
}
}
这段代码很简单, 就是在消息框即将d出时,遍历所有消息框对象,将其文本对齐属性修改为 UITextAlignmentleft即可。
添加其他部件也如出一辙, 如下代码添加两个UITextFIEld:
CGRect frame = alertVIEw.frame;
frame.origin.y -= 120;
frame.size.height += 80;
alertVIEw.frame = frame;
for( UIVIEw * vIEwin alertVIEw.subvIEws )
if( ![vIEwisKindOfClass:[UILabelclass]] )
{
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 = CGRectMake( 10,frame.origin.y + 80,frame.size.wIDth -20,68); Font-family:Arial; Font-size:14px">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
中做同样的处理一样可以得到自己想要的界面。
改变alertVIEw的显示位置
myAlertVIEw.transform = CGAffinetransformTransla
改变alertVIEw大小:
- (voID)willPresentAlertVIEw:(UIAlertVIEw *)alertVIEw {
}
UIAlertVIEw类似于C#中的模态对话框 或 MessageBox ,但是,ios中使用起来要麻烦得多。
下面这段代码是一段典型的应用:
UIAlertVIEw *alert =[[UIAlertVIEw alloc] initWithTitle:@"hello"
message:@"ipad,i come"
delegate:self
cancelbuttonTitle:@"ok"
otherbuttonTitles:nil];
[alert show];
[alert release];
但是,如果复杂一点,就麻烦了,如果上面加上几个按钮,如:
UIAlertVIEw *alert =[[UIAlertVIEw alloc] initWithTitle:@"hello"
message:@"ipad,i come"
delegate:self
cancelbuttonTitle:@"ok"
otherbuttonTitles:@"cancel",@"Ignore",nil ];
vIEw 中会显示3个按钮,那怎么知道用户选择了哪个按钮呢?
步骤如下:
1、在修改.h文件,添加对alertvIEw的处理,如下:
@interface pad4VIEwController : UIVIEwController
<UIAlertVIEwDelegate>
{.。。
2、在.m文件中添加对alertvIEw事件的响应,如下:
- (voID) alertVIEw:(UIAlertVIEw *)alertvIEw
clickedbuttonAtIndex:(NSInteger)buttonIndex{
NSLog(@"%@",alertvIEw.Title);
}
以上方法实现了当前.m中所有UIAlertVIEw的事件响应,alertvIEw指明是哪个vIEw,buttonIndex指明是哪介按钮。
UIActivityIndicatorVIEw的两种形式(转) (2012-04-10 12:20:37)转载
▼标签: ios uiactivity indicatorview 杂谈 | 分类:IOSUI |
//显示进度滚轮指示器
-(voID)showWaiting
progressInd=[[UIActivityIndicatorVIEwalloc]initWithActivityIndicato
progressInd.center=CGPointMake(self.vIEw.center.x,240);
[self.navigationController.vIEwaddSubvIEw:progressInd];
@H_273_502@ [progressIndstartAnimating];}
//消除滚动轮指示器
-(voID)hIDeWaiting
{
@H_273_502@ [progressIndstopAnimating];}
用法二:带有半透明背景的进度轮指示器。
//显示进度滚轮指示器
-(voID)showWaiting:(UIVIEw*)parent {
frame = CGRectMake((x - 70)/2,(y - height) / 2 + height, 80, 20);
UILabel *waitingLable = [[UILabelalloc] initWithFrame:frame];
waitingLable.text= @"Loading...";
waitingLable.textcolor= [UIcolorwhitecolor];
waitingLable.Font= [UIFontsystemFontOfSize:15];
waitingLable.backgroundcolor = [UIcolor clearcolor];
[theVIEw addSubvIEw:waitingLable];
[waitingLable release];
}
//消除滚动轮指示器
-(voID)hIDeWaiting
{
@H_273_502@}
总结以上是内存溢出为你收集整理的UIAlertView+UIActivityIndicatorView全部内容,希望文章能够帮你解决UIAlertView+UIActivityIndicatorView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)