开发者

push view when touchesBegan

I have a apps with a navigation controller, when the user is i开发者_开发问答n 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?


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:]


In your subclass of UIScrollView create two instance 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 method:

...
[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)];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜