博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift] Storyboard outlet and action
阅读量:4965 次
发布时间:2019-06-12

本文共 1571 字,大约阅读时间需要 5 分钟。

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 UIKitclass 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 UIKitclass 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")    }

 

转载于:https://www.cnblogs.com/Answer1215/p/6082056.html

你可能感兴趣的文章
linux 命令之top
查看>>
有关远程设置的问题
查看>>
BZOJ 1800: [Ahoi2009]fly 飞行棋
查看>>
2019,2月份第三个星期,js小突破了一波,笔记
查看>>
洛谷 [P3033] 牛的障碍
查看>>
jquery 对HTML标签的克隆、删除
查看>>
用C写的俄罗斯方块游戏 By: hoodlum1980 编程论坛
查看>>
05最小生成树_Prim
查看>>
centos iptables
查看>>
Mysql数据库日志
查看>>
Python基础-数据类型
查看>>
unity3d 移动与旋转 2
查看>>
MyEclipse安装Freemarker插件
查看>>
php 文件下载
查看>>
寻找二叉查找树中比指定值小的所有节点中最大的那个节点
查看>>
如何设置输入框达到只读效果
查看>>
html5模拟平抛运动
查看>>
java面向对象下:Java数据库编程
查看>>
RT3070 USB WIFI 在连接socket编程过程中问题总结
查看>>
Traffic Management Gym - 101875G
查看>>