2015-05-04 85 views
5

我有两个控制器。第一个有一个导航栏,里面有一个书签按钮,我按下来显示我的第二个控制器,里面有一个tableview。Swift:当选择一个tableview单元格时关闭PopOver

Example project from this link : http://www.koraybirand.co.uk/download/xcode/PopTest.zip

我希望能够选择一个单元格,然后关闭该酥料饼的视图。 The layout is as seems

另外一个奇怪的现象是,第一个单元格显示警告视图 - 控制其在iPhone上,但在iPad上的酥料饼的工作正常,突然调整到alerviewcontroller的大小。

enter image description here

这里是我的主视图控制器:

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() 
} 



} 

感谢。

回答

2

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) 
    } 
} 
+0

我需要关闭它显示之前的警报控制器不是当取消按钮被点击 –

+0

还好吧,那只是把self.dismissViewControllerAnimated(真,完成:无)的外cancelAction的块。我已经编辑了我的答案。 – dadalar

+0

@KorayBirand你有没有想办法把选定的数据带回主视图控制器? – Saty

相关问题