大佬教程收集整理的这篇文章主要介绍了iphone – UIActionSheet混乱的优雅解决方案,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我像这样使用UIActionSheets:
UIActionSheet * myChoices = [[UIActionSheet alloc] initWithtitle:nil delegate:self cancelButtontitle:@"cancel" destructiveButtontitle:@"erase" otherButtontitles: @"aaa",@"bbb",@"ccc",@"ddd",nil];
问题是,为了发现用户选择的选项,我必须使用:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonATindex:(NSInteger)buttonIndex { switch ([actionSheet tag]) { case 0: ... case 1: ... case 2: ... case 3: ... } }
基于索引的这种情况很糟糕,因为如果我改变了aaa,bbb,ccc等的顺序,我必须在动作表上更改大小写顺序.这个索引的东西并不是一个可靠的解决方案.
我试图想象一种方法来做到这一点,并成为独立于索引但没有得到任何满意的解决方案.使用buttontitleATindex也不够好,因为我的应用程序是本地化的,我必须为每个条目测试n个标题.有什么建议?
OHActionSheet
和
OHAlertView
类.
因为它们基于completionBlock模式,所以它们更具可读性(所有代码都在同一个地方,没有多个UIActionSheets的公共委托,……),并且功能更强大(因为块也可以根据需要捕获它们的上下文).
NSArray* otherButtons = @[ @"aaa",@"ddd" ]; [OHActionSheet showSheeTinView:self.view title:nil cancelButtontitle:@"cancel" destructiveButtontitle:@"erase" otherButtontitles:otherButtons completion:^(OHActionSheet* sheet,NSInteger buttonIndeX) { if (buttonIndex == sheet.cancelButtonIndeX) { // cancel } else if (buttonIndex == sheet.destructiveButtonIndeX) { // erase } else { NSUInteger idx = buttonIndex - sheet.firstOtherButtonIndex; // Some magic here: thanks to the blocks capturing capability,// the "otherButtons" array is accessible in the completion block! NSString* buttonName = otherButtons[idx]; // Do whatever you want with idx and buttonName } }];
请注意,在完成处理程序中if / else测试的otherButtons部分,您可以使用开关/ case测试idx,或使用我的ObjcSwitch
类别,这将允许您编写类似开关/类似代码但是对于NSStrings,所以你可以在你的OHActionSheet的完成处理程序中有这样的代码:
NSUInteger idx = buttonIndex - sheet.firstOtherButtonIndex; NSString* buttonName = otherButtons[idx]; [buttonName switchCase: @"aaa",^{ /* Some code here to execute for the "aaa" button */ },^{ /* Some code here to execute for the "bbb" button */ },^{ /* Some code here to execute for the "ccc" button */ },...,nil ];
编辑:既然最新的LLVM编译器支持新的“Object Literals”语法,您可以使用NSDictionary的紧凑语法执行与ObjcSwitch相同的操作:
((dispatch_block_t)@{ @"aaa": ^{ /* Some code here to execute for the "aaa" button */ },@"bbb": ^{ /* Some code here to execute for the "bbb" button */ },@"ccc": ^{ /* Some code here to execute for the "ccc" button */ },}[buttonName] ?:^{ /* Some code here to execute for defaults if no case found above */ })();
以上是大佬教程为你收集整理的iphone – UIActionSheet混乱的优雅解决方案全部内容,希望文章能够帮你解决iphone – UIActionSheet混乱的优雅解决方案所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。