图像未设置为UIImageView中的嵌套UIViewController

时间:2015-04-29 09:49:23

标签: ios swift uiviewcontroller

我遇到了swift的问题。

import UIKit

class StarbucksRefreshersBeverages: UIViewController, UITableViewDelegate {

@IBAction func doneButtonPressed(sender: AnyObject) {  
    self.dismissViewControllerAnimated(true, completion: nil)
}
var drink = "blah"

var refreshersDrinks = ["Cool Lime Starbucks Refreshers", "Valencia Orange Starbucks Refreshers", "Verry Berry Hibiscus Starbucks Refreshers"]

var refresherDrink = "blahblah"

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return refreshersDrinks.count   
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "starbucksRefreshersPrototypeCell")

    cell.textLabel?.text = refreshersDrinks[indexPath.row]

    return cell
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.refresherDrink = refreshersDrinks[indexPath.row]

    self.performSegueWithIdentifier("starbucksRefreshersToImageViewSegue", sender: self) 
}
/*
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {


    var detailViewController = segue.destinationViewController as RefreshersImageView

    detailViewController.refresherDrink = self.refresherDrink

    if self.refresherDrink == "Cool Lime Starbucks Refreshers" {

        detailViewController.refreshersImageView.image = UIImage( contentsOfFile: "coolLime.png")


    }

    if self.refresherDrink == "Valencia Orange Starbucks Refreshers" {


        detailViewController.refreshersImageView == UIImage(contentsOfFile: "valenciaOrange.png")

    }


}

*/



override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {


    if segue.identifier == "starbucksRefreshersToImageViewSegue" { // is set in interface builder


        let detailViewController = segue.destinationViewController as RefreshersImageView

        if self.refresherDrink == "Cool Lime Starbucks Refreshers" {

            detailViewController.image = UIImage(contentsOfFile: "coolLime.png")

        }

        if self.refresherDrink == "Valencia Orange Starbucks Refreshers" {


            detailViewController.image = UIImage(contentsOfFile: "valenciaOrange.png")

        }

    }


}
}

在第

detailViewController.image = UIImage(contentsOfFile: "coolLime.png")

我想将下一个视图控制器的图像视图设置为我想要显示的.png文件。但是当我导航到视图控制器应该显示图像时,没有图像,我根本没有任何错误。这是另一个应该显示图像的View Controller;

import Foundation
import UIKit

class RefreshersImageView: UIViewController {

@IBOutlet weak var refreshersImageView: UIImageView!
var refresherDrink = "blahblah"
var image: UIImage?

override func viewDidLoad() {
    super.viewDidLoad()

    image = refreshersImageView.image
}
}

这真的令人沮丧,我也尝试了所有的堆栈溢出结果,但nınıf他们工作。请帮助!

1 个答案:

答案 0 :(得分:1)

RefreshersImageView

改变:

image = refreshersImageView.image

有:

refreshersImageView.image = image

此外:

  • 调用UIViewController RefreshersImageView是不好的做法。这给人的印象是它只是一个小部件,而不是一个完整的视图控制器。
  • 如@Woodstock所述,请检查您的目标中是否已选中所有图片并将其复制到应用程序包中。
  • 同时检查图片名称大写