首页 > 代码库 > [Swift] Storyboard outlet and action
[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 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") }
[Swift] Storyboard outlet and action
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。