国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS中UIAlertView警告框組件的使用教程

2019-10-21 18:54:09
字體:
供稿:網(wǎng)友
需要注意的是在IOS8之后,UIAlertView和UIActionSheet這兩個(gè)控件被UIAlertController代替,但是這兩個(gè)控件依然可以使,下面我們就簡(jiǎn)單了解一下iOS中UIAlertView警告框組件的使用教程
 

1. 最簡(jiǎn)單的用法
初始化方法:

復(fù)制代碼代碼如下:

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;

這個(gè)方法通過設(shè)置一個(gè)標(biāo)題,內(nèi)容,代理和一些按鈕的標(biāo)題創(chuàng)建警告框,代碼示例如下:
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"我的警告框" message:@"這是一個(gè)警告框" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
    [alert show];
效果如下:

 

iOS,UIAlertView,警告框

注意:如果按鈕數(shù)超過兩個(gè),將會(huì)創(chuàng)建成如下樣子:

iOS,UIAlertView,警告框

如果按鈕數(shù)量超出屏幕顯示范圍,則會(huì)創(chuàng)建類似tableView的效果。

2. 為UIAlertView添加多個(gè)按鈕

復(fù)制代碼代碼如下:

UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"

 

                                                  message:@"請(qǐng)選擇一個(gè)按鈕:"

                                                  delegate:nil  

                                                  cancelButtonTitle:@"取消"

                                                  otherButtonTitles:@"按鈕一", @"按鈕二", @"按鈕三",nil]; 

[alert show]; 

[alert release]; 

 

3. 如何判斷用戶點(diǎn)擊的按鈕
UIAlertView有一個(gè)委托(代理)UIAlertViewDelegate ,繼承該委托來實(shí)現(xiàn)點(diǎn)擊事件

 頭文件:

復(fù)制代碼代碼如下:

@interface MyAlertViewViewController : UIViewController {

 

}

(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
-(IBAction) buttonPressed;

@end


源文件:
復(fù)制代碼代碼如下:

-(IBAction) buttonPressed

 

{

UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"

                                                 message:@"請(qǐng)選擇一個(gè)按鈕:"

                                                 delegate:self  

                                                 cancelButtonTitle:@"取消"

                                                 otherButtonTitles:@"按鈕一", @"按鈕二", @"按鈕三",nil]; 

[alert show]; 

[alert release];

}

(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

NSString* msg = [[NSString alloc] initWithFormat:@"您按下的第%d個(gè)按鈕!",buttonIndex];

UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示"

                                                 message:msg

                                                 delegate:nil

                                                 cancelButtonTitle:@"確定"

                                                 otherButtonTitles:nil];

[alert show];

[alert release];

[msg release];

}


點(diǎn)擊“取消”,“按鈕一”,“按鈕二”,“按鈕三”的索引buttonIndex分別是0,1,2,3

 

4. 手動(dòng)的取消對(duì)話框

復(fù)制代碼代碼如下:

[alertdismissWithClickedButtonIndex:0 animated:YES];

 

5. 為UIAlertView添加子視圖
在為UIAlertView對(duì)象太添加子視圖的過程中,有點(diǎn)是需要注意的地方,如果刪除按鈕,也就是取消UIAlerView視圖中所有的按鈕的時(shí)候,可能會(huì)導(dǎo)致整個(gè)顯示結(jié)構(gòu)失衡。按鈕占用的空間不會(huì)消失,我們也可以理解為這些按鈕沒有真正的刪除,僅僅是他不可見了而已。如果在UIAlertview對(duì)象中僅僅用來顯示文本,那么,可以在消息的開頭添加換行符(@"/n)有助于平衡按鈕底部和頂部的空間。

下面的代碼用來演示如何為UIAlertview對(duì)象添加子視圖的方法。

復(fù)制代碼代碼如下:

UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"請(qǐng)等待"

 

                                                 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默認(rèn)情況下所有的text是居中對(duì)齊的。 那如果需要將文本向左對(duì)齊或者添加其他控件比如輸入框時(shí)該怎么辦呢? 不用擔(dān)心, iPhone SDK還是很靈活的, 有很多delegate消息供調(diào)用程序使用。 所要做的就是在

復(fù)制代碼代碼如下:

(void)willPresentAlertView:(UIAlertView *)alertView

中按照自己的需要修改或添加即可, 比如需要將消息文本左對(duì)齊,下面的代碼即可實(shí)現(xiàn):
復(fù)制代碼代碼如下:

-(void) willPresentAlertView:(UIAlertView *)alertView

 

{

      for( UIView * view in alertView.subviews )

      {

            if( [view isKindOfClass:[UILabel class]] )

            {

                  UILabel* label = (UILabel*) view;

                  label.textAlignment=UITextAlignmentLeft;

            }

      }

}


 這段代碼很簡(jiǎn)單, 就是在消息框即將彈出時(shí),遍歷所有消息框?qū)ο螅瑢⑵湮谋緦?duì)齊屬性修改為 UITextAlignmentLeft即可。

 

添加其他部件也如出一轍, 如下代碼添加兩個(gè)UITextField:

復(fù)制代碼代碼如下:

-(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 = @"請(qǐng)輸入賬號(hào)";

accoutPassword.placeholder = @"請(qǐng)輸入密碼";

accoutPassword.secureTextEntry = YES;

[alertView addSubview:accoutPassword];

[alertView addSubview:accoutName];

[accoutName release];

[accoutPassword release];

}


顯示將消息框固有的button和label移位, 不然添加的text field會(huì)將其遮蓋住。 然后添加需要的部件到相應(yīng)的位置即可。

 

對(duì)于UIActionSheet其實(shí)也是一樣的, 在

復(fù)制代碼代碼如下:

(void)willPresentActionSheet:(UIActionSheet *)actionSheet

中做同樣的處理一樣可以得到自己想要的界面。
 

注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久治县| 中阳县| 庆元县| 宝应县| 云龙县| 鹤岗市| 虎林市| 东辽县| 南和县| 大宁县| 开鲁县| 台前县| 二连浩特市| 云浮市| 温泉县| 侯马市| 都昌县| 上犹县| 寻乌县| 布拖县| 黔西| 新和县| 施甸县| 长沙市| 潢川县| 夏邑县| 邯郸县| 盐池县| 抚州市| 宁南县| 阿拉善盟| 吉安县| 台湾省| 开原市| 丰县| 湖南省| 蛟河市| 图片| 开江县| 沂源县| 顺义区|