Answer the question
In order to leave comments, you need to log in
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];
}
};
Answer the question
In order to leave comments, you need to log in
ASCardView *cView = [[ASCardView alloc] initWithFrame:cardRect andWithCard:card];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[cView addGestureRecognizer:singleTap];
[self addSubview:cView];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question