4.3. Creating the Display Screen
Now, we need to create another screen to display the data the user sends from the first screen. We need to create two swift files here, one is for the view (DisplayView), and another is for the view controller (DisplayViewController).
DisplayView.swift
We have two Labels in the view:
The first Label displays the message.
The second Label displays the mood.
So create the 'DisplayView' file like before and add the codes as follows:
//
// DisplayView.swift
// App4
//
import UIKit
class DisplayView: UIView {
var labelMessage: UILabel!
var labelMood: UILabel!
//MARK: View initializer...
override init(frame: CGRect) {
super.init(frame: frame)
//setting the background color...
self.backgroundColor = .white
setupLabelMessage()
setupLabelMood()
initConstraints()
}
//MARK: initializing the UI elements...
func setupLabelMessage(){
labelMessage = UILabel()
labelMessage.textAlignment = .left
labelMessage.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(labelMessage)
}
func setupLabelMood(){
labelMood = UILabel()
labelMood.textAlignment = .left
labelMood.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(labelMood)
}
//MARK: initializing the constraints...
func initConstraints(){
NSLayoutConstraint.activate([
labelMessage.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 32),
labelMessage.leadingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leadingAnchor, constant: 16),
labelMood.topAnchor.constraint(equalTo: labelMessage.bottomAnchor, constant: 16),
labelMood.leadingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leadingAnchor, constant: 16),
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Adding DisplayViewController
Now, let's add the view controller for the display screen to our project:
Now let's create a separate file, 'DisplayViewController.swift' in the project.
Click File -> New -> File...
Select Cocoa Touch Class and press Next
The class name should be DisplayViewController.
Select UIViewController as for 'Subclass of.' And press Next.
Press Create.
Now, we initialize DisplayView and patch the view to the controller:
Last updated
Was this helpful?