A
A
andreys752014-04-22 20:02:41
Objective-C
andreys75, 2014-04-22 20:02:41

How to add action for UIView created in code?

 Good afternoon!
How to set an action on clicking on an object, if the object is created in the code? I understand how to do this if we form objects in the storyboard, but how to do it in code?
I display objects on the screen - playing cards, by clicking on the card, you need to perform an action.
Here is the code that I display objects on the screen:

- (void) drawCards
{
   //UIScrollView
   
    //Удалить все карты, если мы вызываем этот метод после поворота экрана - 
//может измениться кол-во выводимых на экран карт 
    
    for (UIView *v in self.subviews) {
        [v removeFromSuperview];
    }
    
    
    
    NSInteger cardsOnScreen = (NSInteger)(self.bounds.size.width-20)/CARD_WIDTH;
    
    for (int i=0; (i<cardsOnScreen) & (i<[self.player.hend.cards count] ); i++) {
        Card *card = self.player.hend.cards[i];
        
        CGRect cardRect = CGRectMake(10+(CARD_WIDTH+10)*i, 10, CARD_WIDTH, CARD_HEIGHT);
        ASCardView *cView = [[ASCardView alloc] initWithFrame:cardRect andWithCard:card];
        [self addSubview:cView];
        
    }
    
    
};

How to write a method in the controller that will work on clicking on each card?

Answer the question

In order to leave comments, you need to log in

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

ASCardView *cView = [[ASCardView alloc] initWithFrame:cardRect andWithCard:card];
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [cView addGestureRecognizer:singleTap];
        [self addSubview:cView];

The answer is obviously simple :) Apparently, no one answered this ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question