swift UINavigationController 导航控制器

AppDelegate.swift

//

//  AppDelegate.swift

//  swift UINavigationController导航控制器

//

//  Created by zhangbiao on 14-6-16.

//  Copyright (c) 2016年理想. All rights reserved.

//

importUIKit

@UIApplicationMain

classAppDelegate:UIResponder,UIApplicationDelegate{

varwindow:UIWindow?

funcapplication(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()

//定义一个视图控制器

letone_vc =OneViewController(nibName:nil,bundle:nil);

//创建导航控制器

letnvc=UINavigationController(rootViewController:one_vc);

//设置根视图

self.window!.rootViewController=nvc;

returntrue

}

funcapplicationWillResignActive(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.

}

funcapplicationDidEnterBackground(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.

}

funcapplicationWillEnterForeground(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.

}

funcapplicationDidBecomeActive(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.

}

funcapplicationWillTerminate(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.

//

importUIKit

classOneViewController:UIViewController{

init(nibName nibNameOrNil:String?, bundle nibBundleOrNil:NSBundle?) {

super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

// Custom initialization

}

//

overridefuncviewDidLoad() {

super.viewDidLoad()

//设置导航栏标题

self.title="One";

//Item标题//Item样式//目标所有//nxet事件触发的方法名字

letnextItem=UIBarButtonItem(title:"下一页",style:.Plain,target:self,action:"next");

//添加到到导航栏上

self.navigationItem.rightBarButtonItem= nextItem;

}

//Item事件

funcnext()

{

println("点击了下一页");

//定义一个控制器

lettow_vc =TowViewController();

//推入下一个视图

self.navigationController.pushViewController(tow_vc,animated:true);

}

overridefuncdidReceiveMemoryWarning() {

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.

//

importUIKit

classTowViewController:UIViewController{

//

//    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {

//        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

//        // Custom initialization

//    }

overridefuncviewDidLoad()

{

super.viewDidLoad()

self.title="Tow";

//创建一个按钮

varbut=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);

varbut1=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);

//添加一个Item  Item使用系统图标

letmyItem=UIBarButtonItem(barButtonSystemItem:.Action,target:self,action:"ItemClick");

self.navigationItem.leftBarButtonItem=myItem;

}

//but按钮事件

funcbutClick(but:UIButton)

{

switch(but.tag)

{

case1001:

//返回上一页出栈操作

self.navigationController.popViewControllerAnimated(true);

case1002:

//进入下一页再次压栈

//创建一个试图控制器

letthree=ThreeViewController();

//压入栈

self.navigationController.pushViewController(three,animated:true);

default:

println("没有匹配的");

}

}

// but按钮事件

funcItemClick()

{

varlabel=UILabel(frame:CGRect(x:20,y:300,width:280,height:50));

label.text="我给你笑一个吧

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,564评论 5 6
  • 一 人间最美四月天。 白落尘轻轻呼吸着空气中馥郁的花香,眼前又浮现出那个画面,一个白衣素裙的女孩站在阳光下,笑的格...
    一剪芳踪阅读 787评论 2 3
  • /shichen 最新版本=1.8 下载地址=http://q20947154.website.fengniaoi...
    秋风凉凉阅读 142评论 0 0
  • 今天我收拾了一下以前的旧东西,三国演义、数学练习册、日记本、笔记本、水卡……都是高中的东西,过去的美好时光已然过去...
    晴天ol阅读 197评论 0 1
  • 我们都有多面性,其中有一些是我们不希望别人看到的──你可能会认为这是你的“阴暗面”,或是脑袋中的魔鬼 。 有时候,...
    洋风物志阅读 1,130评论 0 1