Swift教程_零基础学习Swift完整实例(六)_swift完整实例(构建控制层)


4.构建控制层

本章节主要来构建个功能的控制层,为之前已经创建好的storyboard中页面的自定义controller添加对应功能。

1.TabBarViewController

该控制器按照需求通过数据层服务类PKOElementDataService所构建的实现了PKOTableDataSourceProtocolUITableViewDataSource协议的4类数据集dataSource,并将他们赋值给tab种所展示view的控制器,即PKOElementTableViewController中。与此同时,为tabBarItem和navigation赋予相应的文字描述及图片。

代码如下。

import UIKit

class PKOElementTabBarViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        var tableViewControllers = [UINavigationController]()//tab中所挂接的viewController集合
        var navigetionController : UINavigationController
        var tableViewController : PKOElementTableViewController
        var tabBarDataSource : PKOTableDataSourceProtocol
        
        //by name
        navigetionController = self.storyboard?.instantiateViewControllerWithIdentifier("navForTableView") as UINavigationController//获取标识为navForTableView的导航页面控制器
        tableViewController = navigetionController.topViewController as PKOElementTableViewController//获取该导航页面下所显示的列表控制器
        tabBarDataSource = PKOTableByNameDataSource()//获取数据集
        tableViewController.tabBarItem.title = tabBarDataSource.name//tabbarItem的文字描述
        tableViewController.tabBarItem.image = tabBarDataSource.tabBarImage//tabbarItem的图标
        tableViewController.navigationItem.title = tabBarDataSource.navigationBarName//导航标题
        tableViewController.dataSource = tabBarDataSource//将数据赋值给列表控制器的dataSource
        tableViewControllers.append(navigetionController)//将该导航添加到集合中
        
        //by number
        navigetionController = self.storyboard?.instantiateViewControllerWithIdentifier("navForTableView") as UINavigationController
        tableViewController = navigetionController.topViewController as PKOElementTableViewController
        tabBarDataSource = PKOTableByNumberDataSource()
        tableViewController.tabBarItem.title = tabBarDataSource.name
        tableViewController.tabBarItem.image = tabBarDataSource.tabBarImage
        tableViewController.navigationItem.title = tabBarDataSource.navigationBarName
        tableViewController.dataSource = tabBarDataSource
        tableViewControllers.append(navigetionController)
        
        //by symbol
        navigetionController = self.storyboard?.instantiateViewControllerWithIdentifier("navForTableView") as UINavigationController
        tableViewController = navigetionController.topViewController as PKOElementTableViewController
        tabBarDataSource = PKOTableBySymbolDataSource()
        tableViewController.tabBarItem.title = tabBarDataSource.name
        tableViewController.tabBarItem.image = tabBarDataSource.tabBarImage
        tableViewController.navigationItem.title = tabBarDataSource.navigationBarName
        tableViewController.dataSource = tabBarDataSource
        tableViewControllers.append(navigetionController)
        
        //by state
        navigetionController = self.storyboard?.instantiateViewControllerWithIdentifier("navForTableView") as UINavigationController
        tableViewController = navigetionController.topViewController as PKOElementTableViewController
        tabBarDataSource = PKOTableByStateDataSource()
        tableViewController.tabBarItem.title = tabBarDataSource.name
        tableViewController.tabBarItem.image = tabBarDataSource.tabBarImage
        tableViewController.navigationItem.title = tabBarDataSource.navigationBarName
        tableViewController.dataSource = tabBarDataSource
        tableViewControllers.append(navigetionController)
        
        self.viewControllers = tableViewControllers//将该导航集合赋值给tabbar的viewControllers,将会出现以上4种tabbarItem
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}

2.TableViewController

该控制器将TabBarViewController传来的dataSource赋值给自身的tableView.dataSource中,很方便就能显示想要的数据。

重写func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)方法,该方法是跳转到明细页面前需要执行的。

代码如下。

import UIKit

class PKOElementTableViewController: UITableViewController {
    
    var dataSource:PKOTableDataSourceProtocol?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.dataSource = self.dataSource
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        // Pass the selected object to the new view controller.
        var indexPath = self.tableView.indexPathForSelectedRow()
        var element = self.dataSource?.elementDataModelForIndexPath(indexPath!)//根据索引获取对象
        var elementDetailViewController = segue.destinationViewController as PKOElementDetailViewController
        elementDetailViewController.element = element//将数据对象赋值给明细页面的控制器
    }
    
}

3.DetailViewController

该控制器将绘制一个用来展示化学元素信息的卡片,包含背景图、名称、编码等信息。

其中涉及到的倒影和翻转动画会在以后详细说明,这里只要先把架子打起来即可。

代码如下。

import UIKit

class PKOElementDetailViewController: UIViewController {
    
    var element = PKOElementDataModel()
    
    var frontViewIsVisible = true//是否正面显示标识
    
    var subView:UIView?
    var detailImage:PKOElementDetailImageView?
    var detailImageFlipped = PKOElementDetailImageFlippedView()
    var reflectionImage = UIImageView()
    
    let reflectionRadio = 0.35//倒影的高度比例
    
    //Called after the controller's view is loaded into memory.
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.frontViewIsVisible = true
        
        //绘制图片view的父view
        var imageSize = CGSizeMake(256, 256)
        var detailRect = CGRectMake((self.view.bounds.size.width - imageSize.width)/2, (self.view.bounds.size.height - imageSize.height)/2 - 40, imageSize.width, imageSize.height)
        var subView = UIView(frame: detailRect)
        self.subView = subView
        self.view.addSubview(subView)
        
        //绘制图片
        var imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height)
        var detailImage = PKOElementDetailImageView(frame: imageRect)
        detailImage.element = self.element
        detailImage.detailController = self
        self.detailImage = detailImage
        self.subView?.addSubview(detailImage)
    }
}

点击进入ooppookid的博客

你可能感兴趣的:(ios,技术,xcode,swift,Object-C)