iphone開發 UIActionSheet(動作表) 和UIAlertView(警告)的用法

來源:互聯網
上載者:User

一.UIActionSheet(動作表) 和UIAlertView(警告)
 
UIActionSheet用於迫使使用者在兩個或更多的選項之間進行選擇的模式視圖。動作表從螢幕底部彈出,顯示一系列按鈕供使用者選擇,使用者只有單擊一個按鈕後才能繼續使用應用程式。
UIAlertView警告以藍色圓角矩形形式出現在螢幕中部,警報可顯示一個或多個按鈕
為了讓控制器類充當動作表的委託,控制器需要遵從UIActionSheetDelegate協議
 
二.UIActionSheet(動作表)的建立
 
initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:
 
注釋:initWithTitle是對象函數
            initWithTitle:動作表標題
            delegate:接收對象
     cancelButtonTitle關閉按鈕標題
            destructiveButtonTitle: 操作按鈕標題
            otherButtonTitles:其他按鈕
 
1.   建立一個簡單的動作表:
    //open a dialog with an OK and cancel button
    UIActionSheet*actionSheet = [[UIActionSheet alloc] 
                  initWithTitle:@" UIActionSheet<title>"
                   delegate:self  
                  cancelButtonTitle:@"Cancel"
                  destructiveButtonTitle:@"OK"
                    otherButtonTitles:nil];
 
    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
 
    [actionSheetshowInView:self.view]; //show from our table view (pops up in the middle of the table)
 
    [actionSheetrelease];

2.    建立兩個按鈕的動作表

            // open a dialog with an OKand cancel button

    UIActionSheet*actionSheet = [[UIActionSheet alloc]

                   initWithTitle:@" UIActionSheet<title>"

                    delegate:self

                   cancelButtonTitle:@"Cancel"

                    destructiveButtonTitle:@"OK"

                   otherButtonTitles:nil];

 

    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

    [actionSheetshowInView:self.view]; //show from our table view (pops up in the middle of the table)

    [actionSheetrelease];

 

3.   建立自訂動作表:

 

    //open a dialog with two custom buttons

    UIActionSheet*actionSheet = [[UIActionSheet alloc]

                   initWithTitle:@" UIActionSheet<title>"

                    delegate:self

                   cancelButtonTitle:nil

                    destructiveButtonTitle:nil

                otherButtonTitles:@"Button1", @"Button2", nil];

 

    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

 

    actionSheet.destructiveButtonIndex = 1; // make the second button red(destructive)

 

    [actionSheetshowInView:self.view]; //show from our table view (pops up in the middle of the table)

 

   [actionSheet release];

 

對動作表中按鈕的操作用UIActionSheetDelegate協議函數,這裡你可以控制在點擊按鈕之後的操作

#pragma mark - UIActionSheetDelegate

 

- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    //the user clicked one of the OK/Cancel buttons

    if(buttonIndex == 0)

    {

        //NSLog(@"ok");

    }

    else

    {

        //NSLog(@"cancel");

    }

}

三.UIAlertView(警告)的建立

 

      initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:

   注釋:initWithTitle是對象函數

               initWithTitle:警告框標題

               message:警告內容

               delegate:接收對象

               cancelButtonTitle:關閉按鈕標題

               otherButtonTitles:其他按鈕

 

1.    建立一個簡單的警告框

 

    //open an alert with just an OK button

    UIAlertView*alert = [[UIAlertView alloc]

                           initWithTitle:@"UIAlertView"

                          message:@"<Alertmessage>"

                        delegate:self

                           cancelButtonTitle:@"OK"

                           otherButtonTitles: nil];

 

    [alert show];//顯示警告框

    [alert release];//釋放警告框

 

 

2.    建立兩個按鈕的警告框

     // open a alert with an OK andcancel button

    UIAlertView*alert = [[UIAlertView alloc]

                           initWithTitle:@"UIAlertView"

                           message:@"<Alertmessage>"

                        delegate:self

                           cancelButtonTitle:@"Cancel"

                           otherButtonTitles:@"OK", nil];

 

    [alert show];//顯示警告框

    [alert release];//釋放警告框

 

3.  建立自訂警告框

    //open an alert with two custom buttons

    UIAlertView*alert = [[UIAlertView alloc]

                           initWithTitle:@"UIAlertView"

                           message:@"<Alertmessage>"

                        delegate:self

                           cancelButtonTitle:@"Cancel"

                otherButtonTitles:@"Button1",@"Button2", nil];

 

      

[alert show];//顯示警告框

[alert release];//釋放警告框

 

4   警告框中按鈕的操作

對警告框中按鈕的操作用UIAlertViewDelegate協議函數,這裡你可以控制在點擊按鈕之後的操作

#pragma mark - UIAlertViewDelegate

 

- (void)alertView:(UIAlertView*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    //use "buttonIndex" to decide your action

    //

}

 

5   給警告框添加子視圖:

 

    UIAlertView*alert = [[UIAlertView alloc]     initWithTitle:@"UIAlertView " 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];

 

摘自 decemberd的專欄


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.