简体   繁体   中英

iOS superview of UIView added in Storyboard is nil

I ran into some unexpected behavior with my UIViews, so I decided to print out some information about the views and their superviews. When I tried to do this, the superview of my UIViews returned nil.

The UIViews for which I tried to print out the superviews were added to a view controller in Storyboard (via drag and drop from the Object Library) and then connected with the view controller code file with IBOutlet. Also, the correct view controller code file class has been set in the Identity Inspector. Specifically, the views I have are UIPickerView, UILabel, and UITextField. When I try to access the superview of these UIViews I get nil. I was under the impression that when UIViews were added via Storyboard, they become part of the view hierarchy of the view controller. So my question is:

1) Is my assumption wrong? That is, is it suppose to be nil? If so, it would be great to get an explanation about how the view hierarchy is set up when UIView is added using Storyboard, or some reference documents

2) If it isn't suppose to be nil, what could possibly be making the superview nil?

This will happen if the view is added to a size class that is different than the size for which you are running. By changing the size in your storyboard to be W:Any H:Any, you have a catch-all for all device types.

分镜脚本大小类

If superview is nil a view is not in the view hierarchy. You're probably checking before the views have been added. Try checking in the awakeFromNib method. At that point the hierarchy has been set up.

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