swift基礎文法之控制項使用02,swift文法控制項02

來源:互聯網
上載者:User

swift基礎文法之控制項使用02,swift文法控制項02

//第一個控制器:顯示基礎控制項

import UIKit


class ViewController:UIViewController {

    

   var label: UILabel =UILabel()

   var button: UIButton =UIButton()

    var imageView:UIImageView = UIImageView()

    

//    var label: UILabel?

//    var button: UIButton?

//    var imageView: UIImageView?

    

   override func viewDidLoad() {

       super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

       /**

        UILabel

        */

       self.label =UILabel(frame: CGRectMake(10,50, 100, 30))

       self.label.text ="hehe"

        self.label.backgroundColor =UIColor.greenColor()

        self.label.textAlignment =NSTextAlignment.Center

       self.view.addSubview(self.label)

       /**

        UIButton

        */

       self.button =UIButton(frame: CGRectMake(50,100, 100, 30))

       self.button.setTitle("button", forState:UIControlState.Normal)

        self.button.backgroundColor =UIColor.redColor()

       self.button.addTarget(self, action:"bntclik:", forControlEvents: UIControlEvents.TouchUpInside)

       self.view.addSubview(self.button)

       /**

        UIImageView

        */

       self.imageView =UIImageView(frame: CGRectMake(100, 150, 100, 100))

       self.imageView.image =UIImage(named:"user")

       self.view.addSubview(self.imageView)

    }

    

   func bntclik(button:UIButton){

       var oneVC = ViewControllerOne()

       var oneNA: UINavigationController =UINavigationController(rootViewController: oneVC)

       self.presentViewController(oneNA, animated:true, completion:nil)

    

       println("button")

        

    }

   override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}



//第二個控制器:顯示表格視圖


import UIKit


class ViewControllerOne:UIViewController,UITableViewDataSource,UITableViewDelegate {

    var tableView:UITableView = UITableView()

   var dataArray: NSArray = []


   override func viewDidLoad() {

       super.viewDidLoad()


        // Do any additional setup after loading the view.

        self.view.backgroundColor =UIColor.whiteColor()

       self.dataArray = ["1","2","3","4","5","6"]

       /**

        UITableView

        */

       self.tableView =UITableView(frame: CGRectMake(0, 0,CGRectGetWidth(self.view.frame),CGRectGetHeight(self.view.frame)), style:UITableViewStyle(rawValue: 0)!)

       self.tableView.delegate =self

       self.tableView.dataSource =self

       self.view.addSubview(self.tableView)

    }

   func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->Int

    {

       return self.dataArray.count

    }

   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

    {

       self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier:"cell")


       let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)as UITableViewCell

        cell.textLabel.text =self.dataArray[indexPath.row]as NSString;

       return cell


        

    }

   override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    


}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.