swift基础语法之控件使用02

前端之家收集整理的这篇文章主要介绍了swift基础语法之控件使用02前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//第一个控制器:显示基础控件

import UIKit

@H_404_8@

class ViewController: UIViewController {

@H_404_8@

var label: UILabel = UILabel()

var button: UIButton = UIButton()

var imageView: UIImageView = UIImageView()

@H_404_8@

// var label: UILabel?

// var button: UIButton?

// var imageView: UIImageView?

@H_404_8@

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, 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))

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

self.view.addSubview(self.imageView)

}

@H_404_8@

func bntclik(button:UIButton){

var oneVC = ViewControllerOne()

var oneNA: UINavigationController = UINavigationController(rootViewController: oneVC)

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

@H_404_8@

println("button")

@H_404_8@

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

@H_404_8@

@H_404_8@

}



//第二个控制器:显示表格视图


import UIKit

@H_404_8@

class ViewControllerOne: UIViewController,UITableViewDataSource,UITableViewDelegate {

var tableView: UITableView = UITableView()

var dataArray: NSArray = []

@H_404_8@

override func viewDidLoad() {

super.viewDidLoad()

@H_404_8@

// 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")

@H_404_8@

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

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

return cell

@H_404_8@

@H_404_8@

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

猜你在找的Swift相关文章