2016-04-18 34 views
3

我試圖從tableview控制器B傳遞一個圖標和一個名稱,通過委託來查看控制器A.將TableviewController B中的圖標和名稱傳遞給TableviewController A

爲了把這個簡單的,一個用戶呈現的ViewController A:(這裏顯示)

的ViewController A和源代碼

enter image description here

的ViewController源

// 
// MasterTableViewController.swift 
// Sample image 
// 
// 

import UIKit 

class MasterTableViewController: UITableViewController, IconselectedDelegate { 

@IBOutlet weak var passedIconName: UILabel! 

var tempname: String? 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.title = "Master" 
} 

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(true) 

} 

func iconName(iconName: String) -> String { 

    self.tempname = iconName 

    self.passedIconName.text = "\(iconName)" 

    return tempname! 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
} 

因此,當用戶點擊這個單元格,然後向用戶展示另一個顯示圖標列表的視圖控制器(viewController B)。以下是此示例的屏幕截圖和源代碼。

的ViewController乙

enter image description here

這裏是的viewController乙 的ViewController B碼

// 
// IconTableViewController.swift 
// Sample image 
// 
// Created by me on 4/18/16. 
// 

import UIKit 

protocol IconselectedDelegate { 
func iconName(iconName: String)-> String 
} 

class IconTableViewController: UITableViewController { 

enum Icon: Int { 
    case Study 
    case Apple 

    var icon: UIImage { 
     switch self { 
     case.Study: return UIImage(named: "study")! 
     case.Apple: return UIImage(named: "apple")! 
     } 
    } 
} 


var icons = ["Study", "Apple"] 

var delegate: IconselectedDelegate? = nil 
var selectedIndex = 0 

// holding vars 
var image: UIImage? 
var name: String? 


override func viewDidLoad() { 
    super.viewDidLoad() 


    self.title = "Icon selection" 


} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return icons.count 
} 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let cell: UITableViewCell! = tableView.cellForRowAtIndexPath(indexPath) 

    if cell.accessoryType == UITableViewCellAccessoryType.None { 


     if indexPath.row == 0 { 

      self.name = icons[0] 

     } else if indexPath.row == 1 { 
      self.name = icons[1] 
     } 


     cell.accessoryType = UITableViewCellAccessoryType.Checkmark 
     self.navigationController?.popToRootViewControllerAnimated(true) 
    } 

    selectedIndex = indexPath.row 

    tableView.reloadData() 
    tableView.deselectRowAtIndexPath(indexPath, animated: true) 
} 


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


    let cellIdentifier = "Cell" 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! IconTableViewCell 



    // Configure the cell... 
    cell.iconName.text = icons[indexPath.row] 

    if let iconIndex = Icon(rawValue: indexPath.row) { 

     cell.iconPreview.image = iconIndex.icon 
    } 

    return cell 
} 

override func viewWillDisappear(animated: Bool) { 
    super.viewWillAppear(true) 

    if name == nil { 
     delegate?.iconName("None") 
    } else { 
     delegate?.iconName(name!) 
     print("Delegate left with \(delegate?.iconName(name!))") 
    } 
} 


} 

問題

示例代碼

我想將所選圖標傳遞迴顯示名稱和圖標圖像的主視圖控制器。 (在主視圖控制器中,有一個UIImage對象,但你不能在UILable旁邊看到它說'選擇')

我該如何做到這一點?我用這個類似的代碼來完成UIColor,它工作,但它似乎並沒有處理圖像。

將極大地幫助

+0

我想你是不是委託屬性設置爲自在Viewcontroller中。按照慣例,您的委託名稱應該以大寫字符開頭:SetIconValueDelegate。請參閱我的答案並管理您的代碼 – Lion

回答

0

可以使用delegate(protocol)方法,數據發送回一個視圖控制器。

當前VC:

protocol MyProtocol: class 
{ 
    func sendArrayToPreviousVC(myArray:[AnyObject]) 
} 

,讓您的一類變種。

weak var mDelegate:MyProtocol? 

現在調用協議方法,當你彈出視圖控制器,你的「屬性」數組作爲參數。 (在你的情況下,從沒有選擇行通過你想要什麼樣的圖標或名稱)

mDelegate?.sendArrayToPreviousVC(properties) 

在以前的VC:

在以前的VC,設置mDelegate屬性自我,當你把當前VC 。

currentVC.mDelegate = self 
    //PUSH VC 

現在在你以前的VC中實現協議方法。

func sendArrayToPreviousVC(myArray:[AnyObject]) { 
    //DO YOUR THING 
} 

這是如何在導航堆棧中發回數據的場景。根據您的數據和要求進行調整。

您應該使用didselect行委託方法來傳遞數據。 希望這將有助於:)

+0

這不起作用,我得到的是與上述答案相同的確切錯誤 – DwellingNYC

3

設定值從DestinationViewController回到你的主(前)的ViewController

實現一個協議,例如創建一個名爲protocol.swift文件。

protocol SetIconValueDelegate { 
     func didChoose(icon:Icon) 
    } 

2.設置委託在你的第二個視圖控制器

class yourNextViewControllerClass { 

    var delegate:SetIconValueDelegate? 

設置,當你加載secondViewController(配合圖標詳細)委託

if(segue.identifier == "yourIdentifierInStoryboard") { 

     var yourNextViewController = (segue.destinationViewController as yourNextViewControllerClass) 
     yourNextViewController.delegate = self 

4.將函數添加到FirstViewController

func didChoose(icon:Icon) { 
     // set here whatever you want 
    } 

5.呼叫從自己的SecondViewController此功能

 delegate?.didChoose(icon) // this will send your "icon" object - run this from didSelectRowAtIndexPath 

6.將委託你FirstViewController

class FirstViewController: UIViewController, setIconValueDelegate { 
+0

按照慣例,您的委託名稱應以大寫字符開頭:SetIconValueDelegate。此外,更符合Swift命名的函數名稱可能類似於* didChoose(icon:Icon)*請參閱此接受的提案,以瞭解爲什麼應從方法名稱修剪單詞「icon」* didChooseIcon(icon :Icon)*:https://github.com/apple/swift-evolution/blob/master/proposals/0005-objective-c-name-translation.md – Moonwalkr

+1

感謝您的建議 - 我現在更改了一些名稱。 – derdida

+0

當你引用「從你的secondViewController調用這個函數」時,你是指在FirstViewController中實現的函數中添加'delegate?.didChoose(icon)',還是指從secondViewController的方法調用這個委託? – DwellingNYC

相關問題