简体   繁体   English

STPopup 如何在 Swift 中初始化?

[英]STPopup how to initialize in Swift?

Im playing with this library .我在玩这个图书馆 It says that I have to init it with code它说我必须用代码初始化它

STPopupController *popupController = [[STPopupController alloc] initWithRootViewController:[ViewController new]];
[popupController presentInViewController:self];

I try to do this with following code (HelpersNear is name of UIViewController)我尝试使用以下代码执行此操作(HelpersNear 是 UIViewController 的名称)

var popup = STPopupController(rootViewController: HelpersNear)
popup.presentInViewController(self)

but in response I get compile error which offers me to delete rootViewController from constructor.但作为回应,我收到编译错误,这让我可以从构造函数中删除rootViewController After I remove it, I get another error.删除它后,我收到另一个错误。 在此处输入图片说明

What am I doing wrong ?我究竟做错了什么 ?

Try this code:试试这个代码:

var popup = STPopupController(rootViewController: HelpersNear())
popup.presentInViewController(self)

For example:例如:

class FooViewController: UIViewController {  
  let name: String

  init() {
    name = "Bar"
    super.init(nibName: nil, bundle:nil)
  }

  required init(coder: NSCoder) {
    super.init(coder: coder)
  }
}

There is easy way to use this library in Swift.在 Swift 中有很简单的方法可以使用这个库。 Below I put example for BottomSheet style popup using STPopupController.下面我使用 STPopupController 放置了BottomSheet 样式弹出窗口的示例。

  1. Create UIViewController or UITableViewController in Storyboard (you can use example done in Example Project (STPopupExample) of STPopup library available on github).在 Storyboard 中创建 UIViewController 或 UITableViewController(您可以使用在 github 上可用的 STPopup 库的示例项目(STPopupExample)中完成的示例)。

  2. Set the view controller's storyboard id to f.ex.: MultiselectionTableViewController视图控制器的故事板ID设置f.ex: MultiselectionTableViewController

  3. To show this popup view use the following code:要显示此弹出视图,请使用以下代码:

     let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewControllerWithIdentifier("MultiselectionTableViewController") as! MultiselectionTableViewController vc.title = "Choose your options" vc.items = ["First element", "Second element", "Third element"] vc.delegate = self let popup = STPopupController(rootViewController: vc) popup.style = STPopupStyle.BottomSheet dispatch_async(dispatch_get_main_queue(), { popup.presentInViewController(self.parentViewController) })
  4. It is important to show popup inside dispatch_async block.dispatch_async块中显示弹出窗口很重要。 It prevents lags in showing new view controller.它可以防止显示新视图控制器的延迟。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM