2015-05-04 129 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

相關問題