功能如下:
1,在AppDelagete.swift入口文件中把首页ViewController做了导航控件的封装
2,首页是一个表格列出几个Swift控件的名称
3,点击表格项即切换到对应组件展示页面,顶部的导航条标题变为该控件的名称,同时导航条左侧还有返回按钮
4,在展示页中,给导航条右侧添加了“效果/代码”切换的按钮,点击分别展示组件的效果和代码
效果图如下:
代码如下:
--- 入口文件AppDelegate.swift ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
import
UIKit
@UIApplicationMain
class
AppDelegate: UIResponder,UIApplicationDelegate {
var
window: UIWindow?
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// 把起始ViewController作为导航控件封装,我们在ViewController里就能调用导航条进行页面切换了
let rootViewController = ViewController()
let rootNavigationController = UINavigationController(rootViewController: rootViewController)
self.window!.rootViewController = rootNavigationController
return
true
}
func applicationWillResignActive(application: UIApplication) {
}
func applicationDidEnterBackground(application: UIApplication) {
}
func applicationWillEnterForeground(application: UIApplication) {
}
func applicationDidBecomeActive(application: UIApplication) {
}
func applicationWillTerminate(application: UIApplication) {
}
}
|
--- 主页面ViewController.swift ---
ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
// 表格加载
tableView:UITableView?
// 控件类型
ctrls = [
"UILabel"
,
"UIButton"
"UIImageView"
"UISlider"
"UIWebView"
]
override
func viewDidLoad() {
super
.viewDidLoad()
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!)
func didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// UITableViewDataSource协议方法
func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int
{
return
self.ctrls.count
}
// UITableViewDataSource协议方法
-> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier(
as
UITableViewCell
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
cell.textLabel?.text = self.ctrls[indexPath.row]
cell
}
// UITableViewDelegate协议方法,点击时调用
{
// 跳转到detailViewController,取消选中状态
self.tableView!.deselectRowAtIndexPath(indexPath,animated:
true
)
// 创建DetailViewController
let detailViewController = DetailViewController()
// 传递控件的title,在detailView里用于判断生成响应的控件
detailViewController.title = self.ctrls[indexPath.row]
// navigationController跳转到detailViewController
self.navigationController!.pushViewController(detailViewController,monospace!important; min-height:inherit!important">)
}
--- 子页面DetailViewController.swift ---
如果使用StoryBoard实现更加简单
AppDelegate.swift都不需要修改。打开Main.storyboard。
(1)点击首页的Scene,选择Editor -> Embed In -> Navigation Controller 即可。
(2)从首页单元格拖“show”的关联Segue到详细页,或从首页View Controller拖手动关联Segue到详细页
(3)定义上面刚添加的Segue的Indentifier(比如detail)
这样,点击单元格跳转的代码有所改变,是根据刚才定义Segue的Indentifier来跳转
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_586.html 相关文章![]() 前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
|