2015-07-27 5 views
0

У меня есть три контроллера, и мне нужна эта навигация:Как вызвать контроллер из модального popover в Swift?

| 1stVC | -> | popoverVC | -> | 2ndVC |

Первый показывает модальный вид, используя popper segue, а затем из модального представления, используя протокол, он должен отображать второй контроллер.

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

Одна важная вещь, firstViewcontroller вызывается из другого контроллера, который находится в tabbarcontoller.

Заранее спасибо.

Это код:

PopoOverController:

import UIKit 
protocol basketDelegate { 
func didSelectValue() 
} 
class PopoverViewController: UITableViewController { 
var delegate: basketDelegate! 
let options = ["Filters", "Basket"] 
    override func viewDidLoad() { 
    super.viewDidLoad() 
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier") 
    let rdc = storyboard!.instantiateViewControllerWithIdentifier("FirstViewController") as! FirstViewController 
    self.delegate = rdc 
} 

// MARK: - Table view data source 
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return options.count 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) 
    cell.textLabel?.textAlignment = .Center 
    cell.textLabel?.textColor = colorWithHexString("#1C7393") 
    cell.textLabel?.text = options[indexPath.row] 
    return cell 
} 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 
    delegate?.didSelectValue() 
} 
} 

FirstController

import UIKit 

class FirstViewController: AEAccordionTableViewController, 
UIPopoverPresentationControllerDelegate, UISearchBarDelegate,basketDelegate { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    var Filters = PopoverViewController() 
    Filters.delegate = self 

} 

// MARK: - Popup filter call 
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 
} 


func didSelectValue() { 

    //Option 1--Failed! 
    let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil); 
    let SecondViewControllerObject : UIViewController = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController; 
    self.presentViewController(SecondViewControllerObject, animated: true, completion: nil); 

    //Option 2--Failed 
    self.performSegueWithIdentifier("secondSegue", sender: self) 

    //Option 3--Failed 
    let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
    let SecondViewControllerObject : UIViewController = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController; 
    let navigationController = UINavigationController(rootViewController: SecondViewControllerObject) 
    self.presentViewController(navigationController, animated: true, completion: nil) 

    //Option 4--Failed 
    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController 
    FirstViewController()!.presentViewController(vc, animated: true, completion: nil) 

    //Option 5--Failed 
    self.navigationController?.presentViewController(vc, animated: false, completion: nil) 

} 
} 

SecondController

import UIKit 

class SecondViewController: AEAccordionTableViewController { 

@IBOutlet weak var dismissButton: UIButton! 
override func viewDidLoad() { 
    super.viewDidLoad() 

} 
@IBAction func dismissTap(sender: UIButton) { 
    self.dismissViewControllerAnimated(true, completion: nil) 
} 
} 

ответ

0

Это поведение может быть достигнуто с помощью листа действий. Только в первом контроллере добавьте новый метод создания контекстного меню с тем же параметром, чтобы вызвать второй контроллер. Этот метод вызывается с помощью кнопки.

Это простое решение, без протоколов или делегаций.

В первом контроллере:

@IBAction func showSheetASction(sender: UIBarButtonItem) { 
    print("click") 
    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet) 

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in 
     print(action) 
    } 
    alertController.addAction(cancelAction) 

    let CallSVCAction = UIAlertAction(title: "Filters", style: .Default) { (action) in 
     // ... 
     print(action) 
     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController 
     self.presentViewController(vc, animated: true, completion: nil) 
    } 
    alertController.addAction(CallSVCAction) 

    alertController.view.tintColor = colorWithHexString("#1C7393") 

    self.presentViewController(alertController, animated: true) { 
    } 
} 
0

Ваш первый контроллер представления должен быть PopoverViewControllerDelegate, а не basketDelegate.

+0

я уже проверить его, и он генерирует ошибку для множественного наследования. Кстати, почему бы не использовать делегирование протокола? –

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