iphone常用控件之UIActionSheet(操作表)

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. 

 

posted @ 2012-08-21 18:32  小、  阅读(2961)  评论(1编辑  收藏  举报