简体   繁体   English

如何在iOS开发中处理自定义事件?

[英]How can I handle custom events in iOS development?

We're working on a project using some custom views. 我们正在使用一些自定义视图进行项目。 We have the following hierarchy: 我们具有以下层次结构:

UIViewController -> UIScrollView (custom subclass) -> UIView (custom subclass) UIViewController-> UIScrollView(自定义子类)-> UIView(自定义子类)

We are presenting a grid of buttons that are dynamically generated. 我们展示了动态生成的按钮网格。 When a user taps one of the UIViews that belong to the custom scroll view we fire a method that looks like this: 当用户点击属于自定义滚动视图的UIView之一时,我们将触发如下所示的方法:

- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
    [[self superview] itemSelected:self];
}

The super view in this case is our custom subclass of UIScrollView. 在这种情况下,超级视图是UIScrollView的自定义子类。 From here we fire another method: 从这里,我们触发另一种方法:

- (void) itemSelected: (id)selectedItem {
    itemView *item = selectedItem;
    [[self superview] initSliderViewForItemNamed:item.name];
    item = nil;
}

Now here is where things break. 现在这是事情破裂的地方。 We want to fire another method in UIViewController to load a new view at the top of our view hierarchy. 我们想在UIViewController中触发另一个方法,以在视图层次结构的顶部加载新视图。 So in UIViewController we have this method to test for success: 因此,在UIViewController中,我们可以使用以下方法来测试成功:

- (void) initSliderViewForItemNamed:(NSString *)selectedItemName {
    NSLog(@"Selected item was found! %@",selectedItemName);
}

But we never reach this point and the app crashes. 但是我们从来没有达到这一点,应用程序崩溃了。 It's because we can't reference the UIViewController here. 这是因为我们无法在此处引用UIViewController。 Instead we're referencing the view property of the UIViewController. 相反,我们引用的是UIViewController的view属性。 So our actual object hierarchy is: 因此,我们实际的对象层次结构是:

UIViewController.view -> UIScrollView (custom subclass) -> UIView (custom subclass) UIViewController.view-> UIScrollView(自定义子类)-> UIView(自定义子类)

This leads me to two questions. 这使我想到两个问题。

  1. How do we reference the UIViewController from our subview that belongs to the controller's view property? 我们如何从属于控制器的view属性的子视图中引用UIViewController?
  2. Is this method convoluted. 这种方法令人费解吗? Is there a better way to do this? 有一个更好的方法吗? Should we be assigning the the UIViewController as a delegate of our custom subclass of UIScrollView? 是否应该将UIViewController分配为UIScrollView的自定义子类的委托?

Having trouble understanding exactly what you want to achieve - can you please describe the end goal? 无法准确了解您要实现的目标-您能否描述最终目标? - this method does sound quite over-complicated and convoluted. -这种方法听起来确实过于复杂和复杂。 I think I have a solution but don't know if it is related :P 我想我有解决方案,但不知道是否相关:P

吉姆,您应该在自定义uiview子类上设置一个委托,然后让您查看控制器,使其成为委托并符合刚刚创建的协议,这样就可以了(即:tableview在做什么)

Yeh either using a delegate or you implement:- 是的,要么使用委托,要么您实现:

  • (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event (void)touchesBegan:(NSSet *)touch withEvent:(UIEvent *)event

or 要么

  • (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event (void)touchesEnded:(NSSet *)touch withEvent:(UIEvent *)event

depending on how you want to handle the touch. 取决于您要如何处理触摸。

What I ended up doing to solve this was assign the View Controller as the delegate of the scroll view. 为了解决这个问题,我最终将视图控制器指定为滚动视图的委托。 It looks like this: 看起来像这样:

    itemScrollView = [[ItemScrollView alloc] initWithFrame:CGRectMake(...)];
    [itemScrollView setDelegate:self];      
    [self.view addSubview: itemScrollView];

Then in my ItemScrollView Implementation I could refer to the ViewController with: 然后,在我的ItemScrollView实现中,我可以通过以下方式引用ViewController:

    [[self delegate] initSliderViewForItemNamed:selectedItem.name];

Big thanks to everyone who replied. 非常感谢所有回答的人。 So setting the View Controller as the delegate was the correct answer, however, no one went into detail well enough as to how to do this. 因此,将View Controller设置为委托是正确的答案,但是,没有人对如何做到这一点有足够详细的了解。 So I've covered that here in this response. 因此,我在此回复中已对此进行了介绍。 Additional information can also be found on this question involving delegates: 还可以找到涉及代表的有关此问题的其他信息:

How do I create delegates in Objective-C? 如何在Objective-C中创建委托?

The idiomatic way to do what you want would be to send the message up the responder chain . 做您想要的事情的惯用方式是在响应者链上发送消息。 UIViewController takes part in the responder chain, so it will receive the message. UIViewController参与了响应者链,因此它将接收消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM