简述UIAlertView的属性和用法
1.Title
獲取或設(shè)置UIAlertView上的標(biāo)題。
2.Message
獲取或設(shè)置UIAlertView上的消息
??? UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
??? alertView.title = @"T";
??? alertView.message = @"M";
?
??? [alertView show];
?
3.numberOfButtons (只讀)
返回UIAlertView上有多少按鈕.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
??? NSLog(@"%d",alertView.numberOfButtons);
[alertView show];
?
?
4.cancelButtonIndex
?? UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"?message:@"請選擇一個(gè)按 鈕:"?delegate:nil?cancelButtonTitle:@"取消"?otherButtonTitles:@"按鈕一", @"按鈕二", @"按鈕三",nil];
?? [alert show];
NSLog(@"UIAlertView中取消按鈕的角標(biāo)是%d",alert.cancelButtonIndex);
效果:
?
注意不要認(rèn)為取消按鈕的角標(biāo)是4,“取消”,“按鈕一”,“按鈕二”,“按鈕三”的索引buttonIndex分別是0,1,2,3
?
?
5. alertViewStyle
5.1 UIAlertViewStyleLoginAndPasswordInput
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"產(chǎn)品信息展示" message:p.name delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
???
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
???
// 彈出UIAlertView
?[alert show];
?
?
?
5.2 UIAlertViewStylePlainTextInput
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"產(chǎn)品信息展示" message:p.name delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
???
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
???
// 彈出UIAlertView
?[alert show];
?
?
5.3 UIAlertViewStyleSecureTextInput
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"產(chǎn)品信息展示" message:p.name delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
???
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
???
// 彈出UIAlertView
?[alert show];
6. - (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex
返回textFieldIndex角標(biāo)對應(yīng)的文本框。
取出文本框文字
7.手動(dòng)的取消對話框
[alert dismissWithClickedButtonIndex:0?animated:YES];
?
8. delegate
作為UIAlertView的代理,必須遵守UIAlertViewDelegate。
1.當(dāng)點(diǎn)擊UIAlertView上的按鈕時(shí),就會調(diào)用,并且當(dāng)方法調(diào)完后,UIAlertView會自動(dòng)消失。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
?
2.當(dāng)UIAlertView即將出現(xiàn)的時(shí)候調(diào)用
- (void)willPresentAlertView:(UIAlertView *)alertView;
3. 當(dāng)UIAlertView完全出現(xiàn)的時(shí)候調(diào)用
- (void)didPresentAlertView:(UIAlertView *)alertView;?
?
4. 當(dāng)UIAlertView即將消失的時(shí)候調(diào)用
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;
5. 當(dāng)UIAlertView完全消失的時(shí)候調(diào)用
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;?
?
9.注意UIAlertView調(diào)用show顯示出來的時(shí)候,系統(tǒng)會自動(dòng)強(qiáng)引用它,不會被釋放。
10. 為UIAlertView添加子視圖
在為UIAlertView對象太添加子視圖的過程中,有點(diǎn)是需要注意的地方,如果刪除按鈕,也就是取消UIAlerView視圖中所有的按鈕的時(shí) 候,可能會導(dǎo)致整個(gè)顯示結(jié)構(gòu)失衡。按鈕占用的空間不會消失,我們也可以理解為這些按鈕沒有真正的刪除,僅僅是他不可見了而已。如果在 UIAlertview對象中僅僅用來顯示文本,那么,可以在消息的開頭添加換行符(@"\n)有助于平衡按鈕底部和頂部的空間。
下面的代碼用來演示如何為UIAlertview對象添加子視圖的方法。
UIAlertView*alert = [[UIAlertView?alloc]initWithTitle:@"請等待"?message:nil?delegate:nil ?cancelButtonTitle:nil?otherButtonTitles:nil]; ?
[alert?show];
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]; ?
?
?11. UIAlertView小例子
??UIAlertView默認(rèn)情況下所有的text是居中對齊的。 那如果需要將文本向左對齊或者添加其他控件比如輸入框時(shí)該怎么辦呢? 不用擔(dān)心, iPhone SDK還是很靈活的, 有很多delegate消息供調(diào)用程序使用。 所要做的就是在
- (void)willPresentAlertView:(UIAlertView *)alertView
中按照自己的需要修改或添加即可, 比如需要將消息文本左對齊,下面的代碼即可實(shí)現(xiàn):
-(void) willPresentAlertView:(UIAlertView?*)alertView
{
??????for(?UIView?*?view?in?alertView.subviews?)
??????{
????????????if( [view?isKindOfClass:[UILabel?class]] )
????????????{
??????????????????UILabel* label = (UILabel*) view;
??????????????????label.textAlignment=UITextAlignmentLeft;
????????????}
??????}
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/feife/p/4630323.html
總結(jié)
以上是生活随笔為你收集整理的简述UIAlertView的属性和用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中汉诺塔如何理解_pytho
- 下一篇: 用友T+标准版如何反结账,反记账