views:

39

answers:

2

I have a apps with a navigation controller, when the user is in a specific view, He can touch a scollview and I want a push the rootViewController. So, I have subclasses uiscrollview, and in the method "touchesBegan", I want a push the rootviewcontroller, but I don't have access to the navigation controller! How I can do that?

thanks, Alex

+1  A: 

UIViewControllers have a property "navigationController", so you just need to access the view controller of the view being touched.

However, I've found that in iphone development, jumping through hoops like that is a sign of weakness in development. For example, why should a view tell a view CONTROLLER to do something. It should be the other way around. That being said, UIViewController is a subclass of UIResponder, so you can handle touches in the controller. Then you can simply say.

[myViewController.navigationController pushViewController:animated:]

DexterW
A: 

In your subclass of UIScrollView create two instanse variables:

id delegate;
SEL touchAction;

Then create methods:

-(void)setTarget:(id)target andAction:(SEL)action {
    delegate = target;
    touchAction = action;
}
-(void)callAction {
    if (delegate && [delegate respondsToSelector:touchAction]) {
        [delegate performSelector:touchAction];
    }
}

In your touchesBegan just call this mehtod:

...
[self callAction];
...

In your UIViewController class create method:

-(void)myScrollViewTouchAction {
    [self.navigationController pushViewController:myNewViewController animated:YES];
}

and finally when creating instance of your UIScrollView subclass, set it target and action:

[myScrollView setTarget:self andAction:@selector(myScrollViewTouchAction)];
jamapag
I do all this things but when this line is executed: [delegate performSelector:touchAction], the method myScrollViewTouchAction is not called, in the initwithnibname of my view controller, i create a instance of the uiscrollview subclass and I call setTarget....
alex
The problem is that when I called setTarget and when touchesBegan is called, it's not the same object, so the delegate is not set when the touchesBegan is called...
alex
then set target and action for new object.
jamapag