Swift : 选择表格视图单元格时关闭 PopOver
Swift : Dismiss a PopOver when a tableview cell is selected
我有两个控制器。第一个有一个带有书签按钮项目的导航栏,我按下它来显示我的第二个控制器,里面有一个表格视图。
Example project from this link : http://www.koraybirand.co.uk/download/xcode/PopTest.zip
我希望能够 select 一个单元格然后关闭弹出窗口视图。
另一个奇怪的行为是第一个单元格显示一个警报 viewcontroller,它在 iPhone 上工作正常,但在 iPad 上,弹出窗口突然调整大小为 alerviewcontroller的尺寸。
这是我的主视图控制器:
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "popoverSegue" {
let popoverViewController = segue.destinationViewController as! UIViewController
popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
popoverViewController.popoverPresentationController!.delegate = self
}
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
}
这是我的弹出窗口:
class menuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var links : [String] = ["Share", "Manage"]
var currentPopover:UIPopoverController!
@IBOutlet weak var tableView: UITableView!
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return links.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.item == 0 {
let alertController = UIAlertController(title: "Share", message: "No Bookmarks to Share", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Dismiss", style: .Cancel) { (_) in }
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true) {}
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = links[indexPath.row] as String
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
谢谢。
self.dismissViewControllerAnimated(真,完成:无)
在 menuViewController 中足以关闭弹出窗口。所以代码将如下所示:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.item == 0 {
let alertController = UIAlertController(title: "Share", message: "No Bookmarks to Share", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Dismiss", style: .Cancel) { (_) in }
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true) {}
self.dismissViewControllerAnimated(true, completion: nil)
}
}
我有两个控制器。第一个有一个带有书签按钮项目的导航栏,我按下它来显示我的第二个控制器,里面有一个表格视图。
Example project from this link : http://www.koraybirand.co.uk/download/xcode/PopTest.zip
我希望能够 select 一个单元格然后关闭弹出窗口视图。
另一个奇怪的行为是第一个单元格显示一个警报 viewcontroller,它在 iPhone 上工作正常,但在 iPad 上,弹出窗口突然调整大小为 alerviewcontroller的尺寸。
这是我的主视图控制器:
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "popoverSegue" {
let popoverViewController = segue.destinationViewController as! UIViewController
popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
popoverViewController.popoverPresentationController!.delegate = self
}
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
}
这是我的弹出窗口:
class menuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var links : [String] = ["Share", "Manage"]
var currentPopover:UIPopoverController!
@IBOutlet weak var tableView: UITableView!
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return links.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.item == 0 {
let alertController = UIAlertController(title: "Share", message: "No Bookmarks to Share", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Dismiss", style: .Cancel) { (_) in }
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true) {}
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = links[indexPath.row] as String
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
谢谢。
self.dismissViewControllerAnimated(真,完成:无)
在 menuViewController 中足以关闭弹出窗口。所以代码将如下所示:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.item == 0 {
let alertController = UIAlertController(title: "Share", message: "No Bookmarks to Share", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Dismiss", style: .Cancel) { (_) in }
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true) {}
self.dismissViewControllerAnimated(true, completion: nil)
}
}