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 ...
随机推荐
- C#中的值类型(value type)与引用类型(reference type)的区别
ylbtech- .NET-Basic:C#中的值类型与引用类型的区别 C#中的值类型(value type)与引用类型(reference type)的区别 1.A,相关概念返回顶部 C#中 ...
- SQL你必须知道的-函数及类型转换
use MySchoolTwo --ISNULL(expression,value) :如果 expression不为空则返回 expression ,否则返回 value. select ...
- 【随便走走】Vietnam
从来没有一个地方让我如此留念过. 初到越南印象就是乱,满街轰轰轰的摩托车,狭窄的街道,各种小酒店小商店.从机场出来的路上还看到了不少中国品牌如豪爵摩托等等. 落地办理了落地签,从大陆是不能办的.越 ...
- HDU5479 Colmerauer 单调栈+暴力优化
http://acm.hdu.edu.cn/showproblem.php?pid=5749 思路: bestcoder 84 贡献:所有可能的子矩阵的面积和 //len1:子矩阵所有长的和 ;i&l ...
- Codeforces 364
A 第一题明显统计,注意0和long long(我WA,RE好几次) /* * Problem: A. Matrix * Author: Shun Yao */ #include <string ...
- Spark SQL概念学习系列之Spark SQL 架构分析(四)
Spark SQL 与传统 DBMS 的查询优化器 + 执行器的架构较为类似,只不过其执行器是在分布式环境中实现,并采用的 Spark 作为执行引擎. Spark SQL 的查询优化是Catalyst ...
- UVALive 7275 Dice Cup (水题)
Dice Cup 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/D Description In many table-top ...
- POJ 3672 Long Distance Racing (模拟)
题意:给定一串字符,u表示是上坡,d表示下坡,f表示平坦的,每个有不同的花费时间,问你从开始走,最远能走到. 析:直接模拟就好了,没什么可说的,就是记下时间时要记双倍的,因为要返回来的. 代码如下: ...
- mac 下对 iterm 终端 设置代理
vi .profile export http_prox="http://xxxx:port" export https_proxy="http://xxxx:port& ...
- Jeecms自定义标签用法[单个内容]
1.com.jeecms.cms.action.directive包下建立自己的标签类