如何从Swift中的模态弹出窗口调用控制器?

时间:2015-07-27 21:49:29

标签: ios swift

我有三个控制器,我需要这个导航:

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

第一个显示使用popover 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)
}
}

2 个答案:

答案 0 :(得分:0)

你的第一个视图控制器应该是一个PopoverViewControllerDelegate,而不是一个basketDelegate。

答案 1 :(得分: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) {
    }
}