1. 程式人生 > >【程式碼筆記】iOS-UIActionSheet動態新增按鈕

【程式碼筆記】iOS-UIActionSheet動態新增按鈕

一,效果圖。

二,程式碼。

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController
<UIActionSheetDelegate>

@end

RootViewController.m

複製程式碼
//點選任何處,彈出UIActionSheet
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"
標題" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil]; // 逐個新增按鈕(比如可以是陣列迴圈) [sheet addButtonWithTitle:@"Item A"]; [sheet addButtonWithTitle:@"Item B"]; [sheet addButtonWithTitle:@"Item C"]; // 同時新增一個取消按鈕 [sheet addButtonWithTitle:@"Cancel
"]; sheet.cancelButtonIndex = sheet.numberOfButtons-1; [sheet showInView:self.view]; }
複製程式碼