AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
self.window?.makeKeyAndVisible()
//创建导航视图控制器的根视图
let vc = ViewController()
//2.创建导航视图控制器,并为她制定根视图控制器
let navigation = UINavigationController(rootViewController: vc)
//3.将导航视图控制器设置为window的根视图控制器
self.window?.rootViewController = navigation
return true
}
ViewController.swift:
class ViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate{
var tableView:UITableView!
let identifier = "cell"
//准备数据源
var personArray = [Person(name: "张三", number: 18347457138),Person(name: "阿拉蕾", number: 18347451711),Person(name: "安吉", number: 18347459211),Person(name: "小鱼儿", number: 18376698256),Person(name: "小靓仔", number: 15247479652),Person(name: "宋仲基", number: 14715639875),Person(name: "吴亦凡", number: 18652369458)]
override func loadView() {
tableView = UITableView(frame: UIScreen.main.bounds, style: .plain)
tableView.tableFooterView = UIView()
tableView.delegate = self
tableView.dataSource = self
self.view = tableView
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.title = "通讯录"
//注册cell
//UITableViewCell.self h获取一个类的对象
//UITableViewCell.classForCoder() 获取一个类的对象
//参数一:类对象
//参数二:这种cell的重用标识
tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return personArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: identifier)
cell?.textLabel?.text = personArray[indexPath.row].name
cell?.backgroundColor = #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let secondVC = secondViewController()
secondVC.name = personArray[indexPath.row].name
secondVC.num = personArray[indexPath.row].number
self.navigationController?.pushViewController(secondVC, animated: true)
}
secondViewController.swift:
class secondViewController: UIViewController {
var label1 : UILabel!
var label2:UILabel!
var textfield1:UITextField!
var textfield2:UITextField!
//定义要传值的类型的属性 属性传值第一步
var name:String!
var num:Int!
override func viewDidLoad() {
super.viewDidLoad()
label1 = UILabel(frame: CGRect(x: 40, y: 100, width: 80, height: 50))
label1.text = "姓名"
label1.backgroundColor = UIColor.red
self.view.addSubview(label1)
textfield1 = UITextField(frame: CGRect(x: 200, y: 100, width: 150, height: 50))
textfield1.backgroundColor = UIColor.red
self.view.addSubview(textfield1)
label2 = UILabel(frame: CGRect(x: 40, y: 200, width: 80, height: 50))
label2.text = "电话"
label2.backgroundColor = UIColor.yellow
self.view.addSubview(label2)
textfield2 = UITextField(frame: CGRect(x: 200, y: 200, width: 150, height: 50))
textfield2.backgroundColor = UIColor.yellow
self.view.addSubview(textfield2)
//3
textfield1.text = self.name
textfield2.text = String(self.num)
// Do any additional setup after loading the view.
}
Person.swift:
class Person: NSObject {
var name:String?
var number:Int?
init(name:String,number:Int) {
self.name = name
self.number = number
}
}