关于Xcode 13.2.1中创建ViewController、添加Outlet变量及解决项目界面选项缺失问题的技术咨询
iOS Development Starter Issues in Xcode 13.2.1: Solutions
Hey there! I totally get how overwhelming Xcode can feel when you're just dipping your toes into iOS development—let's work through each of your problems step by step:
1. Missing Interface Options (SwiftUI/UIKit) & No Storyboard/ViewController in Project
The root of this is understanding Xcode 13's project setup flow:
- When starting a new project, make sure you pick the iOS > App template first.
- On the next setup screen, you’ll see an Interface dropdown menu—this is where you choose between SwiftUI and UIKit (Storyboard). If you don’t spot this, double-check you selected the correct template (some niche templates skip this option).
- If you already created a project without this setup (and ended up with only
App.swiftinstead of the expected files), here’s how to add them manually:- Right-click your project in the Project Navigator > New File > Select Storyboard (under iOS > User Interface) > Name it
Mainand click Create. - Go to your project’s target settings > Under General > Deployment Info, set Main Interface to your new
Main.storyboard. - Add a ViewController: Right-click the project > New File > Select UIViewController Subclass (under iOS > Cocoa Touch) > Name it
ViewController, uncheck "Also create XIB file", then click Create. - Open
Main.storyboard, drag a ViewController from the Object Library (right sidebar) onto the canvas. Select this ViewController, go to the Identity Inspector (right sidebar, 3rd icon), and set Class to yourViewControllersubclass to link them.
- Right-click your project in the Project Navigator > New File > Select Storyboard (under iOS > User Interface) > Name it
2. Creating a ViewController in Xcode 13.2.1
The process varies a bit based on your project type:
- UIKit (Storyboard) Project:
- Either drag a ViewController directly onto the Storyboard canvas (as above), then link it to a custom subclass if you need custom logic.
- Or create a
UIViewControllersubclass file first, then associate it with a Storyboard ViewController via the Identity Inspector.
- SwiftUI Project (if you want to use UIKit ViewControllers):
- Create a
UIViewControllersubclass as above, then wrap it in aUIHostingControllerto integrate it into your SwiftUI flow. Example code:struct ViewControllerWrapper: View { var body: some View { UIHostingController(rootView: YourSwiftUIView()).view } }
- Create a
3. Can't Add Outlet Variables
Outlets only work with Storyboard/XIB files—so first confirm your ViewController is linked to a Storyboard:
- Open
Main.storyboardand select the ViewController you want to add an outlet to. - Open the Assistant Editor (top-right icon, looks like two overlapping circles) to show your ViewController’s code side-by-side with the Storyboard.
- Select a UI element (like a Label) in the Storyboard, hold Ctrl, and drag from the element to your ViewController’s code (inside the class, above
viewDidLoad()). - A popup will appear—name your outlet (e.g.,
myLabel) and click Connect. If this fails, double-check that your Storyboard ViewController’s Class is set to your custom ViewController subclass (via the Identity Inspector).
4. Can't Find the Area to Drag Labels
- UIKit (Storyboard) Project: Open
Main.storyboard—the large canvas showing your ViewController’s screen is where you drag elements from the Object Library (search for "Label" in the right sidebar and drag it onto the canvas). - SwiftUI Project: There’s no drag-and-drop UI builder here—you add text by writing SwiftUI code, e.g.,
Text("Hello World")inside yourViewstruct. If you prefer visual editing, switch to a UIKit (Storyboard) project.
内容的提问来源于stack exchange,提问作者Cherryman
相关产品推荐
相关产品推荐

