UI第十三节——UIActionSheet

时间:2023-03-08 15:46:52

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UISwitch *swc = [[UISwitch alloc] initWithFrame:CGRectMake(40, 100, 295, 30)];
    [swc addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:swc];
}

- (void)switchValueChanged:(UISwitch *)swc
{
    if (swc.on == YES) {
        
        // 实例化UIActionSheet
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"这里是ActionSheet的标题 这里是ActionSheet的标题 这里是ActionSheet的标题 这里是ActionSheet的标题 这里是ActionSheet的标题 这里是ActionSheet的标题" delegate:self cancelButtonTitle:@"取消按钮" destructiveButtonTitle:@"删除按钮" otherButtonTitles:@"酱油按钮1", @"酱油按钮2", @"酱油按钮3", nil];
        
        // 显示UIActionSheet
        [actionSheet showInView:self.view];
    }
}

// UIActionSheet 代理方法,用户点击的时候会回调这个方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // 这里可以用buttonIndex来判断用户点击了哪个按钮
    NSLog(@"%ld, %@", buttonIndex, [actionSheet buttonTitleAtIndex:buttonIndex]);
}