UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

day 18: challenge 3, i used Switch/Case to change title, it worked but is it ok?

Forums > 100 Days of Swift

I used the below method to change the detail viewcontroller navigation bar title to "Picture 1 of 10", for example.

I changed the Title property using Switch/Case method. Is it ok? attached screenshot link: https://drive.google.com/file/d/1E0814xrKI5hiN8rvv2hVDztWbHEL7UXx/view?usp=sharing

class DetailViewController: UIViewController {
    @IBOutlet var imageView: UIImageView!
    var selectedImage: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        //change navigation bar title of picture page
        switch selectedImage{
        case "nssl0033.jpg":
            title = "Picture 1 of 10" //both are optionals so no need to unwrap
        case "nssl0034.jpg":
            title = "Picture 2 of 10" //both are optionals so no need to unwrap
        case "nssl0041.jpg":
            title = "Picture 3 of 10" //both are optionals so no need to unwrap
        case "nssl0042.jpg":
            title = "Picture 4 of 10" //both are optionals so no need to unwrap
        case "nssl0043.jpg":
            title = "Picture 5 of 10" //both are optionals so no need to unwrap
        case "nssl0045.jpg":
            title = "Picture 6 of 10" //both are optionals so no need to unwrap
        case "nssl0046.jpg":
            title = "Picture 7 of 10" //both are optionals so no need to unwrap
        case "nssl0049.jpg":
            title = "Picture 8 of 10" //both are optionals so no need to unwrap
        case "nssl0051.jpg":
            title = "Picture 9 of 10" //both are optionals so no need to unwrap
        case "nssl0091.jpg":
            title = "Picture 10 of 10" //both are optionals so no need to unwrap
        default:
            title = nil //both are optionals so no need to unwrap
        }

        navigationItem.largeTitleDisplayMode = .never

        // Do any additional setup after loading the view.
        if let imageToLoad = selectedImage{

            imageView.image = UIImage(named: imageToLoad)
        }
    }

Edit*:I also just solved using the recommended method by modifying from View Controller.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.