Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 未设置此应用中的代理_Ios_Swift_Delegates - Fatal编程技术网

Ios 未设置此应用中的代理

Ios 未设置此应用中的代理,ios,swift,delegates,Ios,Swift,Delegates,我有一个带有两个视图控制器的应用程序。。。ViewController和CollectionViewController。我在viewController中有一些过滤函数,可以对加载到此视图控制器的图像执行颜色过滤。CollectionViewController包含一个集合视图,该视图充当带有单元格的水平滚动菜单,当按下单元格时,应调用ViewController中的筛选函数。情节提要有一个ID为“FilterSegue”的显示序列 我让它与通知中心一起工作,但我想尝试让它与协议和代理一起工作

我有一个带有两个视图控制器的应用程序。。。ViewController和CollectionViewController。我在viewController中有一些过滤函数,可以对加载到此视图控制器的图像执行颜色过滤。CollectionViewController包含一个集合视图,该视图充当带有单元格的水平滚动菜单,当按下单元格时,应调用ViewController中的筛选函数。情节提要有一个ID为“FilterSegue”的显示序列

我让它与通知中心一起工作,但我想尝试让它与协议和代理一起工作,以了解这些方法。我收到了一些建议,但尽管进行了多次尝试,仍无法让代表们进行设置。 代码如下:

视图控制器:

import UIKit

class ViewController: UIViewController, FilterDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "FilterSegue" {

            let destvc = segue.destinationViewController as! CollectionViewController 
            destvc.filterDelegate = self

        }
    }


    func onRedFilter() {

        // some code

    }

    func onGreenFilter() {

        // some code

    }


    func onBlueFilter() {

        // some code

    }

    func onUnfiltered() {

        // some code

    }

}
import UIKit

//Protocols for filter functions called by the filter menu collection view custom cells.
protocol FilterDelegate: class {
    func onRedFilter()
    func onGreenFilter()
    func onBlueFilter()
    func onUnfiltered()
}

class CollectionViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate {

    let reuseIdentifier = "FilterCell"

    var filterDelegate: FilterDelegate? = nil


    @IBOutlet var collectionView: UICollectionView!

    // Filter labels for custom filter menu cells.
    var tableData:[String] = ["Red Filter",
                              "Green Filter",
                              "Blue Filter",
                              "Unfilter",
                              "New Filter 1",
                              "New Filter 2"]

    // Filter images for custom filter menu cells.
    var tableImages: [String] = ["waterfallred.png",
                                 "waterfallgreen.png",
                                 "waterfallblue.png",
                                 "waterfall.png",
                                 "waterfall.png",
                                 "waterfall.png"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.


        // Set up collectionView for the filters.

        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()

        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

        layout.itemSize = CGSize(width: 120, height: 80)

        collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)

        collectionView!.registerClass(colvwCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        collectionView!.backgroundColor = UIColor.whiteColor()

        self.view.addSubview(collectionView!)

    }


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


    // Set uo required methods for collection view.
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

        return 1

    }


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return tableData.count

    }


    // Method for custom collection view cell texts and images.
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell: colvwCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! colvwCell

        cell.backgroundColor = UIColor.grayColor()

        cell.lblCell.text = tableData[indexPath.row]
        cell.imgCell.image = UIImage(named: tableImages[indexPath.row])

        return cell

    }


    // Method for calling functions upon pressing custom filter menu collection view cells. In this case, the filter functions in the main view controller are called using notifications.

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        print("Cell \(indexPath.row) selected")

        guard let filterDelegate = filterDelegate else {
            print("Filter delegate wasn't set!")
            return
        }

        switch indexPath.row {

            case 0:
                filterDelegate.onRedFilter()
            case 1:
                filterDelegate.onGreenFilter()
            case 2:
                filterDelegate.onBlueFilter()
            case 3:
                filterDelegate.onUnfiltered()
            default:
                print("No available filter.")

        }

    }

}
CollectionViewController:

import UIKit

class ViewController: UIViewController, FilterDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "FilterSegue" {

            let destvc = segue.destinationViewController as! CollectionViewController 
            destvc.filterDelegate = self

        }
    }


    func onRedFilter() {

        // some code

    }

    func onGreenFilter() {

        // some code

    }


    func onBlueFilter() {

        // some code

    }

    func onUnfiltered() {

        // some code

    }

}
import UIKit

//Protocols for filter functions called by the filter menu collection view custom cells.
protocol FilterDelegate: class {
    func onRedFilter()
    func onGreenFilter()
    func onBlueFilter()
    func onUnfiltered()
}

class CollectionViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate {

    let reuseIdentifier = "FilterCell"

    var filterDelegate: FilterDelegate? = nil


    @IBOutlet var collectionView: UICollectionView!

    // Filter labels for custom filter menu cells.
    var tableData:[String] = ["Red Filter",
                              "Green Filter",
                              "Blue Filter",
                              "Unfilter",
                              "New Filter 1",
                              "New Filter 2"]

    // Filter images for custom filter menu cells.
    var tableImages: [String] = ["waterfallred.png",
                                 "waterfallgreen.png",
                                 "waterfallblue.png",
                                 "waterfall.png",
                                 "waterfall.png",
                                 "waterfall.png"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.


        // Set up collectionView for the filters.

        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()

        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

        layout.itemSize = CGSize(width: 120, height: 80)

        collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)

        collectionView!.registerClass(colvwCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        collectionView!.backgroundColor = UIColor.whiteColor()

        self.view.addSubview(collectionView!)

    }


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


    // Set uo required methods for collection view.
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

        return 1

    }


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return tableData.count

    }


    // Method for custom collection view cell texts and images.
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell: colvwCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! colvwCell

        cell.backgroundColor = UIColor.grayColor()

        cell.lblCell.text = tableData[indexPath.row]
        cell.imgCell.image = UIImage(named: tableImages[indexPath.row])

        return cell

    }


    // Method for calling functions upon pressing custom filter menu collection view cells. In this case, the filter functions in the main view controller are called using notifications.

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        print("Cell \(indexPath.row) selected")

        guard let filterDelegate = filterDelegate else {
            print("Filter delegate wasn't set!")
            return
        }

        switch indexPath.row {

            case 0:
                filterDelegate.onRedFilter()
            case 1:
                filterDelegate.onGreenFilter()
            case 2:
                filterDelegate.onBlueFilter()
            case 3:
                filterDelegate.onUnfiltered()
            default:
                print("No available filter.")

        }

    }

}
guard语句在collection view(集合视图)菜单上任何按下的单元格上返回“Filter delegate was not set!”。

而不是此

protocol FilterDelegate: class {
func onRedFilter()
----
}
试试这个

protocol FilterDelegate {
    func onRedFilter()
    ----
    }
我找到了答案

我意识到没有设置segue,因为它试图用segue实例化另一个集合视图控制器。我从故事板上删除了准备曲和序曲。 我在主视图控制器上有一个名为onFilter2的按钮,它在按下时实例化容器中的集合视图(调用函数showFilterMenu以显示过滤器菜单),然后在第二次按下时隐藏它(hideFilterMenu)。showFilterMenu函数是colectionviewcontroller的实例化位置。因此,我在这个函数中设置了委托。这需要一行代码

下面是在正确调用委托的情况下按下筛选按钮时执行的函数

  // New onFilter button action that shows/hides the filter collection view scroll bar on main view controller.
    @IBAction func onFilter2(sender: UIButton) {

        if filterButtonOn == true {

            if (sender.selected) {

                hideFilterMenu()
                editButton.selected = false
                sender.selected = false

            } else {

                showFilterMenu()
                sender.selected = true
                hideSliderMenu()
                editButton.selected = false


                //Reset color sliders to initial values.
                redHorizontalSlider?.setValue(0, animated: false)
                greenHorizontalSlider?.setValue(0.0, animated: false)
                blueHorizontalSlider?.setValue(0.0, animated: false)

                // Set initial values for color indicators.
                redSliderIndicator.text = "0.00"
                greenSliderIndicator.text = "0.00"
                blueSliderIndicator.text = "0.00"

            }

        }

    }


    // Shows the filter menu scrolling bar in container view upon request.
    func showFilterMenu() {

        let newViewController = self.storyboard?.instantiateViewControllerWithIdentifier("FilterCollectionView") as! CollectionViewController

        newViewController.view.translatesAutoresizingMaskIntoConstraints = false
        self.toggleOnViewController(newViewController)
        self.currentViewController = newViewController

        // Set the delegate for the filter functions called by the cells in the collectionViewController filter menu.

    // Set the delegate for the filter functions called by the cells in the collectionViewController filter menu.

//Here is the line of code that fixes the problem...
        newViewController.filterDelegate = self

    filterMenuShowOn = true

    }


    // Hides filter menu scrolling bar from container view upon request.
    func hideFilterMenu() {

        currentViewController!.view.alpha = 1

        UIView.animateWithDuration(0.5, animations: {
            self.currentViewController!.view.alpha = 0
            },
                    completion: { finished in
                            self.currentViewController!.view.removeFromSuperview()
                            self.currentViewController!.removeFromParentViewController()
        })

    }

我要感谢马特让我思考发生了什么。

让我们检查一下基本情况。在行
destvc.filterDelegate=self
上放置断点。它正在被执行吗?不,它没有被执行。所以问题是你的segue实际上没有被称为
“FilterSegue”
。我不确定prepareForSegue是否正在被执行。我在三个点上设置了断点,但没有响应。这也是可能的。但据你所说,集合视图控制器不知怎么出现了。谢谢你的想法,但仍然没有设置委托。有人能向我解释为什么有人会否决这个问题吗?我阅读了与张贴问题相关的材料,在提问之前检查了同一时间的先前问题,并确保遵循了格式。我认为我的语法是合理的,我提供了经过编辑的代码。这对我来说毫无意义。我是一个新的Swift程序员,需要一些帮助。