2015-05-04 28 views
5

Mam dwa kontrolery. Pierwszy z nich ma pasek nawigacyjny z przyciskiem Zakładki, który wciskam, aby wyświetlić drugi kontroler z widokiem tabeli wewnątrz.Swift: Odrzuć PopOver po wybraniu komórki widoku tabeli

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

Chcę móc wybrać jedną komórkę, a następnie odrzucić pogląd popover. The layout is as seems

Jeszcze jedno dziwne zachowanie polega na tym, że pierwsza komórka wyświetla kontrolkę podglądu alertów, która działa dobrze na iPhonie, ale na iPadzie nagle zmienia się rozmiar okna podglądu kontrolera.

enter image description here

tutaj jest mój główny kontroler widok:

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 
} 


} 

i tu jest moje 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() 
} 



} 

Dzięki.

Odpowiedz

2

self.dismissViewControllerAnimated (prawda, realizacja: zero)

w menuViewController wystarczy odwołać popover. Więc kod będzie wyglądać następująco:

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

muszę się go pozbyć, zanim zostanie wyświetlony alert kontroler nie wtedy, gdy przycisk Anuluj kliknięciu –

+0

Allright, następnie wystarczy umieścić self.dismissViewControllerAnimated (true, zakończenie: nil) z zewnątrz blok cancelAction. Tak zredagowałem swoją odpowiedź. – dadalar

+0

@KorayBirand czy znalazłeś sposób na przywrócenie danych wybranych do kontrolera głównego? – Saty