首先控制導(dǎo)航條
? ? ? ? ?let vc = ViewController()
? ? ? ? let?nav =UINavigationController(rootViewController: vc)
? ? ? ? self.window?.rootViewController=nav
在ViewController里寫(xiě)
import UIKit
class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
? ? vartable =UITableView()
? ? varArr =NSArray()
? ? overridefuncviewWillAppear(_animated:Bool) {
? ? ? ? table.reloadData()
? ? }
? ? overridefuncviewDidLoad() {
? ? ? ? super.viewDidLoad()
? ? ? ? self.navigationItem.title="周考成績(jī)單"
? ? ? ? table=UITableView(frame:UIScreen.main.bounds, style: .grouped)
? ? ? ? table.delegate=self
? ? ? ? table.dataSource=self
? ? ? ? self.view.addSubview(table)
? ? ? ? Arr = ["A","B","C","D","E","F"]
? ? ? ? letright =UIBarButtonItem(barButtonSystemItem: .add, target:self, action:nil)
? ? ? ? self.navigationItem.rightBarButtonItem=right
? ? ? ? print("點(diǎn)擊了添加按鈕")
? ? }
? ? funcnumberOfSections(in tableView:UITableView) ->Int{
? ? ? ? return6
? ? }
? ? functableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
? ? ? ? return3
? ? }
? ? functableView(_tableView:UITableView, titleForHeaderInSection section:Int) ->String? {
? ? ? ? varheaders =? self.Arr
? ? ? ? returnheaders[section]as!String
? ? }
? ? functableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{
? ? ? ? varcell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier:"cell")
? ? ? ? ifcell ==nil{
? ? ? ? ? ? cell =UITableViewCell(style: .value1, reuseIdentifier:"cell")
? ? ? ? }
? ? ? ? letBun =UIButton(frame:CGRect(x:300, y:10, width:100, height:30))
? ? ? ? Bun.setTitle("錄入分?jǐn)?shù)", for: .normal)
? ? ? ? Bun.setTitleColor(.red, for: .normal)
? ? ? ? Bun.addTarget(self, action:#selector(TZhuan), for: .touchUpInside)
? ? ? ? cell.addSubview(Bun)
? ? ? ? let sd:AppDelegate = UIApplication.shared.delegate as! AppDelegate
? ? ? ? print(sd.str)
? ? ? ? ifindexPath.section==0{
? ? ? ? ? ? ? ? cell.textLabel?.text= sd.str
? ? ? ? }
? ? ? ? returncell
? ? }
? ? @objcfuncTZhuan(){
? ? ? ? let sco = ScoreViewController()
? ? ? ? self.navigationController?.pushViewController(sco, animated:true)
? ? }
}
在創(chuàng)建一個(gè)Viewcollerter
import UIKit
classScoreViewController:UIViewController{
? ? varLanguage =UITextField()
? ? var? Math =UITextField()
? ? overridefuncviewDidLoad() {
? ? ? ? super.viewDidLoad()
? ? ? ? self.view.backgroundColor=UIColor.white
?? ? ? Language=UITextField(frame:CGRect(x:100, y:200, width:240, height:40))
? ? ? ? Language.backgroundColor=UIColor.lightGray
? ? ? ? Language.placeholder="語(yǔ)文"
? ? ? ? Math=UITextField(frame:CGRect(x:100, y:300, width:240, height:40))
? ? ? ? Math.backgroundColor=UIColor.lightGray
? ? ? ? Math.placeholder="數(shù)學(xué)"
? ? ? ? letEnglish =UITextField(frame:CGRect(x:100, y:400, width:240, height:40))
? ? ? ? English.backgroundColor=UIColor.lightGray
? ? ? ? English.placeholder="英語(yǔ)"
? ? ? ? self.view.addSubview(Language)
? ? ? ? self.view.addSubview(Math)
? ? ? ? self.view.addSubview(English)
? ? ? ? letSave =UIButton(frame:CGRect(x:80, y:600, width:100, height:50))
? ? ? ? Save.setTitle("保存", for: .normal)
? ? ? ? Save.setTitleColor(.red, for: .normal)
? ? ? ? Save.addTarget(self, action:#selector(SaveBun), for:.touchUpInside)
? ? ? ? letBack =UIButton(frame:CGRect(x:250, y:600, width:100, height:50))
? ? ? ? Back.setTitle("返回", for: .normal)
? ? ? ? Back.setTitleColor(.red, for: .normal)
? ? ? ? self.view.addSubview(Save)
? ? ? ? self.view.addSubview(Back)
? ? ? ? Back.addTarget(self, action:#selector(BackBun), for: .touchUpInside)
? ? }
? ? @objcfuncBackBun(){
?? ? ? // let vc = ViewController()
? ? ? ? self.navigationController?.popViewController(animated:true)
? ? }
? ? @objcfuncSaveBun(){
? ? ? //? let vc = ViewController()
? ? ? ? let sd:AppDelegate = UIApplication.shared.delegate as! AppDelegate
? ? ? ? sd.str=Language.text!
? ? ? ? self.navigationController?.popViewController(animated:true)
? ? }
}