IOS:Swift-通訊錄的簡易練習(xí)

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()
    //創(chuàng)建導(dǎo)航視圖控制器的根視圖
    let vc = ViewController()
    //2.創(chuàng)建導(dǎo)航視圖控制器,并為她制定根視圖控制器
    let navigation = UINavigationController(rootViewController: vc)
    
    //3.將導(dǎo)航視圖控制器設(shè)置為window的根視圖控制器
    self.window?.rootViewController = navigation

    
    return true
}

ViewController.swift:
class ViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate{

var tableView:UITableView!
let identifier = "cell"
//準(zhǔn)備數(shù)據(jù)源
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()  獲取一個類的對象
        //參數(shù)一:類對象
        //參數(shù)二:這種cell的重用標(biāo)識
        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
}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容