2015-05-04 8 views
5

Ich habe zwei Controller. Der erste hat eine Navigationsleiste mit einem Lesezeichen-Button, den ich drücke, um meinen zweiten Controller anzuzeigen, in dem sich ein Tableview befindet.Swift: Beendet einen PopOver, wenn eine Tabellenansichtszelle ausgewählt ist

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

Ich möchte in der Lage sein, eine Zelle auszuwählen und dann die popover Ansicht zu schließen. Ein anderes seltsames Verhalten ist, dass die erste Zelle einen Alarm Viewcontroller anzeigt, der auf einem iPhone gut funktioniert, aber auf einem iPad wird das Popover plötzlich auf die Größe von alerviewcontroller angepasst.

enter image description here

hier ist mein Hauptansicht-Controller:

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 
} 


} 

und hier ist mein popover:

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



} 

Dank.

Antwort

2

self.dismissViewControllerAnimated (true, Fertigstellung: nil)

in menuViewController genug popover zu entlassen. So wird der Code wie folgt aussehen:

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

Ich brauche es zu schließen, bevor der Alarm-Controller wird nicht angezeigt, wenn die Löschtaste angeklickt wird –

+0

Allright, dann setzen Sie einfach self.dismissViewControllerAnimated (true, Fertigstellung: nil), die außerhalb des cancelAction's Block. Ich habe meine Antwort so bearbeitet. – dadalar

+0

@KorayBirand haben Sie gefunden, um Daten zurück zu Main-View-Controller zu holen? – Saty