WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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.

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.