//懒加载
//swift中固定写法
lazy var l:UILabel = {
let label = UILabel(frame: CGRectMake(50,100,100,30))
label.backgroundColor=UIColor(red: 211/255.0, green: 192/255.0, blue: 65/255.0, alpha: 1)
return label
}()//易忘
//viewDidLoad中
view.addSubview(l)
//原来的init直接() 枚举直接.
//====================================//
//协议
委托人处
添加协议及方法声明
protocol womanDelegate {
func makeMoney()
}
添加代理属性
var delegate:womanDelegate? //如非可选报错
代理人处
遵守协议 使用,隔开
class ViewController: UIViewController,womanDelegate {
}
//实现协议方法
func makeMoney() {
print("赚钱")
}
//设置代理人
let w = Woman()
w.delegate = self
makeMoney()
//====================================//
//网络请求
//注意字符串没有@
let urlStr = "http://mapi.yinyuetai.com/video/list.json?deviceinfo=%7B%22aid%22%3A%2210201022%22%2C%22os%22%3A%22Android%22%2C%22ov%22%3A%224.0.4%22%2C%22rn%22%3A%22480*800%22%2C%22dn%22%3A%22HUAWEI%20U9508%22%2C%22cr%22%3A%2246001%22%2C%22as%22%3A%22WIFI%22%2C%22uid%22%3A%225eec1e1b389ff457f6c886ef88eeb6dd%22%2C%22clid%22%3A110002000%7D&area=ELITE&offset=0&size=20"
//不支持pch?
// let urlStr = KVideoURL
let url = NSURL(string:urlStr)
/*
① 编译器bug data, response, error 传递不过去 需要手写
*/
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) -> Void in
if let temp = data
{
do{
let dic = try NSJSONSerialization.JSONObjectWithData(temp, options: NSJSONReadingOptions.MutableContainers)
print("请求成功")
}catch
{
print(error)
}
}
}
task.resume()
//====================================//
//单例
//创建单例
class singleTon: NSObject {
var color:UIColor = UIColor.greenColor()
static let vc = singleTon()
class func shareInstance()->singleTon{
return vc
}
}
//单例传值
view.backgroundColor = singleTon.shareInstance().color
//====================================//
//闭包传值
//VC2
//①定义一个闭包
var closure:((str:String)->())?
//②调用闭包 点击button方法中
closure!(str:tf.text!)
//VC1
//③闭包的实现 找到一个地方2个VC都可以访问到
seVC.closure = {
(str:String)->()
in
self.l.text=str
}
闭包的格式
{
(参数名:参数类型)->返回值类型
in
{
}
}
//====================================//
添加导航栏
let vc = ViewController()
window?.rootViewController = UINavigationController(rootViewController: vc)
页面跳转
//VC1
let seVC = SecondViewController()
navigationController?.pushViewController(seVC, animated: true)
//VC2
navigationController?.popViewControllerAnimated(true)