如何连接swift代码和故事板?

时间:2017-05-20 08:41:57

标签: ios swift storyboard segue

我正在试图找出一种使用Pod ColorMatchTabs的方法,但是它的例子是以编程方式进行,我找不到在项目中使用它们的方法。我将总结一下:

要使用pod,我必须遵循此协议在我的viewController中安装选项卡:

extension ExampleViewController: ColorMatchTabsViewControllerDataSource {
    func tabsViewController(_ controller: ColorMatchTabsViewController, iconAt index: Int) -> UIImage {
        return TabItemsProvider.items[index].normalImage
    }

    func tabsViewController(_ controller: ColorMatchTabsViewController, hightlightedIconAt index: Int) -> UIImage {
        return TabItemsProvider.items[index].highlightedImage
    }



    func numberOfItems(inController controller: ColorMatchTabsViewController) -> Int {
        return TabItemsProvider.items.count
    }

    func tabsViewController(_ controller: ColorMatchTabsViewController, viewControllerAt index: Int) -> UIViewController {
        return StubContentViewControllersProvider.viewControllers[index]
    }

    func tabsViewController(_ controller: ColorMatchTabsViewController, titleAt index: Int) -> String {
        return TabItemsProvider.items[index].title
    }

    func tabsViewController(_ controller: ColorMatchTabsViewController, tintColorAt index: Int) -> UIColor {
        return TabItemsProvider.items[index].tintColor
    }

这些是用于处理选项卡外观的协议,调用UIViewController的部分中有多少项和我的困难。我假设它根据哪个选项卡返回使用哪些控制器,这里是StubContentViewControllersProvider中的代码:

import UIKit
import ColorMatchTabs

class StubContentViewControllersProvider {

    static let viewControllers: [UIViewController] = {


        let productsViewController = StubContentViewController()

        productsViewController.type = .products

        let venuesViewController = StubContentViewController()
        venuesViewController.type = .venues

        let reviewsViewController = StubContentViewController()
        reviewsViewController.type = .reviews

        let usersViewController = StubContentViewController()
        usersViewController.type = .users

        return [productsViewController, venuesViewController, reviewsViewController, usersViewController]
    }()

}

我假设我们正在调用来自StubContentViewController的所有控制器,这是它的样子:

import UIKit

class StubContentViewController: UITableViewController {

    enum `Type` {
        case products, venues, reviews, users
    }

    var type: Type!

    fileprivate var objects: [UIImage] = []

    override func viewDidLoad() {
        super.viewDidLoad()


        setupTableView()
        setupDataSource()
    }

    fileprivate func setupTableView() {
        tableView.backgroundColor = UIColor.clear
        tableView.allowsSelection = true
        tableView.separatorColor = UIColor.clear
        tableView.register(UINib(nibName: "ExampleTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
    }

    fileprivate func setupDataSource() {
        if type == .products || type == .reviews {
            self.objects = [UIImage(named: "product_card1")!, UIImage(named: "product_card2")!]
        } else if type == .venues || type == .users {
            self.objects = [UIImage(named: "venue_card1")!, UIImage(named: "venue_card2")!]
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return objects.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ExampleTableViewCell
        let image = objects[(indexPath as NSIndexPath).row]
        cell.apply(image)

        return cell
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return tableView.bounds.width / 1.4
    }


}

所以我的问题是,我正在设置一个TableViewController并将其引用到故事板中的StubContentViewController,但为什么当我在故事板中进行更改时,它没有任何效果?例如,我想为表格单元格执行segue,我找不到通过故事板制作它的任何方法?

0 个答案:

没有答案