A
A
andreys752014-04-23 10:07:04
Objective-C
andreys75, 2014-04-23 10:07:04

How to transfer control by clicking on an object to the main controller?

There is a main controller ViewController - in it the main view
There is a TableView subclass: UIView
This view is created in the storyboard
At some point in time, many subviews are programmatically created in the TableView - of the CardView class - these are playing cards
each card is added to click processing

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [cView addGestureRecognizer:singleTap];

but in this case, the tap method is called, which must be described in the CardView class, but I want this event to be handled in the main controller.
Question 1. Is it possible to get a link to the main controller in order to write initWithTarget: link_to_controller
if this is not impossible or "not right", then what is the best way to implement it?
Option 1. implement the tap method in the CardView class, and send Notifications there
Option 2. try to come up with a solution with delegation of this method (a completely incomprehensible area for me yet)
Option 3. Pass a link to the main controller when creating CardView objects and store it as a property
I'm sorry, but I'm still just getting into ios programming, and I can probably ask stupid questions.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Alexander, 2014-04-23
@alexyat

2 and 3 are one and the same. Method 1 (with notifications) is used if you need to notify several disparate objects. I think in your case the easiest way is to make a link to the controller in the CardView.

K
kaspartus, 2014-04-23
@kaspartus

You have only one VC, we should write logic only in controllers. This VC needs to know about this tableView. Therefore, the delegate methods of tableView should be in this controller, those that are responsible for creating a cell, selecting a cell, and so on, which means that cardView will be created in the VC class, but then there are no questions about processing taps. So you've moved the logic to the View classes in vain.
If we want to separate the logic, then we need one more VC2, which will hold the tableView, will create cards, learn about their click. At the same time, the main VC1 will create it and immediately add the VC2 view to your own. That's when VC2 will delegate clicks on the cards to VC1.
There are a lot of examples of creating custom delegates, I won’t recommend a specific one, but they are easy to google.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question