Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am currently making an app in Swift Playgrounds with a multi page layout. Afte

ID: 3822029 • Letter: I

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 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

Explanation / Answer

To change the action of the second button you just have to look at the function touch() followed followed by the second button that is //Button 2. If you want to change the text that is visible after pressing the button, just change the

func touch() {

print("You made it to the second page!")

}

to

print("hey!")

Please post another question for other changes.