1.。效果圖:分別為有短信分享 無短信分享
-(void)viewDidLoad{ //添加按鈕 UIButton *shareButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 50)]; [shareButton setBackgroundColor:[UIColor redColor]]; [shareButton addTarget:self action:@selector(shareButtonPressed) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:shareButton]; } //點擊觸發分享按鈕 -(void)shareButtonPressed{ [(id)self showActionSheetWeibo:NO];//無短信分享功能 [(id)self showActionSheetWeibo:YES];//有短信分享功能 } - (void)showActionSheetWeibo:(BOOL)ishaveMail { UIActionSheet *actionSheet; if (ishaveMail == YES) { actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"分享到新浪微博", @"分享到騰訊微博",@"短信分享", nil]; actionSheet.tag = 99; }else { actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"dd" otherButtonTitles:@"分享到新浪微博", @"分享到騰訊微博", nil]; //destructiveButtonTitle被紅色高亮顯示 actionSheet.tag = 101; } [actionSheet showFromRect:self.view.bounds inView:self.view animated:YES]; // [actionSheet showInView:self.view]; //顯示操作表單 [actionSheet release]; } #pragma mark ActionSheet Delegate Methods - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"clickedButtonAtIndex:%d",buttonIndex);
//其實如果有dextructiveButtonTitle的話,這個所對應的按鈕才是編號為0 if (buttonIndex==0) { //新浪微博分享 NSLog(@"新浪微博"); /*點擊觸發代碼*/ }else if(buttonIndex==1) { //騰訊微博分享 NSLog(@"騰訊微博"); /*點擊觸發代碼*/ }else if(actionSheet.tag==99&&buttonIndex==2){ NSLog(@"短信分享"); //發送短信 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://"]]; } }
2.。直接按鈕觸發:
通過按鍵觸發buttonPressed - (IBAction)buttonPressed:(id)sender { UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Are you sure?" delegate:self cancelButtonTitle:@"No Way!" //取消 destructiveButtonTitle:@"Yes, I’m Sure!" //繼續 otherButtonTitles:nil]; //其他按鈕,若沒則nil [actionSheet showInView:self.view]; //顯示自己 } - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { if (buttonIndex != [actionSheet cancelButtonIndex]) { NSString *msg = nil; if (nameField.text.length > 0) msg = [[NSString alloc] initWithFormat: @"You can breathe easy, %@, everything went OK.", nameField.text]; else msg = @"You can breathe easy, everything went OK."; } }
自定義ActionSheet:效果圖如下:
背景顏色可以自定義,按鈕也可以
//點擊觸發: -(void)btnPressed{ actionSheet =[[UIActionSheet alloc]initWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n\n" //為后面自己增加的view空出空間 delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:nil]; // [actionSheet addButtonWithTitle:@"登陸注冊"]; // [actionSheet addButtonWithTitle:@"手機快速下單"]; // [actionSheet addButtonWithTitle:@"取消"]; [actionSheet showInView:self.view]; // [actionSheet setActionSheetStyle:UIActionSheetStyleBlackOpaque]; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)]; view.backgroundColor = [UIColor greenColor]; [actionSheet addSubview:view]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(30, 30, 100, 30); btn.tag = 1001; [view addSubview:btn]; [btn addTarget:self action:@selector(exitButtonClick:) forControlEvents:UIControlEventTouchUpInside]; UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn2.frame = CGRectMake(30, 70, 100, 30); btn2.tag = 1002; [view addSubview:btn2]; [btn2 addTarget:self action:@selector(exitButtonClick:) forControlEvents:UIControlEventTouchUpInside]; [actionSheet release]; } //點擊自己建按鈕觸發 -(void)exitButtonClick:(UIButton *)sender{ NSLog(@"%d",sender.tag); [actionSheet dismissWithClickedButtonIndex:sender.tag animated:YES]; } //點擊系統按鈕觸發 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"click %d",buttonIndex); } - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ NSLog(@"dismis %d",buttonIndex); }
另外也可以建兩個View加載到self.view,模擬ActionSheet.