简体   繁体   中英

Cannot control-drag in xcode between cocoa touch uiview class and subview in storyboard

I'm new to xcode and iOS . I'm following apple tutorial, but I can't figure out a thing.

In the tutorial, when there is the need of a connection between the storyboard and the ViewController.swift , it's done a connection using the control-drag from the storyboard to the viewController.swift , and it works like a charm.

If I add a subview to storyboard , apple does the connection and the declaration by code in a custom cocoa touch class that implements UiView Protocol , not using the control-drag .

Now, given the fact that is clear to me the code writted by apple, I'm just wondering why the control-drag doesn't work.

I've tried to do the same thing, adding a button via ui and then tried to control-drag to cocoa touch class, and I cannot do this.

But if I do the same thing, but trying to connect it to the ViewController.swift it works perfectly.

Now, I'm just wondering why this happens. Can't I use control-drag with a custom UiView ?

Thanks for answering.

You can only control-drag from outlet to view on xib, after creating outlet in code you will see a dot at the left side, you can drag from there to your view and outlet will work

在此处输入图片说明

In the storyboard, you have to set the class name to the view controller. 在此处输入图片说明

If you want to create a custom view that will have subview of it's own, you can do so using a Xib file (pretty much like a storyboard, but for only one view instead of for a whole app workflow) and then you'll be able to make all the connection you wish. Storyboard is not meant to design your customView. Connection are mainly used to provide a easy/cost-effective reach from the control grasp to it's main view's subviews (IBOutlets) or to '0 line' a target action pattern. One exception is with the custom tableView / collectionView cell though..

For connectioning between storyboard & class

you must in the storyboard -> customClass -> chose your class

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