1.写法对比
oc
self.window = [UIWindow alloc]initWithFrame:UIScreen.main.bounds];
[self.view addSubview:子视图]
swift
self.window = UIWindow(frame:UIScreen.main.bounds);
let vc = ViewController();
let nav = UINavigationController(rootViewController:vc)
self.window =类名(属性:属性值);
self.view.addSubview(子视图);//之前的方法调用由中括号形式变为属性形式
button
override func viewDidLoad() {
super.viewDidLoad()
self.title = "测试";
let but = UIButton();
//上左下右-----控制图片和文字的位置
bu.imageEdgeInsets=UIEdgeInsetsMake(-20,20,0,0)
bu.titleEdgeInsets=UIEdgeInsetsMake(30,-80,-20,0)
but.frame = CGRect.init(x:20,y:100,width:80,height:40);
but.backgroundColor = UIColor.yellow;
but.setTitle("but", for: UIControlState.normal);
but.addTarget(self, action:#selector(buttonOneClicked), for: UIControlEvents.touchUpInside);
self.view.addSubview(but);
self.view.addSubview(self.but);
self.view.backgroundColor = UIColor.red;
// Do any additional setup after loading the view, typically from a nib.
}
@objc func buttonOneClicked(){
print("123");
NSLog("123");
let v2 = ViewController2();
self.navigationController?.pushViewController(v2, animated: true);
}
懒加载
好处:将对象的创建延迟到了需要对象的时候,这样减少了内存开销。另外这样也将创建对象、相关属性内聚在一个代码块内,降低了其他模块的复杂度。
使用:在swift中,实现懒加载需要lazy和var关键字,如下:
lazy var but:UIButton = {
()->UIButton in
let tempBut = UIButton();
tempBut.backgroundColor = UIColor.yellow;
tempBut.frame = CGRect.init(x:20,y:100,width:80,height:40);
tempBut.setTitle("but", for: UIControlState.normal);
tempBut.addTarget(self, action:#selector(buttonOneClicked), for: UIControlEvents.touchUpInside);
return tempBut;
}();
总结:swift实现懒加载必须使用var关键字来定义延时加载的属性,而不可以使用let关键字,因为let关键字定义的是常量,而常量必须在实例创建时赋值。另外swift定义懒加载的规则,即"后面通过等号赋值一个闭包,闭包后面必须加上()"。
也可以这样:
lazy var butt:UIButton = {
let tempBut = UIButton()
tempBut.backgroundColor = UIColor.yellow
tempBut.frame = CGRect.init(x:20,y:150,width:80,height:40)
tempBut.setTitle("but", for: UIControlState.normal)
tempBut.addTarget(self, action:#selector(buttonOneClicked), for: UIControlEvents.touchUpInside)
return tempBut
}();
UITableView
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
let arrStr = ["1","2","3","12","23","33",];
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
NSLog(arrStr[indexPath.row], indexPath.row);
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrStr.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = (self.myTableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)) as UITableViewCell
cell.textLabel?.text = arrStr[indexPath.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
self.title = "测试";
self.view.addSubview(self.myTableView);
self.view.backgroundColor = UIColor.red;
}
lazy var myTableView:UITableView = {
let tempTableV = UITableView(frame:self.view.frame);
tempTableV.delegate = self;
tempTableV.dataSource = self;
tempTableV.backgroundColor = UIColor.blue;
//注册UITableView,cellID为重复使用cell的Identifier
tempTableV.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
return tempTableV;
}();
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}