各种传值

首先控制导航条

         let vc = ViewController()

        let nav =UINavigationController(rootViewController: vc)

        self.window?.rootViewController=nav

在ViewController里写

import UIKit

class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{

    vartable =UITableView()

    varArr =NSArray()


    overridefuncviewWillAppear(_animated:Bool) {

        table.reloadData()

    }


    overridefuncviewDidLoad() {

        super.viewDidLoad()


        self.navigationItem.title="周考成绩单"


        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("点击了添加按钮")



    }

    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("录入分数", 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)

    }

}

在创建一个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="语文"


        Math=UITextField(frame:CGRect(x:100, y:300, width:240, height:40))


        Math.backgroundColor=UIColor.lightGray


        Math.placeholder="数学"


        letEnglish =UITextField(frame:CGRect(x:100, y:400, width:240, height:40))


        English.backgroundColor=UIColor.lightGray


        English.placeholder="英语"


        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)

    }

}

你可能感兴趣的:(各种传值)