iOS中UIAlertView警告框組件的使用教程
1. 最簡單的用法
初始化方法:
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;
這個方法通過設(shè)置一個標題,內(nèi)容,代理和一些按鈕的標題創(chuàng)建警告框,代碼示例如下:
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"我的警告框" message:@"這是一個警告框" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
[alert show];
效果如下:
注意:如果按鈕數(shù)超過兩個,將會創(chuàng)建成如下樣子:
如果按鈕數(shù)量超出屏幕顯示范圍,則會創(chuàng)建類似tableView的效果。
2. 為UIAlertView添加多個按鈕
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"請選擇一個按鈕:"
delegate:nil
cancelButtonTitle:@"取消"
otherButtonTitles:@"按鈕一", @"按鈕二", @"按鈕三",nil];
[alert show];
[alert release];
3. 如何判斷用戶點擊的按鈕
UIAlertView有一個委托(代理)UIAlertViewDelegate ,繼承該委托來實現(xiàn)點擊事件
頭文件:
@interface MyAlertViewViewController : UIViewController {
}
(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
-(IBAction) buttonPressed;
@end
源文件:
-(IBAction) buttonPressed
{
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"請選擇一個按鈕:"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"按鈕一", @"按鈕二", @"按鈕三",nil];
[alert show];
[alert release];
}
(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視圖中所有的按鈕的時候,可能會導(dǎo)致整個顯示結(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];
[activeView release];
[alert release];
6. 其他
UIAlertView默認情況下所有的text是居中對齊的。 那如果需要將文本向左對齊或者添加其他控件比如輸入框時該怎么辦呢? 不用擔心, iPhone SDK還是很靈活的, 有很多delegate消息供調(diào)用程序使用。 所要做的就是在
(void)willPresentAlertView:(UIAlertView *)alertView
中按照自己的需要修改或添加即可, 比如需要將消息文本左對齊,下面的代碼即可實現(xiàn):
-(void) willPresentAlertView:(UIAlertView *)alertView
{
for( UIView * view in alertView.subviews )
{
if( [view isKindOfClass:[UILabel class]] )
{
UILabel* label = (UILabel*) view;
label.textAlignment=UITextAlignmentLeft;
}
}
}
這段代碼很簡單, 就是在消息框即將彈出時,遍歷所有消息框?qū)ο?,將其文本對齊屬性修改為 UITextAlignmentLeft即可。
添加其他部件也如出一轍, 如下代碼添加兩個UITextField:
-(void) willPresentAlertView:(UIAlertView *)alertView
{
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, 30 );
accoutName.placeholder = @"請輸入賬號";
accoutPassword.placeholder = @"請輸入密碼";
accoutPassword.secureTextEntry = YES;
[alertView addSubview:accoutPassword];
[alertView addSubview:accoutName];
[accoutName release];
[accoutPassword release];
}
顯示將消息框固有的button和label移位, 不然添加的text field會將其遮蓋住。 然后添加需要的部件到相應(yīng)的位置即可。
對于UIActionSheet其實也是一樣的, 在
(void)willPresentActionSheet:(UIActionSheet *)actionSheet
中做同樣的處理一樣可以得到自己想要的界面。
相關(guān)文章
iOS應(yīng)用開發(fā)中StoryBoard搭建UI界面的基本使用講解
這篇文章主要介紹了iOS應(yīng)用開發(fā)中StoryBoard搭建UI界面的基本使用,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-02-02iOS實現(xiàn)調(diào)用QQ客戶端發(fā)起臨時會話
本篇文章主要給大家詳細分析了用IOS實現(xiàn)調(diào)用QQ客戶端發(fā)起臨時會話的功能,對此有需要的朋友收藏分享下。2018-02-02iOS開發(fā)中使用UIScrollView實現(xiàn)無限循環(huán)的圖片瀏覽器
這篇文章主要介紹了iOS開發(fā)中使用UIScrollView實現(xiàn)無限循環(huán)的圖片瀏覽器的方法,感興趣的小伙伴們可以參考一下2016-03-03Unity3d發(fā)布IOS9應(yīng)用時出現(xiàn)中文亂碼的解決方法
這里給大家分享的是使用UNity3d發(fā)布IOS9應(yīng)用的時候,遇到出現(xiàn)中文亂碼的現(xiàn)象的解決方法,核心內(nèi)容非常簡單就是批量修改NGUI的label字體,下面把代碼奉上。2015-10-10iOS CAReplicatorLayer實現(xiàn)脈沖動畫效果
這篇文章主要介紹了iOS CAReplicatorLayer實現(xiàn)脈沖動畫效果 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06