代码之家  ›  专栏  ›  技术社区  ›  Philippe Leybaert

遍历响应程序链以传递自定义事件。这不对吗?

  •  18
  • Philippe Leybaert  · 技术社区  · 16 年前

    我真正想做的是 习俗

    // some event happened in my view that 
    // I want to turn into a custom event and pass it "up":
    
    UIResponder *responder = [self nextResponder];
    
    while (responder) {
    
       if ([responder conformsToProtocol:@protocol(ItemSelectedDelegate)]) {
           [responder itemSelected:someItem];
           break;
       } 
    
       responder = [responder nextResponder];
    }
    

    这很有效,但我觉得应该有其他方法来处理。手动走这条路看起来不怎么。。。很好。

    注意,在这里通知不是一个好的解决方案,因为我只希望涉及视图层次结构中的对象,并且通知是全局的。

    编辑

    我想完成什么?

    处理tap的视图位于视图层次结构中主视图下面的几个级别。我必须告诉控制器(或者在某些情况下是一个特定的子视图“上链”)选择了一个项。

    侦听通知是一个选项,但我不喜欢该解决方案,因为选择项不是全局事件。它严格绑定到当前视图控制器。

    3 回复  |  直到 14 年前
        1
  •  12
  •   Mike Abdullah    12 年前

    你很接近。更标准的是这样的:

    @implementation NSResponder (MyViewController)
    - (void)itemSelected:(id)someItem
    {
        [[self nextResponder] itemSelected:someItem];
    }
    @end
    

        2
  •  14
  •   Peter Hosey    12 年前

    UIApplication has a method for just this purpose ,就像 its Cocoa cousin . 您可以用一条消息替换问题中的所有代码。

        3
  •  4
  •   Community Mohan Dere    9 年前

    如果你确信 设置正确。如果您希望更多地控制通知哪个对象事件,则应使用 targetForAction:withSender: 相反。

    这允许您指定第一个应该能够处理事件的视图,然后从该视图开始,它将沿着响应程序链向上爬行,直到找到一个可以处理消息的对象。

    这是一个 fully documented

    @interface ABCResponderChainHelper
    /*!
     Sends an action message identified by selector to a specified target's responder chain.
     @param action 
        A selector identifying an action method. See the remarks for information on the permitted selector forms.
     @param target 
        The object to receive the action message. If @p target cannot invoke the action, it passes the request up the responder chain.
     @param sender
        The object that is sending the action message.
     @param userInfo
        The user info for the action. This parameter may be @c nil.
     @return
        @c YES if a responder object handled the action message, @c NO if no object in the responder chain handled the message.
     @remarks
        This method pushes two parameters when calling the target. This design enables the action selector to be one of the following:
     @code
     - (void)action
     - (void)action:(id)sender
     - (void)action:(id)sender userInfo:(id)userInfo@endcode
    */
    + (BOOL)sendResponderChainAction:(SEL)action to:(UIResponder *)target from:(id)sender withUserInfo:(id)userInfo;
    @end
    

    实施:

    @implementation ABCResponderChainHelper
    + (BOOL)sendResponderChainAction:(SEL)action to:(UIResponder *)target from:(id)sender withUserInfo:(id)userInfo {
        target = [target targetForAction:action withSender:sender];
        if (!target) {
            return NO;
        }
    
        NSMethodSignature *signature = [target methodSignatureForSelector:action];
        const NSInteger hiddenArgumentCount = 2; // self and _cmd
        NSInteger argumentCount = [signature numberOfArguments] - hiddenArgumentCount;
        switch (argumentCount) {
            case 0:
                SuppressPerformSelectorLeakWarning([target performSelector:action]);
                break;
            case 1:
                SuppressPerformSelectorLeakWarning([target performSelector:action withObject:sender]);
                break;
            case 2:
                SuppressPerformSelectorLeakWarning([target performSelector:action withObject:sender withObject:userInfo]);
                break;
            default:
                NSAssert(NO, @"Invalid number of arguments.");
                break;
        }
    
        return YES;
    }
    @end
    

    SuppressPerformSelectorLeakWarning

    从一个合适的角度来看,这很有效。假设您在单元上有一个手势识别器,您需要将执行的操作通知视图控制器。不必为单元格创建委托,然后将消息转发给委托,您可以改为使用响应程序链。

    // in table view cell class:
    - (void)longPressGesture:(UILongPressGestureRecognizer *)recognizer {
        // ...
        [ABCResponderChainHelper sendResponderChainAction:@selector(longPressCell:) to:self from:self withUserInfo:nil];
    }
    

    在这里 self 指的是细胞本身。这个 responder chain UITableViewCell ,然后 UITableView UIViewController .

    示例用法(接收器):

    #pragma mark - Custom Table View Cell Responder Chain Messages
    - (void)longPressCell:(UITableViewCell *)sender {
        // handle the long press
    }
    

    如果你想做同样的事 sendAction:to:from:forEvent: ,您有两个问题:

    • 为了让它与响应链协同工作,您必须传入 nil 致 to 第一反应者 而不是你选择的对象。控制 可能很麻烦。使用此方法,您只需告诉它从哪个对象开始。
    • would need to subclass UIEvent userInfo 并在事件参数中传递;这种情况下的笨拙解决方案。