Hey guys! I have this little problem:
I have one ViewController which adds 2 subViews in my ViewController so I have something like this:
//in my viewController.m i have this:
- (void)startIcons
{
IconHolder *newIconHolder = [[IconHolder alloc] initWithItem:@"SomeItenName"];
[self.view addSubview:newIconHolder];
}
- (void)onPressIcon targetIcon(IconHolder *)pressedIcon
{
NSLog(@"IconPressed %@", [pressedIcon getName]);
}
And this is my subclass touchs:
//And in my IconHolder.m i have this:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//Here i need to call the method onPressIcon from my ViewController
}
Now:
How can I do that? The best way is create a linkage in my constructor to save my ViewController? How am I supposed to do that?
Thanks!
Yes, you should create that link just like you suspected.
Simply add a member variable
MyViewController* viewControllerto your view and set it up when you create the view. If you want to get clever, you can create it as a property.Beware that you shouldn’t retain the viewController from a view though – the view is already retained by the controller and if you have a retain going the other way, you will generate a retain cycle and will cause a leak.