简体   繁体   English

如何在 Storyboard 中将 ViewController.swift 连接到 ViewController?

[英]How to connect ViewController.swift to ViewController in Storyboard?

I made a new view controller in Storyboard and made a new .Swift file that inherits UIViewController .我在 Storyboard 中创建了一个新的视图控制器,并创建了一个继承UIViewController的新 .Swift 文件。 However, my viewDidLoad() is not being called.但是,我的viewDidLoad()没有被调用。

What do I need to do to connect my view controller in my story board to my .swift code so viewDidLoad() is called?我需要做什么才能将我的故事板中的视图控制器连接到我的 .swift 代码以便调用viewDidLoad()

I see there's this: How to connect storyboard to viewcontroller我看到有这个: How to connect storyboard to viewcontroller

However, in a default project, FirstViewController(storyboard) doesn't have FirstViewController in that box, it is empty, yet viewDidLoad() is still called.但是,在默认项目中, FirstViewController(storyboard) 在那个框中没有 FirstViewController,它是空的,但viewDidLoad()仍然被调用。 It doesn't make sense.这没有意义。

  1. Choose your ViewController in the storyboard or scene list (mine is called Circuit ).在情节提要或场景列表中选择您的 ViewController(我的称为Circuit )。
  2. Click on the Identity Inspector on the right单击右侧的Identity Inspector
  3. Select Custom Class > Class > Type name of your swift file .选择Custom Class > Class > Type name of your swift file

DONE :)完毕 :)

这是图像上的这两个步骤(我知道丑陋)

Yo have your ViewControler created with some Objects in it ( UILabel , UIButton , UIImage ...)哟,你的ViewControler创建了一些对象( UILabelUIButtonUIImage ...)

在此处输入图片说明

1 - You need to link your ViewControler in your story board to your ViewController.swift , to do this follow the pictures 1 - 您需要将您的故事板中的ViewController.swift链接到您的ViewController.swift ,按照图片执行此操作

在此处输入图片说明

2 - In the class filed put the name of the ViewController class. 2 - 在类字段中输入 ViewController 类的名称。 With that you just linked your storyBoard view controller to your viewController.swift class有了它,您只需将您的 storyBoard 视图控制器链接到您的viewController.swift

Class = ViewController.swift类 = ViewController.swift

在此处输入图片说明

3 - Now you need to cretae the variables you want to asign(UILabel, UIButton ... that you have in your storyboard): in this example: 3 - 现在你需要创建你想要分配的变量(UILabel, UIButton ... 你在你的故事板中):在这个例子中:

class MovieDetailViewController: UIViewController, DetailView {
    
    var presenter: MovieDetailPresenter!
    var movie: Movie?
    
    @IBOutlet weak var lblMovieYear: UILabel!
    @IBOutlet weak var lblMovieTitle: UILabel!
    @IBOutlet weak var movieImage: UIImageView!
    @IBOutlet weak var lblMovieRating: UILabel!
    @IBOutlet weak var lblMovieOverview: UILabel! 
}

4 - To link the UILabel in the story board to your UILabel variable or your UIButton in your story board to your UIButton var, follow the next images 4 - 要将故事板中的 UILabel 链接到您的 UILabel 变量或将故事板中的 UIButton 链接到您的 UIButton 变量,请按照以下图像进行操作

First select the view controller in the storyboard首先在storyboard中选择view controller

在此处输入图片说明

Second select the parragraf icon in the right up corner ands clicked 'Assistant', it will open a screen of your ViewControler.swift其次选择右上角的 parragraf 图标并单击“助手”,它将打开您的ViewControler.swift屏幕

在此处输入图片说明 在此处输入图片说明

Now you just need to drag the variable to the corresponding object and you will be done.现在你只需要把变量拖到对应的对象上就可以了。 在此处输入图片说明

Remember to do this with all variables, you will need to create a variable for each object you have in the storyboard请记住对所有变量执行此操作,您需要为故事板中的每个对象创建一个变量

Connect storyboard and view controller连接故事板和视图控制器

You should set ViewController in Class` field您应该ViewController in Class` 字段中设置ViewController in

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xcode从viewcontroller.swift跳转到storyboard - Xcode jump from viewcontroller.swift to storyboard xcode:故事板不反映ViewController.swift中代码中所做的更改 - xcode : storyboard does not reflect changes made in code in ViewController.swift 如何从 viewcontroller.swift 文件导航到 *.storyboard 文件中的相应场景? - How navigate from viewcontroller.swift file to corresponding scene in *.storyboard file? 如何使用iOS的Swift 3在ViewController.swift中实现UICollectionView - How to implement UICollectionView within ViewController.swift using Swift 3 for iOS 什么是ViewController.swift(接口)文件 - 在Counterparts中 - What is the ViewController.swift (Interface) file for - in Counterparts 从Main.storyboard拖放到ViewController.swift时,我无法更改连接和对象 - I can't change connection and object when drag and drop from Main.storyboard to ViewController.swift 在目标C文件中导入ViewController.swift - Import ViewController.swift in Objective C file 无法控制拖动到ViewController.swift - Cannot Control-Drag into ViewController.swift 在 Xcode 中初始化新项目时没有 main.storyboard 或 ViewController.swift? - No main.storyboard or ViewController.swift upon initialization of new project in Xcode? Xcode:在 ViewController.swift 中禁用视图的旋转 - Xcode: Disable rotation for a view in the ViewController.swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM