Swift basic syntax controls 02 and swift syntax controls 02
// The first controller: displays the basic control.
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 (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.
}
}
// The second controller: displays the table view.
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.
}
}