Question
I am currently making an app in Swift Playgrounds with a multi page layout. After the first button is pressed, it will load a new
I am currently making an app in Swift Playgrounds with a multi page layout. After the first button is pressed, it will load a new page with a new button, and I want this button to have a different action than the first one. At the moment, the first button prints out hello, but I would like the second button to print out something else such as "Hey". Please help me fix this. Code is below. Thanks.
//: Playground - noun: a place where people can play
import UIKit
import XCPlayground
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let color = UIColor(red: 1, green: 1, blue: 0, alpha: 1)
let leftMargin = 20
let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568)) // iPhone 5 proportions
view.backgroundColor = UIColor.white
// LABEL
let label = UILabel(frame: CGRect(x:leftMargin, y: 15, width: 300, height: 44))
label.text = "My name is Aalap Patel."
label.textColor = UIColor.black
view.addSubview(label)
label.font = UIFont(name:"Menlo",size:20)
label.textAlignment = .center
//LABEL2
let label2 = UILabel(frame: CGRect(x:45, y: 45, width: 300, height: 44))
label2.text = "Click below to learn about me!"
label2.textColor = UIColor.black
view.addSubview(label2)
//VIEW1
_ = UIColor(red: 1, green: 1, blue: 0, alpha: 1)
_ = 20
let view1 = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568)) // iPhone 5 proportions
view1.backgroundColor = UIColor.white
//BUTTON
class Responder: NSObject {
func tap() {
print("Hello")
PlaygroundPage.current.liveView = view1
}
}
let responder = Responder()
// 3
let button = UIButton(type: .system)
button.setTitle("Click me!", for: .normal)
button.addTarget(responder, action: #selector(Responder.tap), for: .touchUpInside)
button.titleLabel?.font = UIFont(name:"Helvetica", size: 25)
button.sizeToFit()
button.center = CGPoint(x: 160, y: 150)
// 4
let frame = CGRect(x: 0, y: 0, width: 200, height: 100)
view.addSubview(button)
PlaygroundPage.current.liveView = view
//BUTTON2
func touch() {
print("You made it to the second page!")
}
let responder1 = Responder()
// 3
let button1 = UIButton(type: .system)
button1.setTitle("Page 2!", for: .normal)
button1.addTarget(responder1, action: #selector(Responder.tap), for: .touchUpInside)
button1.titleLabel?.font = UIFont(name:"Helvetica", size: 25)
button1.sizeToFit()
button1.center = CGPoint(x: 160, y: 150)
func buttonClick (sender: UIButton){
print("You have clicked")
}
// 4
let frame1 = CGRect(x: 0, y: 0, width: 200, height: 100)
view1.addSubview(button1)
PlaygroundPage.current.liveView = view1
//IMAGE VIEW
/*
let image = UIImageView(frame: CGRect(x: leftMargin, y: 140, width: 151, height: 151))
image.image = UIImage(named:"IMG_7666.jpg")
view.addSubview(image)
image.layer.cornerRadius = image.frame.size.width/2
image.clipsToBounds = true
*/
PlaygroundPage.current.liveView = view
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started