zoukankan      html  css  js  c++  java
  • [Swift] Storyboard outlet and action

    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")
        }
  • 相关阅读:
    遮罩层点击空白退出代码
    不同浏览器的margin值与padding值
    让div自适应浏览器窗口居中显示
    导航相关(下方导航指示条居中)
    CSS相邻兄弟选择器
    使用font-size:0去掉inline-block元素之间的空隙
    box-sizing属性
    常见浏览器兼容性问题
    秋季编程总结
    POJ 1193 内存分配
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6082056.html
Copyright © 2011-2022 走看看