简体   繁体   中英

Programmatically access subview of UIScrollView

Within a View Controller I added a UIScrollView, then added a UIView to the scroller. I have several buttons placed in that UIView through Storyboard, and I want to conditionally add additional buttons programmatically.

I have tried linking the following references to the UIScrollView and UIView in Storyboard, but either link will cause the app to crash when this view controller is loaded:

@property (strong, nonatomic) IBOutlet UIScrollView *scroll;
@property (strong, nonatomic) IBOutlet UIView *v;

Since I am unable to create a referencing outlet in Storyboard, how do I create a reference to the UIView so I can add my button?

When the app crashes the main method return line is highlighted in green with the message Thread1: signal SIGABRT In the output window, and depending on which of the above properties is linked, I see:

2014-10-20 12:42:26.411 Testbed[2451:283149] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key scroll.'

or:

2014-10-20 12:52:06.986 MTH[2578:299917] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key v.'

Adding sub views like this

[self.v addSubview:button];

Access subviews like this

UIScrollView *scrollview = (UIScrollView *) _scroll;
UIView *View = (UIView *) scrollview.subviews[0];

I found the answer. In the ViewDidLoad method of my view controller's custom class I used the following:

NSArray *sv = self.view.subviews;
UIScrollView *sc = sv[0];
sv = sc.subviews;
UIView *v = sv[0];

I stepped through execution to confirm the ScrollView was the first subview of the view, and to confirm the UIView was the first subview of the ScrollView.

If I needed to access this view beyond the ViewDidLoad method, I would create the variable in the header file.

If you want to add a subview to a view programatically you need to instantiate it and the add it to the subView, like so:

UIButton *button = [UIButton new];
[button setTitle:@"BUTTON!" forControlState:UIControlStateNormal];
[button sizeToFit];

// Do any additional setup for your button here, for example setting the frame.
[self.v addSubview:button];

I can't work out if this is actually what you want to do from your question however. So let me know if I've got what you're attempting wrong!

EDIT 1

Apologies, I misread the question. Looking at the errors you have provided it seems likely that you haven't set the view controller to your custom subclass in the right hand pane on interface builder.

This error could be occurring because although you have linked the IBOutlets the runtime is trying to initialise the incorrect subclass of UIViewController, and therefore cannot find the required properties.

In your storyboard, your view controller does probably not inherit from your custom view controller and thus can not find the iboutlet (this why you can't create the links). Btw : iboutlet should never be strong but weak as they are already retained by the view which is retained by the controller.

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