本系列全部開發文檔翻譯連接地址:iOS7開發-Apple蘋果iPhone開發Xcode官方文檔翻譯PDF下載地址html
UIAlertViewpost
//轉載請註明出處--本文永久連接:http://www.cnblogs.com/ChenYilong/p/3495704.htmlurl
1.Title
獲取或設置UIAlertView上的標題。
2.Message
獲取或設置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];spa
//轉載請註明出處--本文永久連接:http://www.cnblogs.com/ChenYilong/p/3495704.html
4.cancelButtonIndex
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"請選擇一個按鈕:"
delegate:nil
cancelButtonTitle:@"取消"
otherButtonTitles:@"按鈕一", @"按鈕二", @"按鈕三",nil];
[alert show];
NSLog(@"UIAlertView中取消按鈕的角標是%d",alert.cancelButtonIndex);
效果:
注意不要認爲取消按鈕的角標是4,「取消」,「按鈕一」,「按鈕二」,「按鈕三」的索引buttonIndex分別是0,1,2,3
5. alertViewStyle
5.1 UIAlertViewStyleLoginAndPasswordInput
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"產品信息展現" message:p.name delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"肯定", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
// 彈出UIAlertView
[alert show];
5.2 UIAlertViewStylePlainTextInput
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"產品信息展現" message:p.name delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"肯定", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
// 彈出UIAlertView
[alert show];
//轉載請註明出處--本文永久連接:http://www.cnblogs.com/ChenYilong/p/3495704.html翻譯
5.3UIAlertViewStyleSecureTextInput
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"產品信息展現" message:p.name delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"肯定", nil];
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
// 彈出UIAlertView
[alert show];
6. - (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex
返回textFieldIndex角標對應的文本框。
取出文本框文字
7.手動的取消對話框
[alert dismissWithClickedButtonIndex:0 animated:YES];
//技術博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong
8. delegate
做爲UIAlertView的代理,必須遵照UIAlertViewDelegate。
1.當點擊UIAlertView上的按鈕時,就會調用,而且當方法調完後,UIAlertView會自動消失。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
2.當UIAlertView即將出現的時候調用
- (void)willPresentAlertView:(UIAlertView *)alertView;
3. 當UIAlertView徹底出現的時候調用
- (void)didPresentAlertView:(UIAlertView *)alertView;
4. 當UIAlertView即將消失的時候調用
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;
5. 當UIAlertView徹底消失的時候調用
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
//轉載請註明出處--本文永久連接:http://www.cnblogs.com/ChenYilong/p/3495704.html3d
9.注意UIAlertView調用show顯示出來的時候,系統會自動強引用它,不會被釋放。
10. 爲UIAlertView添加子視圖
在爲UIAlertView對象太添加子視圖的過程當中,有點是須要注意的地方,若是刪除按鈕,也就是取消UIAlerView視圖中全部的按鈕的時候,可能會致使整個顯示結構失衡。按鈕佔用的空間不會消失,咱們也能夠理解爲這些按鈕沒有真正的刪除,僅僅是他不可見了而已。若是在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默認狀況下全部的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;
}
}
}
//技術博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong代理
//轉載請註明出處--本文永久連接:http://www.cnblogs.com/ChenYilong/p/3495704.html code
本文對應pdf文檔下載連接,猛戳—>:https://www.evernote.com/shard/s227/sh/ff5a52fc-c82d-4686-86ff-850874ec73fb/28a5a4c877c7337666941d6b078c1b11htm
本系列全部開發文檔翻譯連接地址:iOS7開發-Apple蘋果iPhone開發Xcode官方文檔翻譯PDF下載地址對象