2015-05-15 3 views
1

В моем приложении iOS 8 у меня есть пользовательский ViewController, который я представляю как Popover. Этот ViewController имеет делегат, который получает и отправляет родительский ViewController, щелкнув индекс во всплывающем окне. Проблема в том, что я не могу уволить этого Поповера после selectRow.Отклонить Popover ViewController от Tableviewcell в Swift

Вот код:

Это метод, который я называю, когда я хочу, чтобы показать мою Popup.

@IBAction func registerButtonAction(sender: UIButton) { 
    popup = self.storyboard!.instantiateViewControllerWithIdentifier("PopupViewController") as? PopupViewController 
    popup!.modalPresentationStyle = .Popover 
    popup!.preferredContentSize = CGSizeMake(100, 120) 
    let popoverMenuViewController = popup!.popoverPresentationController 
    popoverMenuViewController?.permittedArrowDirections = .Up 
    popoverMenuViewController?.delegate = self 
    popoverMenuViewController?.sourceView = sender 
    popoverMenuViewController?.sourceRect = CGRect(
     x: sender.frame.size.width/2, 
     y: sender.frame.size.height/2, 
     width: 1, 
     height: 1) 
    popup!.delegate = self 

    presentViewController(
     popup!, 
     animated: true, 
     completion: nil) 

} 

Вот PopupViewController код:

protocol PopupViewControllerDelegate 
{ 
    func rowClickedAtIndex(var index : Int) 
} 

class PopupViewController: MainViewController { 

    var delegate : PopupViewControllerDelegate? 

    @IBOutlet weak var tableView: UITableView! 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

} 

extension PopupViewController:UITableViewDelegate{ 

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { 
     var cell:PopupTableViewCell? = tableView.dequeueReusableCellWithIdentifier("PopupTableViewCell") as? PopupTableViewCell 

     if cell == nil { 
      cell = PopupTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "PopupTableViewCell") 
     } 

     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     if (self.delegate != nil) 
     { 
      self.delegate!.rowClickedAtIndex(indexPath.row) 
      self.dismissViewControllerAnimated(true, completion: nil) 
     } 

    } 

    func numberOfSectionsInTableView(tableView: UITableView?) -> Int { 
     return 1 
    } 

    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{ 
     return 2 
    } 
} 

Спасибо.

ответ

2

Переведите ваш dismissViewControllerAnimated(true, completion: nil) вызов в делегат в конце метода clickedRowAtIndex. Другими словами, контроллер представления представления должен вызывать отклонение, а не представленный контроллер представления.

Попробуйте это:

extension WelcomeViewController: PopupViewControllerDelegate { 
    func rowClickedAtIndex(index: Int) {  
     dismissViewControllerAnimated(true, completion: nil) 
     println(index) 
    } 
} 
+0

Я попытался здесь: расширение WelcomeViewController: PopupViewControllerDelegate { функ rowClickedAtIndex (индекс: Int) { всплывающее окно .dismissViewControllerAnimated? (Правда, завершение: ноль) Println (индекс) }} Но не работает. Он проходит в этом методе, потому что он печатает индекс, но он не отклоняет его. – Bellots

+0

Похоже, что 'popup' все еще пытается выполнить вызов. 'WelcomeViewController' должен выполнить его. 'rejectViewControllerAnimated (true, completion: noil)' – Frankie

+0

Что мне делать по вашему мнению? – Bellots

3

Я решил сам:

Я проверил, что проблема не в том, что поповер не был уволен, но он был уволен, но после разных секунд.

Таким образом, я положил свой вызов в основной поток, и он работал отлично. Вот код:

extension WelcomeViewController: PopupViewControllerDelegate { 
    func rowClickedAtIndex(index: Int) { 
     dispatch_async(dispatch_get_main_queue(),{ 
      self.dismissViewControllerAnimated(true, completion: nil) 
      println(index) 
     }) 
    } 
} 

Я хочу поблагодарить вас Фрэнки, который помог мне в поиске решения, убрав то, что не должно быть проблемой.

+0

Добро пожаловать! Рад, что я смог помочь. – Frankie

Смежные вопросы