简体   繁体   中英

I have a UIView inside of a UIScrollView. How do I reference the parent UIScrollView?

I've finally figured out how to stick a UIView inside of a UIScrollView, but now I have the problem of not being able to call the UIScrollView (who is the parent of the UIView) from within my UIView.

I've tried something like:

UIScrollView *scrollView = (UIScrollView *)self.superview;

But that's not working. My thought process was that the superview would be the container for the current view (UIView), but that returned a UIView so I just casted it to a UIScrollView, which I turned out to not be the right guess.

When you call

[someView addSubview:someOtherView];

someView is automatically set as superview of someOtherView . In your case, if this

UIScrollView *scrollView = (UIScrollView *)[self superview];

does not work (you probably have a typo in your question as the "=" is missing), then you have a problem setting things up (If that's the case, post your setup code).

You can add the scrollView to the UIView, thorough IB or code.

Through code you can add as

[self.view addSubView:scView];

Before that you need to add all the fields, labels and TextFields to the ScrollView. and set tags to the fields for the reference.

if u add textfield in scrollView ...

[scView addSubView: TextFieldsName];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM