V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
Livid
V2EX  ›  iDev

关于一个 view controller 里的不同 UIActionSheet 使用同一个 delegate 的问题

  •  
  •   Livid · 2012-02-27 11:24:39 +08:00 · 6256 次点击
    这是一个创建于 4439 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如果,在同一个 UIViewController 里,两个不同的按钮触发了两个不同的 UIActionSheet,然后这两个 UIActionSheet 的 delegate 都是这个 UIViewController 本身,那么大家会用什么样的办法在实现 delegate 的响应方法时区分不同的 UIActionSheet 呢?

    或者,有更好的设计模式?
    19 条回复    1970-01-01 08:00:00 +08:00
    Livid
        1
    Livid  
    MOD
    OP
       2012-02-27 11:28:27 +08:00
    我目前在用的一种方式是,为这些不同的 UIActionSheet 分配不同的 tag,然后在 delegate 的响应方法里做判断。

    所以我好奇是否还有其他更好的方法?
    ydhydh
        2
    ydhydh  
       2012-02-27 11:43:26 +08:00
    根据 buttonTitle ??

    buttonTitleAtIndex
    Fann
        3
    Fann  
       2012-02-27 11:45:41 +08:00
    同样用 tag 来区分处理,不同的 UIAlertView 也是用 tag 区分。
    fly2never
        4
    fly2never  
       2012-02-27 11:48:18 +08:00
    目前也是tag区分,还发现什么不好
    Livid
        5
    Livid  
    MOD
    OP
       2012-02-27 11:49:36 +08:00
    那么,如果就是用 tag 来区分的话,在 tag 的编码上,大家是否有什么好的规则心得可以分享一下呢?
    ydhydh
        6
    ydhydh  
       2012-02-27 11:59:25 +08:00
    @Livid 汗,理解错误,以为最终目的是区分button呢,考虑下block?
    froo
        7
    froo  
       2012-02-27 12:01:47 +08:00
    UIActionSheet *sheet1;
    UIActionSheet *sheet2;

    然后在Delegate中用if来判断
    if (actionsheet == sheet1 ){}, if (actionsheet == sheet2 ){} ……

    这样可读性应该好一些。
    Kai
        8
    Kai  
    MOD
       2012-02-27 12:07:26 +08:00 via iPhone
    同 @froo 的方法,感觉清楚一些。
    doskoi
        9
    doskoi  
       2012-02-27 12:18:40 +08:00
    TAG可以定义成enum

    enum {
    kAlertView,
    kInfomationView,
    kWarningView,
    kErrorView
    }

    然后tag = Alertview.tag;

    switch tag, case不同tag做处理
    levey
        10
    levey  
       2012-02-27 12:35:58 +08:00
    要么用类的变量,要么用tag,我喜欢后者。as.tag = 0x2001
    Fann
        11
    Fann  
       2012-02-27 13:02:05 +08:00
    @doskoi +1.
    linlinqi
        12
    linlinqi  
       2012-02-27 13:34:57 +08:00
    我试过用actionSheet的title来判断——当它们的标题不一样的时候
    sawyerzhang
        13
    sawyerzhang  
       2012-02-27 13:42:26 +08:00
    同意 @froo, 直接在IB里面拉很方便的。
    BB9z
        14
    BB9z  
       2012-02-27 18:02:55 +08:00
    喜欢 @doskoi 的方法。
    lldong
        15
    lldong  
       2012-02-27 18:23:05 +08:00
    支持用tag,而且tag是NSInteger类型,能保证存得下一个指针,有时可以利用这点保存一些和该view关联的信息。
    avatasia
        16
    avatasia  
       2012-02-28 17:51:09 +08:00
    public class UIActionSheet
    {
    public static UIActionSheet Sheet1 = new UIActionSheet ("Sheet1");
    public static UIActionSheet Sheet2 = new UIActionSheet ("Sheet2");
    private UIActionSheet (string type)
    {
    }
    }

    UIController(UIActionSheet sheet)
    {
    }

    随便写的
    elden
        17
    elden  
       2012-02-28 18:17:19 +08:00
    Blocks
    Shane
        19
    Shane  
       2012-03-01 14:45:56 +08:00
    @lex +1 这个不错
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5373 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 07:45 · PVG 15:45 · LAX 00:45 · JFK 03:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.