-1

我想知道如何使用子视图 UILabel 访问 UIScrollView。

我尝试使用.superview;访问 UIScrollView 但是我现在收到一个错误

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

我正在使用的代码如下所示

- (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];

}
4

1 回答 1

1

a 的父视图UILabel是类型的UIView,因此不会响应您尝试调用的方法。您可以将 superview 转换为 a UIScrollView,以便 Xcode 可以看到您尝试访问的方法和属性。您还应该检查超级视图是否响应该方法。

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

鉴于您的示例代码,您还需要强制转换超级视图以获取 contentOffset

float newPosition = ((UIScrollView *)label.superview).contentOffset.x+label.frame.size.width;
于 2014-01-19T22:48:41.257 回答