1

I've created a custom UIViewController with storyboard and this is the class: .h file:

#import <UIKit/UIKit.h>

@interface PlayerViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *PlayerNumber;
@property (weak, nonatomic) IBOutlet UIImageView *PlayerImage;
@property (weak, nonatomic) IBOutlet UILabel *PlayerNameLabel;

-(void)setPlayerName:(NSString*)PlayerNameString;

@end

.m file:

#import "PlayerViewController.h"

@interface PlayerViewController ()

@end

@implementation PlayerViewController
@synthesize PlayerImage,PlayerNameLabel,PlayerNumber;


- (void)viewDidLoad {
[super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

-(void)setPlayerName:(NSString *)PlayerNameString{
self.PlayerNameLabel.text=PlayerNameString;
}

@end

from main view I create 2 instance of PlayerViewController in order to display 2 custom views:

- (void)viewDidLoad {
[super viewDidLoad];

[self DrawPlayerWithTag:1];
[self DrawPlayerWithTag:2];
}


- (void)DrawPlayerWithTag:(int)PlayerTag{
PlayerViewController *myPlayerViewController = [self.storyboard  instantiateViewControllerWithIdentifier:@"PlayerViewController"];
UIView *myPlayerView=myPlayerViewController.view;
myPlayerView.tag=PlayerTag;
myPlayerView.frame=CGRectMake(0, 0, 100, 100);
myPlayerView.center = CGPointMake(self.view.frame.size.width / 2,
                                 self.view.frame.size.height / 2);

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doDoubleTap)];
doubleTap.numberOfTapsRequired = 2;
[myPlayerView addGestureRecognizer:doubleTap];


[self.view addSubview:myPlayerView];
[myPlayerViewController setPlayerName:@"NameA"];

}

It seems to work. But, if I want to change the PlayerName, how can refer to view1 rather then view2 after a double tap event, and use the method setPlayerName?

Thanks a lot for your answer!

Beppe
  • 13
  • 3

1 Answers1

0

Change the callback method

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doDoubleTap:)];

You can get the tag of the view like this:

-(void)doDoubleTap:(UITapGestureRecognizer *)gesture{
   NSLog(@"%ld", gesture.view.tag);
}

You can keep your code and invoke setPlayerName with this post Get to UIViewController from UIView?

But, I suggest this way:

  • set target for gesture is myPlayerViewController

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:myPlayerViewController action:@selector(doDoubleTap:)];

  • Keep ref of myPlayerViewController in "main view" by an array like:

    [arrPlayerView addObject:myPlayerViewController]; /// keep pointing to myPlayerViewController, so it isn't released.

  • Place (void)doDoubleTap:(UITapGestureRecognizer *)gesture in PlayerViewController

Community
  • 1
  • 1
tuledev
  • 10,177
  • 4
  • 29
  • 49
  • Hi, it's clear, but as soon as I get the tag, how can I invoke the method setPlayerName? Thank you – Beppe Oct 25 '15 at 15:42
  • Great, it works perfectly, thank you very much, Beppe – Beppe Oct 25 '15 at 16:08
  • Hi anthu, i've marked your answer but it seems that we've to wait for my reputation up to 15 befor seeing it effect... Ciao grazie – Beppe Oct 25 '15 at 16:37
  • @Beppe mark correct is different with upvote. Upvote is the triangle. Mark answer is the check symbol. It is between 2 triangle. :) – tuledev Oct 25 '15 at 16:39
  • @Beppe And mark correct doesn't require the reputation. :) – tuledev Oct 25 '15 at 16:42