如何在 Swift 4 中显示没有 UIViewController 的新屏幕?
How to present a new screen without a UIViewController in Swift 4?
我还没有实现 UIViewController,因为我已经从另一个 class 继承了它,它给出了 present is not member of this class
的错误
func shareAppLink() {
let name = "http://aijaz.com"
let items = [name] as [Any]
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
present(ac, animated: true)
}
present(_:animated:completion:)
是 UIViewController
的方法,因此必须在 UIViewController
.
的某种类型上调用它
如果您的 class 最初是由视图控制器创建的,那么您可以尝试使用 delegation pattern:
传递引用
Delegation is a simple and powerful pattern in which one object in a
program acts on behalf of, or in coordination with, another object.
The delegating object keeps a reference to the other object—the
delegate—and at the appropriate time sends a message to it. The
message informs the delegate of an event that the delegating object is
about to handle or has just handled.
如果您为您的自定义 class 创建了一个类似这样的协议:
protocol MyClassDelegate {
func shareAppLink()
}
然后您可以在您的视图控制器中遵守该协议并调用如下方法:delegate.shareAppLink()
您还可以使用 Respoder Chain 获取视图的父视图控制器
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if let viewController = parentResponder as? UIViewController {
return viewController
}
}
return nil
}
}
并像
一样声明您的 shareAppLink 函数
func shareAppLink(sender : UIView) {
let name = "http://aijaz.com"
let items = [name] as [Any]
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
sender.parentViewController(ac, animated: true)
}
然后在didSelectRowAt中,你可以这样称呼它:
self.shareAppLink(sender : cell)
你必须继承 UIViewController subclass 或 UIViewController class。通过这样做应该解决错误。
我还没有实现 UIViewController,因为我已经从另一个 class 继承了它,它给出了 present is not member of this class
的错误func shareAppLink() {
let name = "http://aijaz.com"
let items = [name] as [Any]
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
present(ac, animated: true)
}
present(_:animated:completion:)
是 UIViewController
的方法,因此必须在 UIViewController
.
如果您的 class 最初是由视图控制器创建的,那么您可以尝试使用 delegation pattern:
传递引用Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled.
如果您为您的自定义 class 创建了一个类似这样的协议:
protocol MyClassDelegate {
func shareAppLink()
}
然后您可以在您的视图控制器中遵守该协议并调用如下方法:delegate.shareAppLink()
您还可以使用 Respoder Chain 获取视图的父视图控制器
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if let viewController = parentResponder as? UIViewController {
return viewController
}
}
return nil
}
}
并像
一样声明您的 shareAppLink 函数func shareAppLink(sender : UIView) {
let name = "http://aijaz.com"
let items = [name] as [Any]
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
sender.parentViewController(ac, animated: true)
}
然后在didSelectRowAt中,你可以这样称呼它:
self.shareAppLink(sender : cell)
你必须继承 UIViewController subclass 或 UIViewController class。通过这样做应该解决错误。