将UIActionSheet选项连接到操作的正确方法 [英] Proper way to connect UIActionSheet options to actions

查看:195
本文介绍了将UIActionSheet选项连接到操作的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iPhone应用程序中使用UIActionSheet时,将操作与按钮匹配的典型方法看起来非常脆弱,而且在美学上令人不快。也许是因为我最小的C / C ++背景(更多的Perl,Java,Lisp等)。匹配按钮索引似乎太多的魔术数字,并且断开连接,以避免简单的逻辑或一致性错误。

While using a UIActionSheet in an iphone app, the typical methods of matching actions to buttons seem very fragile and aesthetically unpleasant. Perhaps its due to my minimal C/C++ background (more Perl, Java, Lisp and others). Matching on button indexes just seems like too many magic numbers and too disconnected to avoid simple logical or consistency errors.

例如,

UIActionSheet *sources = [[UIActionSheet alloc]
         initWithTitle:@"Social Networks"
              delegate:self 
     cancelButtonTitle:@"Cancel" 
destructiveButtonTitle:nil 
     otherButtonTitles:@"Twitter", @"Facebook", @"Myspace", @"LinkedIn", @"BlahBlah", nil
];

<snip>

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == [actionSheet cancelButtonIndex]) {
        // all done
    } else if (buttonIndex == 0) {
        // Twitter
    } else if (buttonIndex == 1) {
        // Facebook
    } else if (buttonIndex == 2) {
        // LinkedIn
    } else if (buttonIndex == 3) {
        // Myspace
    }
}

请注意,行动处理代码至少有两个错误(至少根据评论)。

Notice there's at least two errors in the action handling code (according to the comments at least).

我缺少的是正确的设计模式,以避免在Objective-C中断开连接。如果这是perl,我将首先构建一个我的按钮选项的数组,然后可能创建一个快速查找表哈希,这将对应于对每个项目做适当的对象或子例程的另一个查找表。在java中,原始列表可能是首先是回调的对象。我知道我可以建立一个字典来模拟perl哈希,但是对于3-4个选项来说,这感觉非常笨重和繁琐。我也考虑过使用枚举来掩饰索引的魔力,但这只是问题的一小部分。

What I'm missing is the correct design pattern for avoiding that disconnect in Objective-C. If this were perl, I would first build an array of my button options and then probably create a quick lookup table hash that would correspond to another lookup table of objects or subroutines that did the appropriate thing for each item. In java, the original list would probably be objects in the first place with callbacks. I know I could build a Dictionary to mimic the perl hash but that feels very unwieldy and cumbersome for 3-4 options. I've also considered using a enum to mask the magic-ness of indexes but that's only a minor part of the problem.

真正的问题似乎是没有(简单?)方式在一个地方指定按钮字符串列表和相应的动作,从而消除了在添加/删除/重新排序选项时在两个地方修改代码的必要性,从而使得有效地不可能使我的错误示例代码使得。

The real problem seems to be that there's no (simple?) way to specify BOTH the list of button strings and corresponding actions in one place thereby eliminating the need to modify code in two places when adding/removing/reordering options and thus making it effectively impossible to make the kinds of mistakes that my sample code makes.

我不是想开始编程语言的圣战,我只是想在这种情况下确定正确的设计模式(我相信很多

I'm not trying to start a programming language holy war, I just want to figure what the correct design pattern in this scenario (and I believe many others in Objective C) for connecting the list of button strings to the list of actions.

推荐答案

在Aaron的选择器建议的基础上,我真的很喜欢现在做一个简单的ad-hoc调度方法的想法。它成功地避免了处理错误选项的可能性,并提供了一个清晰的分解关注点。当然,我可以想象一个用例,你想为每个选项做一些其他的事情,比如实例化一个对象,并把选项串传给Toro的答案。

Building on Aaron's selector suggestion, I really like the idea now of doing a simple ad-hoc dispatch method. It successfully avoids the possibility of handling the wrong option as well as providing a clean factorization of concerns. Of course, I could imagine a use case where you want to do something else first for each option, such as instantiating an object and passing it the option string much like Toro's answer.

这是一个简单的调度,调用诸如'actionTwitter'的方法:

Here's a simple dispatch that calls methods such as 'actionTwitter':

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {  
    if (buttonIndex == [actionSheet cancelButtonIndex]) {
        return;
    }

    NSString *methodName = [@"action" stringByAppendingString:[actionSheet buttonTitleAtIndex:buttonIndex]];
    SEL actionMethod = NSSelectorFromString(methodName);
    if ([self respondsToSelector:actionMethod]) {
        [self performSelector:actionMethod];
    } else {
        NSLog(@"Not yet implemented")
    }
}

这篇关于将UIActionSheet选项连接到操作的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆