此类使得使用带有块的UIActionSheet
更加容易。
这允许您直接提供要执行的代码(作为块),以响应按钮的点击,而不是声明委托并实现相应的方法。
这也有一个很大的优势,即**简化代码,尤其是在使用同一个对象中的多个UIActionSheets
时**(在这种情况下,如果共享同一个委托,代码很难保持简洁)。
注意:您可能还对OHAlertView感兴趣。
NSArray* flavours = [NSArray arrayWithObjects:@"chocolate",@"vanilla",@"strawberry",nil];
[OHActionSheet showSheetInView:self.window
title:@"Ice cream?"
cancelButtonTitle:@"Maybe later"
destructiveButtonTitle:@"No thanks!"
otherButtonTitles:flavours
completion:^(OHActionSheet *sheet, NSInteger buttonIndex)
{
NSLog(@"button tapped: %d",buttonIndex);
if (buttonIndex == sheet.cancelButtonIndex) {
self.status.text = @"Your order has been postponed";
} else if (buttonIndex == sheet.destructiveButtonIndex) {
self.status.text = @"Your order has been cancelled";
} else {
NSString* flavour = [flavours objectAtIndex:(buttonIndex-sheet.firstOtherButtonIndex)];
self.status.text = [NSString stringWithFormat:@"You ordered a %@ ice cream.",flavour];
}
}];
您还可以使用这个类来生成一个ActionSheet,它将在给定时间后消失。(您甚至可以在您的表格中添加一个动态文本,以显示实时倒计时)。
NSArray* flavours = [NSArray arrayWithObjects:@"apple",@"pear",@"banana",nil];
[[[OHActionSheet alloc] initWithTitle:@"What's your favorite fruit?"
cancelButtonTitle:@"Don't care"
destructiveButtonTitle:nil
otherButtonTitles:flavours
completion:^(OHActionSheet *sheet, NSInteger buttonIndex)
{
NSLog(@"button tapped: %d",buttonIndex);
if (buttonIndex == sheet.cancelButtonIndex) {
self.status.text = @"You didn't answer the question";
} else if (buttonIndex == -1) {
self.status.text = @"The action sheet timed out";
} else {
NSString* fruit = [flavours objectAtIndex:(buttonIndex-sheet.firstOtherButtonIndex)];
self.status.text = [NSString stringWithFormat:@"Your favorite fruit is %@.",fruit];
}
}] showInView:self.window withTimeout:8 timeoutButtonIndex:-1];
此代码受MIT许可证的约束。