PDA

View Full Version : Push a controller from a UIImageview touch




allian
Apr 14, 2009, 09:56 AM
Hello everyone, (sorry for my english first)

I'll explain you my problem.
I have a UIViewController (TagCloudNavigationController) with a lot of UIViewController (MyTag) instance inside. Each instance is like an icon with an UIImageView and a UILabel. My desire is to push a new UIViewController when a user touch one of these icons.

I have a method called switchViews which works perfect if I call them from a button placed in my parent view (TagCloudNavigationController).

- (IBAction)switchViews:(id)sender {

// Navigation logic may go here. Create and push another view controller.
NavigationViewController *newViewController = [[NavigationViewController alloc] init];
newViewController.tagTitle = @"Surf";
[self.navigationController pushViewController:newViewController animated:YES];

}

But when I call this method from the touchesBegan :

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
if (touch.phase == UITouchPhaseBegan)
{


TagCloudNavigationController *huh = [[TagCloudNavigationController alloc] init];
[huh switchViews:self.view.window];

}
}

it doesn't work !! the switchiews method is called (i see the nslog inside) but nothing happened.
I think the mistake comes from the calling of the switchViews.

Anyone can help me please.
Thanks



PhoneyDeveloper
Apr 15, 2009, 10:01 AM
Don't create a new TagCloudNavigationController to call switchviews on. Call switchviews on your already existing instance of TagCloudNavigationController.

What is the value of self.navigationController in the new instance of TagCloudNavigationController when you call switchviews? Probably nil since it hasn't been pushed itself.

allian
Apr 15, 2009, 10:07 AM
yes it's exactly this !! the return value is nil !!

how I have to do to deal with my existing instance of TagCloudNavigationController, I don't know that.

Thank you

PhoneyDeveloper
Apr 16, 2009, 12:28 AM
Does your app delegate create the TagCloudNavigationController? If so keep this controller in an ivar and add a property to access it. In your touchesBegan that you showed read the property and then call switchviews with the single instance of the TagCloudNavigationController that you have.

allian
Apr 16, 2009, 02:44 AM
thank for your answer, I don't know what is an ivar and I don't find anything clear on the net, but I've resolved my problem declaring a property in my class MyTag which is a UINavigationController. So when I create each instance of this class I fill this property with super.navigationController which is my rootLevel controller.


Now it works but I don't think it's the better way

PhoneyDeveloper
Apr 16, 2009, 10:42 AM
ivar = instance variable.