试图解决我遇到的问题。我想按一个按钮并通过共享表共享URL。这是我到目前为止的内容:
import UIKit
class SideMenu: UIView {
//share app
@IBAction func shareAppBtn(sender: AnyObject ) {
print("shareAppBtn tapped")
let myWebsite = NSURL(string:"http://www.google.com/")
guard let url = myWebsite else {
print("nothing found")
return
}
let shareItems:Array = [url]
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
}
}
这给了我以下错误:
Value of type 'SideMenu' has no member 'presentViewController'
。我认为上面的方法不起作用,因为presentViewController是UIViewcontroller的组件,我从UIView调用它。因此,我尝试了以下尝试,尝试调用UIViewcontroller:
import UIKit
class SideMenu: UIView {
//share app
@IBAction func shareAppBtn(sender: AnyObject, viewController : UIViewController) -> Void
{
print("shareAppBtn tapped")
let myWebsite = NSURL(string:"http://www.google.com/")
guard let url = myWebsite else {
print("nothing found")
return
}
let shareItems:Array = [url]
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
viewController.presentViewController(activityViewController, animated: true, completion: nil)
}
}
但是上面给了我以下错误:
unrecognized selector sent to instance 0x7fe93acb6720'
。所以我终于尝试了:
import UIKit
class SideMenu: UIView {
//share app
@IBAction func shareAppBtn(sender: AnyObject) {
print("shareAppBtn tapped")
let myWebsite = NSURL(string:"http://www.google.com/")
guard let url = myWebsite else {
print("nothing found")
return
}
let shareItems:Array = [url]
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
self.window?.rootViewController?.presentViewController(activityViewController, animated: true, completion: nil)
}
}
但是上面给了我以下错误:
Warning: Attempt to present <UIActivityViewController: 0x7fe03e04c600> on <RAMAnimatedTabBarController.RAMAnimatedTabBarController: 0x7fe03bc4a170> which is already presenting <SideMenu.UISideMenuNavigationController: 0x7fe03c094200>
我在这里想念什么?我想在UIView上调用共享表。
谢谢。
最佳答案
这正在按预期方式工作。UIViews不应呈现视图。他们应该向代表进行报告,由他们来处理。
在SideMenu
的情况下,您的UIView需要一个委托来处理它的工作。设置将SideMenu作为代表的视图控制器。
因此,我使用了一些示例代码。
protocol SideMenuDelegate {
func sideMenuDidShare()
}
委托协议是处理SideMenu视图的视图控制器将实现的协议。
class SideMenu: UIView {
var delegate: SideMenuDelegate?
}
在SideMenu视图类中,您将添加一个委托属性。
class SideMenuViewController: UIViewController, SideMenuDelegate {
func sideMenuDidShare() {
print("this is where your share code goes and where you can present other viewControllers from")
}
override func viewDidLoad() {
super.viewDidLoad()
let sideMenu = self.view as? SideMenu
sideMenu?.delegate = self
}
}
在处理SideMenu的视图控制器中,您将实现SideMenuDelegate协议,例如
class SideMenuViewController: UIViewController, SideMenuDelegate
这样做可以使您的SideMenu在发生某些情况(在本例中为已实现的
func sideMenuDidShare()
)时,将其告知委托人(ViewController)。关于ios - 从UIView调用共享表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39284055/