iOS开发——设备篇Swift篇&判断设备类型
判断设备类型

4,样例代码
--- AppDelegate.swift 应用入口 ---
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// Override point for customization after application launch.
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible()
//初始化列表面板
let master = MasterViewController()
//初始化详情面板
let detail = DetailViewController()
//设置列表面板引用详情面板,以便用户点击列表项时调用详情面板的相应方法
master.detailViewController = detail
//用导航包装master列表,显示导航条,如果是分割面板也不影响功能
let nav = UINavigationController(rootViewController: master)
// 如果是iPhone或iPod则只显示列表页,如果是iPad则显示分割面板
24 if (UIDevice.currentDevice().userInterfaceIdiom == .Phone) {
25 self.window!.rootViewController = nav
26 }
27 else {
28 //初始化分割面板
29 let split = UISplitViewController()
30 //设置分割面板的2个视图控制器
31 split.viewControllers = [nav, detail]
32
33 //分割面板作为window的主视图加载
34 self.window!.rootViewController = split
35 }
36
return true
}
func applicationWillResignActive(application: UIApplication) {
}
func applicationDidEnterBackground(application: UIApplication) {
}
func applicationWillEnterForeground(application: UIApplication) {
}
func applicationDidBecomeActive(application: UIApplication) {
}
func applicationWillTerminate(application: UIApplication) {
}
}
--- MasterViewController.swift 列表页 ---
import UIKit
class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// 表格加载
var tableView:UITableView?
// 控件类型
var ctrls = ["UILabel", "UIButton", "UIImageView", "UISlider"]
//
var detailViewController:DetailViewController?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.title = "Swift控件演示"
self.tableView = UITableView(frame:self.view.frame, style:UITableViewStyle.Plain)
self.tableView!.delegate = self
self.tableView!.dataSource = self
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
self.view.addSubview(self.tableView!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// UITableViewDataSource协议方法
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.ctrls.count
}
// UITableViewDataSource协议方法
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("SwiftCell",
forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
cell.textLabel?.text = self.ctrls[indexPath.row]
return cell
}
// UITableViewDelegate协议方法,点击时调用
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
{
//调用DetailViewController的方法更新详细页
detailViewController!.loadControl(self.ctrls[indexPath.row])
53 //如果是iPhone、iPod则导航到详情页
54 if (UIDevice.currentDevice().userInterfaceIdiom == .Phone) {
55 // 跳转到detailViewController,取消选中状态
56 //self.tableView!.deselectRowAtIndexPath(indexPath, animated: true)
57
58 // navigationController跳转到detailViewController
59 self.navigationController!.pushViewController(detailViewController!, animated:true)
60 }
}
}
--- DetailViewController.swift 详情页 ---
import UIKit
class DetailViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor.whiteColor()
let ctrl = self.title != nil ? self.title! : ""
loadControl(ctrl)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadControl(ctrl:String) {
clearViews()
switch (ctrl) {
case "UILabel":
var label = UILabel(frame: self.view.bounds)
label.backgroundColor = UIColor.clearColor()
label.textAlignment = NSTextAlignment.Center
label.font = UIFont.systemFontOfSize()
label.text = "Hello, Hangge.com"
self.view.addSubview(label)
case "UIButton":
var button = UIButton(frame: CGRectMake(,,,))
button.backgroundColor = UIColor.blueColor()
button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
button.setTitle("点击我", forState: .Normal)
self.view.addSubview(button)
default:
println("clicked: \(ctrl)")
}
}
func clearViews() {
for v in self.view.subviews {
v.removeFromSuperview()
}
}
}
(注意:项目直接新建一个Master-Detail Application,就已经具有同上述一样的兼容iPhone、iPad的二级导航功能)
iOS开发——设备篇Swift篇&判断设备类型的更多相关文章
- iOS开发——技术精华Swift篇&Swift 2.0和Objective-C2.0混编之第三方框架的使用
swift 语言是苹果公司在2014年的WWDC大会上发布的全新的编程语言.Swift语言继承了C语言以及Objective-C的特性,且克服了C语言的兼容性问题.Swift语言采用安全编程模式,且引 ...
- iOS开发——新特性Swift篇&Swift 2.0 异常处理
Swift 2.0 异常处理 WWDC 2015 宣布了新的 Swift 2.0. 这次重大更新给 Swift 提供了新的异常处理方法.这篇文章会主要围绕这个方面进行讨论. 如何建造异常类型? 在 i ...
- iOS开发——网络编程Swift篇&Alamofire详解
Alamofire详解 预览图 Swift Alamofire 简介 Alamofire是 Swift 语言的 HTTP 网络开发工具包,相当于Swift实现AFNetworking版本. 当然,AF ...
- iOS开发——网络编程Swift篇&(八)SwiftyJSON详解
SwiftyJSON详解 最近看了一些网络请求的例子,发现Swift在解析JSON数据时特别别扭,总是要写一大堆的downcast(as?)和可选(Optional),看?号都看花了.随后发现了这个库 ...
- ios开发——实用技术篇Swift篇&地址薄、短信、邮件
//返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(tru ...
- iOS开发零基础--Swift篇:逻辑分支
一. 分支的介绍 分支即if/switch/三目运算符等判断语句 通过分支语句可以控制程序的执行流程 二. if分支语句 和OC中if语句有一定的区别 判断句可以不加() 在Swift的判断句中必须有 ...
- ios开发——实用技术总结Swift篇&swift常用开发技术总结
swift常用开发技术总结 懒加载:属性,数组(字典),控件... 数组(懒加载): lazy var shops:Array<Dictionary<String, String>& ...
- iOS开发——数据持久化Swift篇&(二)沙盒文件
沙盒文件 //******************** 5.2 文件操作 func use_FileOperations() { //1.获取程序的Home目录 let homeDirectory = ...
- iOS开发——数据持久化Swift篇&文件目录路径获取(Home目录,文档目录,缓存目录等)
文件目录路径获取(Home目录,文档目录,缓存目录等) iOS应用程序只能在自己的目录下进行文件的操作,不可以访问其他的存储空间,此区域被称为沙盒.下面介绍常用的程序文件夹目录: 1,Home ...
- iOS开发——网络编程Swift篇&(七)NSURLSession详解
NSURLSession详解 // MARK: - /* 使用NSURLSessionDataTask加载数据 */ func sessionLoadData() { //创建NSURL对象 var ...
随机推荐
- java 使用相对路径读取文件
java 使用相对路径读取文件 1.java project环境,使用java.io用相对路径读取文件的例子: *目录结构: DecisionTree |___src ...
- PHP 调用外部程序的几种方式
/* php 调用python 的代码 // 第一种: // echo passthru('C:/Python34/PY.exe D:/do.py'); // 第二种: // echo exec('C ...
- 怎么用PHP在HTML中生成PDF文件
原文:Generate PDF from html using PHP 译文:使用PHP在html中生成PDF 译者:dwqs 利用PHP编码生成PDF文件是一个非常耗时的工作.在早期,开发者使用PH ...
- bzoj 3698 XWW的难题(有源汇的上下界最大流)
[题意] 对每个格子确定上下取整,使得满足1.A[n][n]=0 2.每行列前n-1个之和为第n个 3.格子之和尽量大. [思路] 设格子(i,j)上下取整分别为up(i,j)down(i,j),构图 ...
- 分析特定类的python脚本
今天接触了下pyUSB,事先没看对象内部成员资料,直接用python的dir函数看了看pyUSB的内部构成.突然间想到自己可不可以写个简单的脚本,利用dir或其他函数遍历某个对象内部的所有成员,并打印 ...
- poj1000 A+B Problem
Description Calculate a+b Input Two integer a,b (0<=a,b<=10) Output Output a+b Sample Input 1 ...
- 快速开发 jQuery 插件的 10 大技巧(转)
转自:http://www.oschina.net/news/41776/jquery-10-tips 在开发过很多 jQuery 插件以后,我慢慢的摸索出了一套开发jQuery插件比较标准的结构和模 ...
- SCAU 07校赛 10317 Fans of Footbal Teams
10317 Fans of Footbal Teams 时间限制:1000MS 内存限制:65535K 题型: 编程题 语言: 无限制 Description Two famous footba ...
- elasticsearch配置文件解析
配置es的集群名称 : cluster.name: fcz_es
- centos6.4 安装erlang
erlang官网: http://www.erlang.org 下载程序去年: