Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios View controller仅在第一次正确加载图像_Ios_Swift - Fatal编程技术网

Ios View controller仅在第一次正确加载图像

Ios View controller仅在第一次正确加载图像,ios,swift,Ios,Swift,我正在使用视图控制器动态显示按钮和带有图像的按钮。我在应用程序的不同级别重复使用此视图控制器。第一次打开时,它会正确显示图像。当我后退一步并重新打开同一视图时,它会显示所有按钮上加载的最后一个图像 下面是屏幕截图,显示它第一次正常工作,但不是第二次。我做错了什么 正确: 显示所有相同的图像: 相关代码: override func viewDidLoad() { super.viewDidLoad() // Do any additional setup

我正在使用视图控制器动态显示按钮和带有图像的按钮。我在应用程序的不同级别重复使用此视图控制器。第一次打开时,它会正确显示图像。当我后退一步并重新打开同一视图时,它会显示所有按钮上加载的最后一个图像

下面是屏幕截图,显示它第一次正常工作,但不是第二次。我做错了什么

正确:

显示所有相同的图像:

相关代码:

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
}
override func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
self.navigationController?.navigationBar.isHidden=false
AddImages()
ListFolders()
}
func ListFolders(){
做{
让filemgr=FileManager.default
让filelist=try filemgr.contentsOfDirectory(路径:BaseFolder+“/edusessentials/Home/”+MyFolder)
对于文件列表中的文件{
如果(MyFunctions().mid(文件,0,4)=“但是”| |(MyFunctions().mid(文件,0,4)=“IBT|”){
IncreaseMyArrays()
MyButton\u名称[ButtonCount]=MyFunctions().mid(文件,4,50)
MyButton_类型[ButtonCount]=MyFunctions().mid(文件,0,4)
ReadButtons(ButtonFolder:BaseFolder+“/edusessentials/Home/”+MyFolder+“/”+文件)
按钮计数+=1
}
}
CreateIButtons()
CreateButtons()
}捕捉错误{
打印(“错误:\(Error.localizedDescription)”)
}
}
func CreateIButtons(){
变量i:Int=0
重复{
如果(我的按钮类型[i]=“IBT”){
let button=ui按钮(帧:CGRect(x:Int(MyDisplayWidth/100*Float(MyButton\u xPos[i])),y:Int(MyDisplayHeight/100*Float(MyButton\u yPos[i]+MySetFromTop)),宽度:Int(MyDisplayWidth/100*Float(MyButton\u width[i]),高度:Int(MyDisplayHeight/100*Float(MyButton\u height[i]))
让imageURL=URL(fileURLWithPath:BaseFolder+“/edusessentials/Home/”+MyFolder+“/IBT_”+MyButton_Name[i]+“/MyImage.gif”)
让MyImage=UIImage(名为:imageURL.path)!
按钮.setImage(MyImage,用于:uicontrol状态.normal)
button.tag=i
button.addTarget(self,action:#选择器(buttonAction),用于:.touchUpInside)
self.view.addSubview(按钮)
}
i+=1
}当我按下按钮时
}

正如您所说,您在应用程序中多次重复使用该视图。在这里,您已经放置了
if
用于自定义视图,但忘记了实现
else
以使该视图处于原始状态


在每次
之后使用
else
将代码置于默认状态,如果

指向图像的正确方式是使用UIImage(contentsOfFile:“MyFullPath”)而不是UIImage(名为“MyFullPath”) 问题是,我的所有图像都有相同的名称,但位于不同的目录中。由于某些原因,UIImage dit在第一次调用视图时获得正确的图像。然而,第二次调用视图时,它指向最后找到的目录,因此在每个按钮上显示相同的图像

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

您在viewcontroller中的何处使用此代码?在从ViewDidLoad调用的函数中的view controller中,请在此行之前添加此语句
print(imageURL)
看到了吗?当你再次打开视图时,它是否会打印不同的URL?对于用作背景的图像,我遇到了相同的问题。代码在同一个视图控制器中//设置背景让imagebg=UIImageView(帧:cRect(x:Int(MyDisplayWidth/100*Float(0)),y:Int(MyDisplayHeight/100*Float(MySetFromTop)),宽度:Int(MyDisplayWidth/100*Float(100)),高度:Int(MyDisplayHeight/100*Float(100)))让imageURLbg=URL(fileURLWithPath:BaseFolder+“/edusessentials/Home/“+MyFolder+”/background.jpg”)imagebg.image=UIImage(名为:imageURLbg.path)self.view.addSubview(imagebg)我试过了,它使用了不同的URL