Post

Replies

Boosts

Views

Activity

swift code not working as intended in Xcode
I wanted to build a two-screen app. The first screen prompts the user to click the button to trigger a segue to the second screen. The second screen will contain a table view with three or more cells, each containing a UIImageView and a label. When the user clicks on one of the Table View cells on the second screen, that screen should be dismissed, and the background image of the first screen should be updated based on the selected cell on the second screen and present the option to do it again. import UIKit class FirstViewController: UIViewController { @IBOutlet weak var backgroundImageView: UIImageView! func didSelectImage(_ image: UIImage) { backgroundImageView.image = image } @IBAction func buttonTapped(_ sender: Any) { performSegue(withIdentifier: "showSecondScreen", sender: nil) } } ![]("https://developer.apple.com/forums/content/attachment/cd03b00d-f469-4d16-8aa4-11b042962937" "title=Image.png;width=828;height=1792") import UIKit protocol SecondScreenDelegate: AnyObject { func didSelectImage(_ image: UIImage) } class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let images = ["Beautiful-Beach.jpeg", "Daisy.png", "Fall_season.jpeg", "purple.jpg"] let labels = ["Beautiful-Beach", "Daisy", "Fall_season", "purple"] weak var delegate: SecondScreenDelegate? func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return images.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.imageView?.image = UIImage(named: images[indexPath.row]) cell.textLabel?.text = labels[indexPath.row] return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { delegate?.didSelectImage(UIImage(named: images[indexPath.row])!) dismiss(animated: true, completion: nil) } }
1
1
375
Nov ’23