ISO swift代码 第一个

http://study.163.com/course/courseLearn.htm?courseId=873007#/learn/video?lessonId=1049033&courseId=873007


Empty Application


AppDelegate.Swift


var window: UIWindow?


self.window = UIWindow(frame:UIScreen.mainScreen().bounds)
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible()




//创建一个控制器
var rvc = RootViewController()
self.window!.rootViewController = rvc
return true








RootViewController.Swift
import Foundation;
import UIKit;


class RootViewController : UIViewController{
override func viewDidLoad() {
super.viewDidLoad();
//在界面上加一个UILable
let rect = CGRect(x:0,y:100,width:320,height:44)
//创建一个UILable对象
var myLabel = UILabel(frame:rect)
//给label上设置一个文字
myLabel.text = "前锋IOS"
//给label设置一个背景颜色
myLabel.backgroundColor = UIColor.redColor()
self.view.addSubview(myLabel)

var myButton = UIButton(frame: CGRect(x:100,y:100,width:100,height:100))
myButton.setTitle("点击我",forState:.Normal)
myButton.backgroundColor = UIColor.blueColor()
//给myButton添加点击事件
myButton.addTarget(self,action:"clickMe:",forControlEvents:.TouchUpInside)
self.view.addSubview(myButton)
}
}


var button = UIButton(frame: CGRect(x:0,y:0,width:100,height:100))
button.setTitle("你好",forControlEvents:.Normal)
button.addTarget(self,action:"clickMe",forControlEvents:.TouchUpInside)
button.backgroundColor= UIColor.blueColor()
self.view.addSubview(button)






//2 UINavigation


let rcv :UIViewController = RootViewController()
//创建导航控制器
let nav = UINavigationController(rootViewController:rcv)
self.window!.rootViewController = nav;


RootViewController.Swift
import Foundation
import UIKit


class RootViewController: UIViewController,FontSizeChangeDelegate{
override func viewDidLoad()
{
self.title = "title"
//右边放一个按钮UIBarButtonItem
let nextItem = UIBarButtonItem(title:"下一页",style:.plain,
target:self,action:"nextPage")
self.navigationItem.rightBarButtonItem = nextItem

//定义一个label
var myLabel = UILabel(frame:CGRect(x:1,y:1,width:100,height:100))
myLabel!.text = "hello world"
self.view.addSubview(myLabel)
}

func nextPage()
{
NSLog("点击了nextPage")
let svc = SecondViewController()
svc.delegate = self
self.navigationController.pushView(svc,animated:true)
}

func fontSizeDidChange(controller:SecondViewController, fontSize:int)
{
let font = UIFount.systemFontOfSize(Float(fontSize))
myLabel!.font = font
}
}


// SecondViewController.Swift
import Foundation
import UIKit


protocol FontSizeChangeDelegate : NSObjectProtocol{
//定义的一个协议函数、代理的一个函数
//参数1 当前controller本身
//参数2 字体大小
func fontSizeDidChange(controller:SecondViewController,fontSize:Int)
}
class SecondViewController : UIViewController{
var fontSize:Int = 20
//定义一个delegate对象,写上协议对象
var delegate:FontSizeChangeDelegate?
override fun viewDidLoad()
{
super.viewDidLoad()

self.title = "第二页"
self.view.backgroundColor = UIColor.redColor()

var b = UIButton.buttonWithType(.System) as UIButton
var frame = CGRect(x:100,y:100,width:100,height:40)
b.frame = frame
b.setTitle("返回上一层", forState:.Normal)
b.addTarge(self,action:"clickMe:",forControllerEvent:.TouchUpSide)
self.view.addSubview(b)

}
func clickMe(sender:UIButton)
{
fontSize ++;
if(deleagte)
{
delegate?.fontSizeDidChange(self,fontSize:fontSize)
}
//返回上一层
//self.navigationController.popViewControllerAnimated(true)
}


}














































































































,

你可能感兴趣的:(ISO swift代码 第一个)