簡體   English   中英

使用prepareForSegue將圖像文件名從一個ViewController發送到下一個ViewController

[英]Using prepareForSegue to send an image file name from one ViewController to the next

我正在嘗試將圖像數據從一個視圖控制器發送到另一個。 數據被硬編碼在一個單獨的快速文件中。 我在xcode中有一個數據文件,其中包含以下內容:

class Data {

struct Entry {

    let filename: String
    let heading: String
    let headerSub: String

    init(profilePicture: String, profileTitle: String, profileSub: String) {
        self.heading = profileTitle
        self.filename = profilePicture
        self.headerSub = profileSub
    }

}

let headerData = [

    Entry(profilePicture: "image1.png", profileTitle: "person1", profileSub: ""),
    Entry(profilePicture: "image2.png", profileTitle: "person2", profileSub: ""),
]

}

而且,在我的ViewController中,我有以下內容:

let categorySegueIdentifier = "CategoryDetailSegue"

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == categorySegueIdentifier {
            if let destination = segue.destinationViewController as? RequestViewController {
                if let categoryIndex = tableView.indexPathForSelectedRow?.row {
                destination.categoryImage.image = data[0]

            }
        }
    }
}

在ViewController的開頭,我添加了以下內容-

let data = Data()

在我的目標視圖控制器中,我有以下內容-

var categoryImage = UIImage()

override func viewWillAppear(animated: Bool) {
    headerImage.image = categoryImage
}

為什么會出現以下錯誤:UIImage沒有名為image的成員

知道可能是什么問題嗎?

我認為您想這樣做:

destination.categoryImage = data[0]

不是這個:

destination.categoryImage.image = data[0]

假設data[0]UIImage 如果是文件名,您當然要這樣做:

if let image = UIImage( named:data[0] ) {
    destination.categoryImage = image
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM