在 Swift 中使用两个视图控制器连接对象 [英] Connecting Objects with two View controllers in Swift

查看:24
本文介绍了在 Swift 中使用两个视图控制器连接对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有两个视图控制器,但无法将 UIImageView 等对象连接到视图控制器.当我尝试创建 IBOutlet 时,它告诉我无法插入新的插座集合:找不到名为 UIViewController 的类的任何信息".我相信这个问题源于我对我的类的原始声明如下:

In my project I have two view controllers, and I am having trouble connecting objects such as an UIImageView to the view controller. When I try to create the IBOutlet, it tells me that "Could not insert new outlet collection: could not find any information for the class named UIViewController". I believe this problem stems from the fact that my original declaration of my class is as follows:

    class UIViewController: UIViewController {

实际上视图控制器被命名为 mainScene.但是,当我将第一个 UIViewController 更改为我认为应该是(mainScene)时,它甚至没有显示连接 IBOutlet 的选项...

when in fact the view controller is named mainScene instead. However, when I change the first UIViewController to what I think it should be (mainScene), it doesn't even show me the option of connecting an IBOutlet...

    class mainScene: UIViewController {

所以,我有两个问题.

  1. 我是否需要为第二个 UIViewController 设置一个完整的单独类,这样可以解决我的问题吗?

  1. Do I need to have a whole separate class for the second UIViewController and would that solve my issues?

是否有更好的方法将对象链接到 UIViewController 或者我只是在做一些非常错误的事情(可能的情况)?

Is there a better way to link objects to the UIViewController or am I just doing something horribly wrong (the likely scenario)?

非常感谢

推荐答案

简短回答:1. 是的,是的.2. 没有更好的方法,而且您并没有做错什么.(您可能只是错过了一步.)

Short answer: 1. Yes, and yes. 2. There's no better way, and you're not doing something horribly wrong. (You probably just missed a step.)

你有两个视图控制器.假设它们不同,您将从 UIViewController 子类中使用不同的名称.例如,mainSceneViewController 和 otherSceneViewController.

You have two view controllers. Assuming they are different, you would subclass each one from UIViewController with a different name. E.g., mainSceneViewController and otherSceneViewController.

您的 mainSceneViewController 和 otherSceneViewController 将各自拥有自己的属性和 IBOutlets.

Your mainSceneViewController and otherSceneViewController would each have their own properties and IBOutlets.

您可能遇到的问题是,需要在 Interface Builder 中更改 viewController 的类以匹配其 .swift 文件中的类名,以便 IB 知道它可以为该视图控制器连接哪些插座.

Where you're probably stuck, is needing to change the class of your viewController within Interface Builder to match the class name in its .swift file, so IB knows what outlets it can connect for that view controller.

故事板中的每个场景都对应一个视图控制器.执行转场后,iOS 会从故事板实例化您的视图控制器.

Each scene in your storyboard corresponds to a view controller. When the segue is performed, iOS instantiates your view controller from the storyboard.

虽然可能只有一个视图控制器子类,并且对不同的视图使用相同的子类,但这种情况并不经常发生.

While it is possible to only have one view controller subclass, and use the same subclass for different views, it doesn't happen too often.

更新:

子类化允许您向类添加属性和方法,并覆盖它们的超类.

Subclassing lets you add properties and methods to a class, and override their superclass.

在您的评论中,UIViewController 是类,mainSceneViewControllerUIViewController 的子类.对于您的第二个视图控制器,它可能是 class otherSceneViewController: UIViewController {,因为您的其他场景可能需要与主场景视图控制器不同的属性和方法.

In your comment, UIViewController is the class, and mainSceneViewController is subclassed from UIViewController. For your second view controller, it would likely be class otherSceneViewController: UIViewController {, as your other scene would likely require a different properties and methods from your main scene view controller.

您可以阅读有关继承的更多信息在 Swift 编程语言指南中.

You can read more about Inheritance in the Swift Programming Language guide.

这篇关于在 Swift 中使用两个视图控制器连接对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆