-1

I would like to know how to access a UIScrollView using a subview UILabel.

I have tried to access the UIScrollView using .superview; however I am now receiving an error

No visible @interface for 'UIView' declares the selector 'scrollRectToVisible:animated:'

The code I am using looks like this

- (void) SymbolButtonPressed:(NSString *)selectedString {

    UILabel *label = (UILabel *)[self.view viewWithTag:currentlySelectedTag];

// perform scrolling here, figure out what view your uilable is in.
    float newPosition = label.superview.contentOffset.x+label.frame.size.width;
    CGRect toVisible = CGRectMake(newPosition, 0, label.superview.frame.size.width, label.superview.frame.size.height);

    [label.superview scrollRectToVisible:toVisible animated:YES];

}
halfer
  • 19,824
  • 17
  • 99
  • 186
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

1 Answers1

1

The superview of a UILabel is of type UIView and so does not respond to the method you are trying to call. You can cast the superview as a UIScrollView so that Xcode can see the methods and properties you are trying to access. You should also check if the superview responds to the method.

if([label.superview respondsToSelector:@selector(scrollRectToVisible:animated:)]) {
    [(UIScrollView *)label.superview scrollRectToVisible:toVisible animated:YES];
}

Given your sample code you will also need to cast the superview to get contentOffset

float newPosition = ((UIScrollView *)label.superview).contentOffset.x+label.frame.size.width;
Matt
  • 2,329
  • 1
  • 21
  • 23
  • XCode dosnt like that code either. Looks like I will have to find another way to access it. its jus that I have multiple UILabels inside Mutiple scrollviews that are placed inside UITableViewCells.. So I have a table of UIScrollviews as their cells and im trying to figure out which UIScroll view to scroll. – HurkNburkS Jan 19 '14 at 23:59
  • @HurkNburkS I have expanded my answer to include casting the superview to a UIScrollView as well - which is probably what Xcode was complaining about. Try that. – Matt Jan 20 '14 at 14:36