iPhone – UIActionSheet Example

UIActionSheet is a cool way to get user input. The following example shows you how to implement the UIActionSheet.

1. Extend the UIActionSheetDelegate in the .h header file of the ViewController and add the(IBAction)showActionSheet:(id)sender method.

@interface MyViewController : UIViewController <UIActionSheetDelegate> {
	...
}

...

-(IBAction)showActionSheet:(id)sender;

@end

2. Add the following code in the .m implementation file. There are 5 parameters for initializing theUIActionSheet

  • initWithTitle:@”Title”
  • delegate:self
  • cancelButtonTitle:@”Cancel Button”
  • destructiveButtonTitle:@”Destructive Button”
  • otherButtonTitles:@”Other Button 1″, @”Other Button 2″, nil
-(IBAction)showActionSheet:(id)sender {
	UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel Button" destructiveButtonTitle:@"Destructive Button" otherButtonTitles:@"Other Button 1", @"Other Button 2", nil];
	popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
	[popupQuery showInView:self.view];
	[popupQuery release];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (buttonIndex == 0) {
		self.label.text = @"Destructive Button Clicked";
	} else if (buttonIndex == 1) {
		self.label.text = @"Other Button 1 Clicked";
	} else if (buttonIndex == 2) {
		self.label.text = @"Other Button 2 Clicked";
	} else if (buttonIndex == 3) {
		self.label.text = @"Cancel Button Clicked";
	}

	/**
	 * OR use the following switch statement
	 * Suggested by Colin =)
	 */
	/*
	switch (buttonIndex) {
		case 0:
			self.label.text = @"Destructive Button Clicked";
			break;
		case 1:
			self.label.text = @"Other Button 1 Clicked";
			break;
		case 2:
			self.label.text = @"Other Button 2 Clicked";
			break;
		case 3:
			self.label.text = @"Cancel Button Clicked";
			break;
	}
	*/
}

3. Link the (IBAction)showActionSheet:(id)sender with a Hello Button in the main view such that the UIActionSheet will appear when user

 presses the Hello Button.

4. Text will be printed on the main view if u press the button in the UIActionSheet

帖上自己的代码

-(IBAction)shareBtn:(id)sender{
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"分享到" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"新浪微博",@"腾讯微博",@"短信",@"邮件",nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];
    [actionSheet release];

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    switch (buttonIndex) {
		case 0:
			NSLog(@"新浪微博");
			break;
		case 1:
			NSLog(@"腾讯微博");
			break;
		case 2:
			NSLog(@"短信");
			break;
		case 3:
			NSLog(@"邮件");
			break;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值