UIActionSheet的简单使用

这是用来弹出多重选项的,并且每个选项还可以自定义事件,这与uialert还不一样。

先看看它长啥样。

再说实现过程

在controller的.m中,先实现代理

@interface controllerA<UIActionSheetDelegate>

然后添加事件,比如点击按钮的事件

-(void)clickBtn{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"添加联系人" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从通讯录添加",@"手动添加",@"从会议通讯录添加", nil];
    
    [sheet showInView:self.view];//在当前view显示action sheet
}
//每个选项的事件
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (0 == buttonIndex) {// 从通讯录添加
        ControllerB *b = [[ControllerB alloc] init];
        b.delegate = self;
        [self presentViewController:b animated:YES completion:nil];
        
    }else if (1 == buttonIndex){// 手动添加
        ControllerC *c = [[ControllerC alloc] init];
        c.delegate = self;
        [self presentViewController:c animated:YES completion:nil];
    }else if (2 == buttonIndex){
        ControllerC *b = [[ControllerC alloc] init];
        c.delegate = self;
        [self presentViewController:c animated:YES completion:nil];
    }

}

cancelButtonTitle destructiveButtonTitle是系统自动的两项。
sheet也可以加到keyWindow中,防止遮挡

[sheet showInView:[UIApplication sharedApplication].keyWindow];
原文地址:https://www.cnblogs.com/Apologize/p/4362955.html