【发布时间】:2016-12-28 07:12:09
【问题描述】:
是否可以从我之前的项目中访问视图控制器?
我想要实现的是以编程方式将我以前项目的视图控制器用于我的新项目。在 Swift 3 中。
【问题讨论】:
标签: ios swift swift3 xcode8 viewcontroller
是否可以从我之前的项目中访问视图控制器?
我想要实现的是以编程方式将我以前项目的视图控制器用于我的新项目。在 Swift 3 中。
【问题讨论】:
标签: ios swift swift3 xcode8 viewcontroller
您可以使用框架目标来包含几乎任何您想要的通用代码,包括 UIView。
在您的框架中:
open func commonView() -> UIView {
// code to create view or call up xib file from framework
}
在您的应用中:
import MyFramework
class MainViewController:UIViewController {
override viewDidLoad() {
self.view = commonView()
}
}
但是如果你想让它成为一个 UIViewController 呢?我不太确定这是否可以轻松工作。也许如果您在应用程序中将其设置为 VC 的子 VC,则在 VC 的视图和逻辑中拆分所有内容。在这种情况下,您可能最好使用@Lion 为您提供的更加手动的解决方案。
如果您有兴趣,请here's 从我几周前回答的问题中获得有关设置框架目标的更详细说明。
编辑根据评论,这里还有一些可能会有所帮助。首先,请记住我最初的答案在末尾包含一个链接,该链接可以帮助您在 Xcode 中进行设置。此外,重要的——也许是最重要的——是正确地设计事物!
假设您希望使用通用代码来添加一组看起来相同的按钮,并且您可以连接到特定应用程序中的特定功能。
首先,了解您可以做什么和不可以做什么,这取决于平台。你有 Cocoa (Foundation) 和 Cocoa Touch (UIKit)。换句话说,你有一个 NSButton 和一个 UIButton。我不知道你可以简单地制作一个返回“按钮”的框架。 (但框架可以返回 Core Image 过滤器输出,因为它与平台无关。)
把事情限制在 UIKit 上,让我们谈谈故事板和视图控制器。我对后者感到很困惑。 “视图”和“视图控制器”之间存在巨大差异。 昨天我因此对一个问题进行了多次编辑。 UIView 是 UIKit 中几乎所有控件的基础。 UIViewControllers 包含处理视图的逻辑。 (顺便说一句,这是 MVC 101。)
Storyboards 是 Xcode 特有的东西,因此,我不是一个很好的来源来说明你可以如何“重用”它们。它们可能是你可以做的——毕竟它们是 Xcode 使用的特定 XML 格式。
虽然您可能无法在框架调用中使用 UIViewController 作为返回值(至少不容易),但您可以使用任何 UIView - 及其子类。你猜怎么着? UIButton就是这样的东西。
Swift 引入了很多框架。扩展和便利初始化器是两个特性。假设您将其放入框架中:
extension UIButton {
convenience public init(
title:String,
titleColor:UIColor,
titleFontSize:CGFloat,
image:UIImage,
titleEdgeInsets:UIEdgeInsets,
imageEdgeInsets:UIEdgeInsets,
tintColor:UIColor,
backgroundColor:UIColor,
borderColor:CGColor,
borderWidth:CGFloat,
cornerRadius:CGFloat
) {
self.init(type: .system)
self.setTitle(title, for: .normal)
self.setTitleColor(titleColor, for: .normal)
self.titleLabel?.font = UIFont.systemFont(ofSize: titleFontSize)
self.setImage(image, for: .normal)
self.titleEdgeInsets = titleEdgeInsets
self.imageEdgeInsets = imageEdgeInsets
self.tintColor = tintColor
self.backgroundColor = backgroundColor
self.layer.borderColor = borderColor
self.layer.borderWidth = borderWidth
self.layer.cornerRadius = cornerRadius
self.translatesAutoresizingMaskIntoConstraints = false
}
}
如果你把它和这个结合起来:
public func returnImage(_ named:String) -> UIImage {
let myBundle = Bundle.init(identifier: "com.company.framework")
let imagePath = (myBundle?.path(forResource: "images", ofType: "bundle"))! + "/" + named
let theImage = UIImage(contentsOfFile: imagePath)
return theImage!
}
[将“com.company.name”替换为您的框架 ID,将“images”替换为您的包的名称]
只要您导入了框架,您就可以在您的应用中执行此操作:
let openCamera = UIButton(
title: "Camera",
titleColor: UIColor.yellow,
titleFontSize: 15,
image: returnImage("Camera"),
titleEdgeInsets: UIEdgeInsets(top: 0, left: -51, bottom: -49, right: 0),
imageEdgeInsets: UIEdgeInsets(top: 0, left: 0, bottom: 23, right: -51),
tintColor: UIColor.yellow,
backgroundColor: UIColorFromRGB(0x1F1F1F),
borderColor: UIColor.yellow.cgColor,
borderWidth: 2,
cornerRadius: 10)
我仍在学习 Swift 和框架的精妙之处……可选项等。但是你也可以在你的框架中这样做:
public class ToolBoardCloseButton: UIButton {
convenience init(
tag :Int,
target :Any,
action :Selector
)
{
self.init(frame: CGRect.zero)
self.backgroundColor = sgRed
self.setTitle("Close", for: .normal)
self.tintColor = UIColor.white
self.layer.cornerRadius = 5
self.layer.borderColor = UIColor.white.cgColor
self.layer.borderWidth = 1
self.tag = tag
self.addTarget(target, action: action, for: .touchUpInside)
}
}
这意味着在您的应用中:
closeButton = ToolBoardCloseButton(tag: tag, target: target, action: hideAction)
当然,如果您对此有控制权,请将您的目标和操作设为可选(如果您需要,我没有)。这样 - 只要你为它编码 - 你就可以在你的调用中使它们成为可选的。
最后一点。在我的框架中,“hideAction”背后的 UI 逻辑是 ToolBoard 类的一部分,它是 UIView 的子类。我的应用在 视图控制器 中的所有功能是 (1) 实例化工具栏、工具栏按钮和板(以及它们的大小),(2) 创建关闭按钮和对框架的调用关闭它,然后留下 (3) 特定于应用程序的代码。虽然您可能找不到将 UIViewController 移动到框架调用中的方法,但您应该能够将所有常用代码和视图移动到一个中。
【讨论】:
您不可能从当前项目中打开另一个项目的viewcontroller。为此,您必须将特定的viewcontroller 添加到您的项目中!您可以复制并粘贴 viewcontroller,它可以轻松地从一个项目转移到另一个项目!是的,您可以从您的应用程序中打开另一个已安装的应用程序(但这不是您想要的,正如我从您的问题中所理解的那样)!
【讨论】: