Edussentials
Edussentials

Reputation: 1

View controller only loading images correctly the first time

I'm using a view controller to dynamically show buttons and buttons with images. I re-use this view controller at different levels in my app. The first time it opens it's showing the images correctly. When I go one step back and re-open the same view it shows the last image loaded on all buttons.

Below are screen shots showing it working correctly the first time yet not the second time. What am I doing wrong?

Correct:

Correct

Showing all the same image:

Showing all the same image

Relevant Code:

override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.navigationController?.navigationBar.isHidden = false
        AddImages()
        ListFolders()
}



func ListFolders(){
    do {
        let filemgr = FileManager.default
        let filelist = try filemgr.contentsOfDirectory(atPath: BaseFolder + "/Edussentials/Home/" + MyFolder)

        for file in filelist {
            if (MyFunctions().mid(file,0,4) == "BUT_") || (MyFunctions().mid(file,0,4) == "IBT_") {
                IncreaseMyArrays()
                MyButton_Name[ButtonCount] = MyFunctions().mid(file, 4, 50)
                MyButton_Type[ButtonCount] = MyFunctions().mid(file, 0, 4)
                ReadButtons(ButtonFolder: BaseFolder + "/Edussentials/Home/" + MyFolder + "/" + file)
                ButtonCount += 1
            }
        }
        CreateIButtons()
        CreateButtons()

    } catch let error {
        print("Error: \(error.localizedDescription)")
    }
}


func CreateIButtons(){

    var i: Int = 0
    repeat {
        if (MyButton_Type[i] == "IBT_"){
            let button = UIButton(frame: CGRect(x: Int(MyDisplayWidth / 100 * Float(MyButton_xPos[i])), y: Int(MyDisplayHeight / 100 * Float(MyButton_yPos[i] + MySetFromTop)), width: Int(MyDisplayWidth / 100 * Float(MyButton_Width[i])), height: Int(MyDisplayHeight / 100 * Float(MyButton_Height[i]))))
            let imageURL = URL(fileURLWithPath: BaseFolder + "/Edussentials/Home/" + MyFolder + "/IBT_" + MyButton_Name[i] + "/MyImage.gif")
            let MyImage = UIImage(named: imageURL.path)!
            button.setImage(MyImage, for: UIControlState.normal)
            button.tag = i
            button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
            self.view.addSubview(button)
        }
        i += 1
    } while i < ButtonCount
}

Upvotes: 0

Views: 113

Answers (2)

Edussentials
Edussentials

Reputation: 1

The correct way of pointing to the image is using UIImage(contentsOfFile: "MyFullPath") and not UIImage(named: "MyFullPath") The problem was that all my images have the same name but are located in different directories. For some reason UIImage dit get the correct image the first time the view was called. However the second time the view was called it pointed to the last found directory hence showing the same image on every button.

button.setImage(UIImage(contentsOfFile: BaseFolder + "/Edussentials/Home/" + MyFolder + "/IBT_" + MyButton_Name[i] + "/MyImage.gif"), for: UIControlState.normal)

Upvotes: 0

Sharad Paghadal
Sharad Paghadal

Reputation: 2154

As you said, you are reusing that view to multiple times into your app. Here you had put if for customising your view, but forgot to implement else to make that view into it's original state.

Put your code to make view to it default state using else after every if.

Upvotes: 1

Related Questions