简体   繁体   中英

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).

In my current design my ViewController creates a single View (PlayingBoardView) that in term creates subviews (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.

My issue is that I don't have a reference to the ViewController in my PlayingBoardView (where the GestureRecognizer is created).

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?

or 2: Is this a designproblem? Should my ViewController create both the PlayingBoard and then add the cards and in this way be able to use self.

I'm new to Swift and brand new to StackOverflow so apologise if this question is not framed correctly.

Thank you,

This is a job for a delegate pattern.

Your ViewController containing the touchCard function should be assigned as a delegate of each of the CardView during the view instantiation. You can then call the function straight from the view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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