實作呼叫上浮選單UIActionSheet


1.實作一個按鈕,點擊按鈕實行如下

- (IBAction)toClick:(id)sender {

    UIActionSheet *myActionSheet = [[UIActionSheetalloc] 
    initWithTitle:@"actionSheet"
    delegate:self 
    cancelButtonTitle:@"Cancel"
    destructiveButtonTitle:@"Delete"
    otherButtonTitles:@"Save", @"Share", nil];

    [myActionSheet showInView:self.view]

};

2.遵循UIActionSheetDelegate , 壓住command + 滑鼠左鍵可查詢此delegate中可用的方法

   此範例中我們使用以下方法

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

3.實作UIActionSheetDelegate中的方法, 方法如下, 此方法可以讓我們得知點擊了哪個buttin,回傳button index值

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    NSLog(@"%ld", (long)buttonIndex);
}

4.備註, 如果UIActionSheet的紅色按鈕要取消填上nil即可,實例如右顯示,destructiveButtonTitle:nil

 

//viewcontroller.h檔內容如下

 #import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIActionSheetDelegate>

 - (IBAction)toClick:(id)sender;

@end

 

//viewcontroller.m檔內容如下 

 #import "ViewController.h"

@interfaceViewController ()

@end

@implementation ViewController

- (IBAction)toClick:(id)sender {
    
    UIActionSheet *myActionSheet = 
    [[UIActionSheetalloc] initWithTitle:@"actionSheet"
                                        delegate:self
                                        cancelButtonTitle:@"Cancel"
                                        destructiveButtonTitle:@"Delete"
                                        otherButtonTitles:@"Save", @"Share", nil];
    [myActionSheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%ld", (long)buttonIndex);
}

@end

 UIActionSheet  

 

arrow
arrow
    文章標籤
    objective-C iOS UIActionSheet
    全站熱搜

    Will 發表在 痞客邦 留言(1) 人氣()