在 UISplitViewController 上显示图像
Show Image on UISplitViewController
我是 SWIFT 的新手,我做了一些示例应用程序。
我唯一无法工作的是让我的图像显示在细节场景 (DetailViewController) 上。
在我的 MasterViewController 文件上,我有这个来获取我的 multiData
文件:
...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showData" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = self.multiData[indexPath.section][indexPath.row]
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
// Here I get my multiData
func createData() {
var first: [Dictionary<String,String>] = []
var second: [Dictionary<String,String>] = []
dataSections = ["First Data", "Second Data"]
first.append(["name": "someName", "image": "somePngFile", "someData": "someText"])
second.append(["name": "someName", "image": "somePngFile", "someData": "someText"])
multiData = [first, second]
}
细节视图控制器:
...
@IBOutlet weak var label: UILabel!
@IBOutlet weak var text: UITextView!
@IBOutlet weak var image: UIImageView!
...
func configureView() {
if let detail = self.detailItem {
if let labelTitle = label {
labelTitle.text = detail["name"] as! String!
}
if let textData = text {
textData.text = detail["someData"] as! String!
}
// This obvious doesn't work
if let imageFile = image {
imageFile.image = detail["image"] as! UIImage!
}
}
}
所以,我的问题是如何从 detail["image"]
获取图像?
我假设 detail["image"]
中包含的图像在您的应用程序包中(即:不是来自 HTTP
URL 的图像)。所以,你应该使用 init(named:)
constructor from UIImage
来加载你的图像。类似的东西:
if let myImage = UIImage(named: detail["image"]) {
myImageView.image = myImage
}
编辑:
这也是您的代码,在 configureView
中更好地管理了可选值和强制转换:
func configureView() {
if let detail = self.detailItem {
if let labelTitle = detail["name"] as? String {
myLabel.text = labelTitle
}
if let textViewContent = detail["someData"] as? String {
myTextView.text = textViewContent
}
if let myImageName = detail["image"] as? String {
if let myImage = UIImage(named: myImageName) {
myImageView.image = myImage
}
}
}
}
我是 SWIFT 的新手,我做了一些示例应用程序。
我唯一无法工作的是让我的图像显示在细节场景 (DetailViewController) 上。
在我的 MasterViewController 文件上,我有这个来获取我的 multiData
文件:
...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showData" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = self.multiData[indexPath.section][indexPath.row]
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
// Here I get my multiData
func createData() {
var first: [Dictionary<String,String>] = []
var second: [Dictionary<String,String>] = []
dataSections = ["First Data", "Second Data"]
first.append(["name": "someName", "image": "somePngFile", "someData": "someText"])
second.append(["name": "someName", "image": "somePngFile", "someData": "someText"])
multiData = [first, second]
}
细节视图控制器:
...
@IBOutlet weak var label: UILabel!
@IBOutlet weak var text: UITextView!
@IBOutlet weak var image: UIImageView!
...
func configureView() {
if let detail = self.detailItem {
if let labelTitle = label {
labelTitle.text = detail["name"] as! String!
}
if let textData = text {
textData.text = detail["someData"] as! String!
}
// This obvious doesn't work
if let imageFile = image {
imageFile.image = detail["image"] as! UIImage!
}
}
}
所以,我的问题是如何从 detail["image"]
获取图像?
我假设 detail["image"]
中包含的图像在您的应用程序包中(即:不是来自 HTTP
URL 的图像)。所以,你应该使用 init(named:)
constructor from UIImage
来加载你的图像。类似的东西:
if let myImage = UIImage(named: detail["image"]) {
myImageView.image = myImage
}
编辑:
这也是您的代码,在 configureView
中更好地管理了可选值和强制转换:
func configureView() {
if let detail = self.detailItem {
if let labelTitle = detail["name"] as? String {
myLabel.text = labelTitle
}
if let textViewContent = detail["someData"] as? String {
myTextView.text = textViewContent
}
if let myImageName = detail["image"] as? String {
if let myImage = UIImage(named: myImageName) {
myImageView.image = myImage
}
}
}
}