一.建立項目 //略
二.建立視圖控制器(快捷的方法:不要在去建立swift 檔案 ,直接如下操作
)
上代碼(含有注釋) AppDelegate.swift
//
// AppDelegate.swift
// swift UINavigationController 導航控制器
//
// Created by zhangbiao on 14-6-16.
// Copyright (c) 2014年 理想. All rights reserved.
//
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 one_vc = OneViewController(nibName:nil,bundle: nil);
//建立導航控制器
let nvc=UINavigationController(rootViewController:one_vc);
//設定根視圖
self.window!.rootViewController=nvc;
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
OneViewController.swift
//
// OneViewController.swift
// swift UINavigationController 導航控制器
//
// Created by zhangbiao on 14-6-16.
// Copyright (c) 2014年 理想. All rights reserved.
//
import UIKit
class OneViewController: UIViewController {
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// Custom initialization
}
//
override func viewDidLoad() {
super.viewDidLoad()
// 設定導覽列標題
self.title="One";
//Item 標題 //Item樣式 //目標所有 //nxet 事件觸發的方法名字
let nextItem=UIBarButtonItem(title:"下一頁",style:.Plain,target:self,action:"next");
// 添加到到導覽列上
self.navigationItem.rightBarButtonItem = nextItem;
}
//Item 事件
func next()
{
println("點擊了下一頁");
// 定義一個控制器
let tow_vc = TowViewController();
//推入下一個視圖
self.navigationController.pushViewController(tow_vc,animated:true);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// #pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
}
TowViewController.swift
//
// TowViewController.swift
// swift UINavigationController 導航控制器
//
// Created by zhangbiao on 14-6-16.
// Copyright (c) 2014年 理想. All rights reserved.
//
import UIKit
class TowViewController: UIViewController {
//
// init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
// super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// // Custom initialization
// }
override func viewDidLoad()
{
super.viewDidLoad()
self.title="Tow";
// 建立一個按鈕
var but=UIButton(frame:CGRect(x:110,y:100,width:100,height:40)) ;
//設定tag
but.tag=1001;
//設定標題
but.setTitle("返回上一頁",forState:.Normal);
//添加事件
but.addTarget(self,action:"butClick:",forControlEvents:.TouchUpInside);
// 設定背景顏色
but.backgroundColor=UIColor.blueColor();
//添加到試圖上
self.view.addSubview(but);
var but1=UIButton(frame:CGRect(x:110,y:200,width:100,height:40)) ;
//設定but tag
but1.tag=1002;
//設定標題
but1.setTitle("下一頁",forState:.Normal);
//添加事件
but1.addTarget(self,action:"butClick:",forControlEvents:.TouchUpInside);
// 設定背景顏色
but1.backgroundColor=UIColor.blueColor();
//添加到視圖上
self.view.addSubview(but1);