ScrollView with Images - changing tabs on touchesended does not work

177 Views Asked by At

In my app I have a UIScrollView and within that I have programmatically created UIImageViews. This is based off the Tab-bar template in Xcode. What I'm trying to do is to change Tabs when a particular image is pressed, and whilst I'm able to register the event as a touch, changing the tabIndex does nothing. Typing in the following code:

    UITabBarController *tab=self.tabBarController;
        if (tab){
            NSLog(@"Good");
        }
        else {
            NSLog(@"No Good");

Always results in No Good being Logged. The code I've written can be seen here, where my UIScrollView is of type ScrollViewer: @implementation scrollViewer - (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {

if (!self.dragging) {
    NSLog(@"Recieved Touch!");
    UITouch *aTouch = [touches anyObject];
    MainView *theInstance = [[MainView alloc] init];


     CGPoint point = [aTouch locationInView:self];
        theInstance.ycoord = point.y;
        [theInstance touchHand];
}
    [super touchesEnded: touches withEvent: event];
}

@end

Any help is appreciated thanks!

1

There are 1 best solutions below

2
On

You never gave us any sample output for:

NSLog(@"Y Coord= %d",adjustedy);

But I will assume the value should cause the switch.

So first you need to log self.tabbarcontroller to be sure it is not nil. If it is ok the system may not want to let you switch inside touch events (it's bad form anyway). I would do the setting as this:

dispatch_async(dispatch_get_main_queue(), ^{ do code here } );

So it runs later.