Swift basic syntax controls 02 and swift syntax controls 02

Source: Internet
Author: User

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.

}


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.