简体   繁体   English

View中的手势识别器和使用View Controller作为Target,我的设计错了吗?

[英]Gesture recogniser in View and use View Controller as Target, is my design wrong?

I'm creating a card-game IOS-application ("Set" card game). 我正在创建一个卡片游戏IOS应用程序(“Set”卡片游戏)。

In my current design my ViewController creates a single View (PlayingBoardView) that in term creates subviews (CardsViews). 在我目前的设计中,我的ViewController创建了一个View(PlayBoardView),它在术语中创建子视图(CardsViews)。 When I now want to add UITapGestureRecognizer to the cards (to be able to select them) I want the cards to target my ViewController and select a "touchCard" function I have there. 当我现在想要将UITapGestureRecognizer添加到卡片中时(为了能够选择它们)我希望卡片以我的ViewController为目标并选择我在那里的“touchCard”功能。

My issue is that I don't have a reference to the ViewController in my PlayingBoardView (where the GestureRecognizer is created). 我的问题是我没有在我的PlayingBoardView(创建GestureRecognizer的地方)中引用ViewController。

My questions is therefore either: 因此我的问题是:

1: Is there a way for a view that is being created in another view to target my ViewController when I add a GestureRecognizer? 1:当我添加GestureRecognizer时,有没有办法让另一个视图中创建的视图以我的ViewController为目标?

or 2: Is this a designproblem? 或者2:这是一个设计问题吗? Should my ViewController create both the PlayingBoard and then add the cards and in this way be able to use self. 我的ViewController应该创建两个PlayBoard,然后添加卡片,这样就可以使用self。

I'm new to Swift and brand new to StackOverflow so apologise if this question is not framed correctly. 我是Swift的新手,也是StackOverflow的新手,如果这个问题没有正确构建,请道歉。

Thank you, 谢谢,

This is a job for a delegate pattern. 这是delegate模式的工作。

Your ViewController containing the touchCard function should be assigned as a delegate of each of the CardView during the view instantiation. 在视图实例化期间,应将包含touchCard函数的ViewController指定为每个CardViewdelegate You can then call the function straight from the view. 然后,您可以从视图中直接调用该函数。

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

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