2015-11-26 77 views
0

我試圖將UIImage()變量的內容傳遞給下一個視圖控制器的單元格。將UIImage傳遞給另一個視圖控制器的單元格

基本上我的佈局是這樣的:

的圖像在ControllerA加載,然後保存在一個模型。

在下一個控制器中,加載一個TableViewCell。

我想要在單元格內加載圖片。

我該如何做到這一點?

我的代碼:

第一部分:

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

    // Save the poem. 
    savePoem() 
} 

func savePoem() { 
    // Save the poem current completion date. 
    poem.date = NSDate() 

    // Save the theme name for the poem. 
    poem.theme = theme.name 

    // Save the currently displayed picture. 
    poem.image = ImageView.image 

    poem.words = textField.words 

    // Make the poem object persist. 
    PoemPersistence.sharedInstance.persistPoem(poem) 
} 

細胞加載圖片:

func configureWithPoem(poem: Poem) { 
    themeLabel.text = "#\(poem.theme)" 
    poemLabel.text = poem.words 
    pictureImageView.image = poem.image 
} 

func capturePoemImage() -> UIImage { 
    // Hide the share button. 
    shareButton.hidden = true 

    // Capture a PNG of the view. 
    UIGraphicsBeginImageContextWithOptions(frame.size, false, 0) 
    layer.renderInContext(UIGraphicsGetCurrentContext()!) 
    let containerViewImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    // Show the share button. 
    shareButton.hidden = false 

    return containerViewImage 
} 

元得以在第二控制器中加載:

func poemCellWantsToSharePoem(poemCell: PoemCell) { 
    // Find the poem displayed at this index path. 
    let indexPath = tableView.mp_indexPathForCell(poemCell) 
    let poem = poems[indexPath.row] 

    // Generate the image of the poem. 
    let poemImage = poemCell.capturePoemImage() 

回答

0

,如果你只要你使用prepareForSegue()將數據發送到下一個UIViewController - 將segue創建到下一個控制器(control + Drag),然後爲該標識符指定一個名稱。

  • 現在通過聲明

    @IBOutlet weak var imageView:UIImageView! 
        var image:String ? 
    override func viewDidLoad() 
    { 
        imageView.image = image  
    } 
    

    這一形象變量將接收數據然後將其分配給@IBOutlet設置你的目標控制器。

    func prepareForSegue (segue:UIStoryboardSegue, sender:AnyObject?) 
        { 
    
         if segue.identifier == "NAMEOFSEGUEIDENTIFIER" 
         { 
          let destinationController = segue.destinationViewController as! NAMEOFTHEDESTIONCONTROLLER 
          let indexPath = tableView.indexPathForSelectedRow! 
         // now the data structure that contains your information, use it there 
          let data = dataContainer[indexPath.row] 
    
         // i noticed that you might be using struct so 
         // let's passed the picture from that specific row to the next conroller 
         destinationController.image = data.image 
         } 
        } 
    

我希望幫助:)

相關問題