[英]Swift - How call UIVIewController from a Button in UITableViewCell
[英]How to create a UIView from a UITableViewCell button click on a separate UIViewController?
我正在 Xcode/Swift 中開發一個 iOS 應用程序,當在單獨的UITableViewController
中單擊UITableViewCell
按鈕(它本身嵌入在UIView
中)時,我試圖在我的MainViewController
中添加一個子視圖UISendViewButton
。 基本上,這個概念就像 Instagram 中的“發送帖子”按鈕:用戶將單擊一個紙飛機按鈕,然后會出現一個單獨的朋友列表( UIView
-> UITableViewController
)。 在聯系人列表旁邊,有一個按鈕 ( customButton
),用戶可以單擊它來選擇要將其發送給哪些朋友。 我想要的是只有當用戶決定單擊他們朋友姓名旁邊的按鈕( customButton
)時,才會出現“發送”按鈕( UIViewButton
)。
我能夠通過將UITableViewController
嵌入到UIView
,然后將其作為子視圖添加到我的MainViewController
中來使它出現,但是當我單擊UITableViewCell
customButton
中的 customButton 時,沒有任何反應。 我希望在單擊customButton
時出現一個新的UIViewButton
(在我的MainViewController
中)。
所以基本上我想知道如何讓這兩個控制器進行通信。 包含UITableViewCell
按鈕的 controller 是由 Xcode 的庫提供的: UITableViewController
-> UITableView
-> UITableViewCell
本身嵌入在UIView
中。
我嘗試在下面的UITableViewCell
class 上使用委托:
import UIKit
public protocol CustomCellDelegate: class {
func customButtonClick()
}
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var customButton: UIButton!
weak var delegate: CustomCellDelegate?
@IBAction func customButtonAction(_ sender: UIButton) {
delegate?.customButtonClick()
}
.. }
以及此處MainViewController
上的相應代碼:
import UIKit
class MainViewController: UIViewController, CustomCellDelegate {
@IBOutlet var UIViewButton: UIView!
func customButtonClick() {
self.UIViewButton.frame = CGRect (x:20, y: 300, width: 369, height: 46)
self.view.addSubview(UIViewButton)
} }
我知道CustomTableViewCell
在兩個不同的控制器( CustomTableView
, CustomTableViewController
)中,所以我認為其中一個可能存在委托問題/我可能需要為這些控制器添加另一個委托,但我不確定如何。 我已經設法更改了CustomTableViewCell
中的customButton
圖標,所以我知道它是可點擊的,但我似乎無法讓它委托或與MainViewController
通信並讓UIViewButton
出現。 對於造成的混亂和不便,我深表歉意,由於我是編碼初學者,因此我將不勝感激。 非常感謝!! ^__^
更新:
CustomTableViewController:
public protocol ContactListTableViewControllerDelegate: class {
func showSendButton() }
class CustomTableViewController: UITableViewController, CustomCellDelegate {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "likesCell", for: indexPath) as! CustomTableViewCell
cell.delegate = self }
func customButtonClick() {
delegate?.showSendButton()
} }
MainViewController
:
extension MainViewController: ContactListTableViewControllerDelegate {
func showSendButton() {
self.UISendButton.frame = CGRect (x:20, y: 300, width: 369, height: 46)
self.view.addSubview(UISendButton)
} }
我希望您的用戶界面類似於下圖。
為此,我會做以下事情
聲明一個協議,讓您知道何時顯示或隱藏“發送”按鈕。 例如
protocol ContactListTableViewControllerDelegate: class { func showSendButton() func hideSendButton() }
使第一個視圖 controller 成為表視圖的代表 controller class
您已經創建了一個協議,用於在單元格內的按鈕上拾取點擊事件。 您也可以選擇將單元格添加為參數,以便代理知道單擊了哪個單元格。
public protocol CustomCellDelegate: class { func customButtonClick(cell: CustomTableViewCell) }
在 cellForRowAtIndexPath 方法中創建每個單元格時,使表視圖 controller 成為單元格的委托
cell.delegate = self
在表視圖controller子類中實現委托方法
func customButtonClick(cell: CustomTableViewCell) { // Find the index path for the clicked cell // You should have an array for storing the indices of selected cells // Check if the index is already in the array. If yes, remove it from //the array (deselection) // If no, add the index to the array(selection) // Check the count of the array // If it is > 0, it means at least one cell is selected. Call the // delegate method // to show button delegate?.showSendButton() // Else call delegate method to hide send button delegate?.hideSendButton() }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.