码迷,mamicode.com
首页 > 编程语言 > 详细

[Swift] Storyboard outlet and action

时间:2016-11-20 07:21:00      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:for   nts   product   -418   text   height   code   with   content   

To programmaictlly change the content of app, we need to contect storyboard to a view controller.

 

To do that, we need to create new file call it ‘ProductViewController‘, subclass should UIViewController:

技术分享

 

Delete some uncessary code, the remain code should looks like:

import UIKit

class ProductViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

 

Link view to controller:

Click the view panel, and on the right side give the Custom class name as ‘ProductViewController‘:

技术分享

 

Link ui elements to outlets:

Click ‘ctrl‘ & drag the image point to the code:

技术分享

Give the name and save.

We can use ‘Image Literal‘ to auto select the image:

技术分享

技术分享

 

DO the same for the label,  then the code looks like:

import UIKit

class ProductViewController: UIViewController {

    @IBOutlet weak var productNameLabel: UILabel!
    @IBOutlet weak var productViewImage: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()

        productNameLabel.text = "1937 Desk Phone";
        productViewImage.image = #imageLiteral(resourceName: "phone-fullscreen3");
    }
}

 


 

class CourseViewController : UIViewController {

  @IBOutlet weak var courseNameLabel: UILabel!
  @IBOutlet weak var courseImageView: UIImageView!

  override func viewDidLoad() {
    super.viewDidLoad()

    var courseName = "App Evolution With Swift"

    courseNameLabel.text = courseName
    courseImageView.image = UIImage(named: "course-badge")
  }
}

 


技术分享

    @IBAction func addProductAction(_ sender: UIButton) {
        print("Button tapped")
    }

 

[Swift] Storyboard outlet and action

标签:for   nts   product   -418   text   height   code   with   content   

原文地址:http://www.cnblogs.com/Answer1215/p/6082056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!