[iOS] UIActionSheet – 呼叫上浮選單

上次google 到的範例:
http://stackoverflow.max-everyday.com/2017/05/ios-uiactionsheet/

 

比較簡單的範例是:

You can add all buttons after the init method.

UIActionSheet* sheet = [[[UIActionSheet alloc] init] autorelease];
sheet.title = @"Illustrations";
sheet.delegate = self;
[sheet addButtonWithTitle:@"ABC"];
[sheet addButtonWithTitle:@"XYZ"];
if (condition)
    [sheet addButtonWithTitle:@"LMNOP"];
sheet.cancelButtonIndex = [sheet addButtonWithTitle:@"Cancel"];

 


點擊按鈕裡的code如下

- (IBAction)toClick:(id)sender {

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

    [myActionSheet showInView:self.view]

};


//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 = 
    [[UIActionSheet alloc] 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
 

相關文章

寫留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *