简体   繁体   English

如何从 UITableViewCell 按钮创建 UIView 单击单独的 UIViewController?

[英]How to create a UIView from a UITableViewCell button click on a separate UIViewController?

I'm working on an iOS app in Xcode/Swift and I'm trying to add a subview UISendViewButton in my MainViewController when a UITableViewCell button is clicked in a separate UITableViewController (which itself is embedded in a UIView ).我正在 Xcode/Swift 中开发一个 iOS 应用程序,当在单独的UITableViewController中单击UITableViewCell按钮(它本身嵌入在UIView中)时,我试图在我的MainViewController中添加一个子视图UISendViewButton Basically, the concept is that of a "send post" button like in Instagram: the user will click a paper airplane button and a separate list of friends appears ( UIView -> UITableViewController ).基本上,这个概念就像 Instagram 中的“发送帖子”按钮:用户将单击一个纸飞机按钮,然后会出现一个单独的朋友列表( UIView -> UITableViewController )。 Next to the list of contacts, there is a button ( customButton ) that the user can click to choose which friends to send it to.在联系人列表旁边,有一个按钮 ( customButton ),用户可以单击它来选择要将其发送给哪些朋友。 What I want is to have a "Send" button ( UIViewButton ) appear ONLY if the user decides to click the button ( customButton ) next to their friends' name.我想要的是只有当用户决定单击他们朋友姓名旁边的按钮( customButton )时,才会出现“发送”按钮( UIViewButton )。

I was able to make the UITableViewController appear by embedding it within a UIView and then adding that as a subview to my MainViewController , but when I click the customButton in the UITableViewCell class, nothing happens.我能够通过将UITableViewController嵌入到UIView ,然后将其作为子视图添加到我的MainViewController中来使它出现,但是当我单击UITableViewCell customButton中的 customButton 时,没有任何反应。 I would like for a new UIViewButton (in my MainViewController ) to appear when I click the customButton .我希望在单击customButton时出现一个新的UIViewButton (在我的MainViewController中)。

So basically I wanted to know how to have these two controllers communicate.所以基本上我想知道如何让这两个控制器进行通信。 The controller which houses the UITableViewCell button is the one that is provided by Xcode's library: UITableViewController -> UITableView -> UITableViewCell which is itself embedded within a UIView .包含UITableViewCell按钮的 controller 是由 Xcode 的库提供的: UITableViewController -> UITableView -> UITableViewCell本身嵌入在UIView中。

I tried using a delegate on the UITableViewCell class below:我尝试在下面的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()

    } 
  .. }

And corresponding code on the MainViewController here:以及此处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)

  } }

I know that CustomTableViewCell is within two different controllers ( CustomTableView , CustomTableViewController ), so I was thinking there may be a delegate issue within one of those/I may need to add another delegate for those controllers but I'm not sure how.我知道CustomTableViewCell在两个不同的控制器( CustomTableViewCustomTableViewController )中,所以我认为其中一个可能存在委托问题/我可能需要为这些控制器添加另一个委托,但我不确定如何。 I've managed to change the customButton icon in CustomTableViewCell so I know that it's clickable, I just can't seem to get it to delegate or communicate with the MainViewController and have the UIViewButton appear.我已经设法更改了CustomTableViewCell中的customButton图标,所以我知道它是可点击的,但我似乎无法让它委托或与MainViewController通信并让UIViewButton出现。 I'm sorry for the confusion and inconvenience, any help with this would be greatly appreciated as I'm a beginner to coding.对于造成的混乱和不便,我深表歉意,由于我是编码初学者,因此我将不胜感激。 Thanks so much!!非常感谢!! ^__^ ^__^

UPDATE:更新:

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 : MainViewController

extension MainViewController: ContactListTableViewControllerDelegate {
func showSendButton() {
    
       self.UISendButton.frame = CGRect (x:20, y: 300, width: 369, height: 46)
    self.view.addSubview(UISendButton)
} }

I hope that your UI is similar to the image below.我希望您的用户界面类似于下图。

在此处输入图像描述

For this, I would do the following为此,我会做以下事情

  1. Declare a protocol that will let you know when to show or hide the 'Send' button.声明一个协议,让您知道何时显示或隐藏“发送”按钮。 For example例如

     protocol ContactListTableViewControllerDelegate: class { func showSendButton() func hideSendButton() }
  2. Make the first view controller the delegate for the table view controller class使第一个视图 controller 成为表视图的代表 controller class

  3. You have already created a protocol for picking up tap events on the button inside the cell.您已经创建了一个协议,用于在单元格内的按钮上拾取点击事件。 Optionally you need to add the cell too as a parameter, so that the delegate knows which cell was clicked.您也可以选择将单元格添加为参数,以便代理知道单击了哪个单元格。

     public protocol CustomCellDelegate: class { func customButtonClick(cell: CustomTableViewCell) }
  4. When creating each cell in cellForRowAtIndexPath method, make the table view controller the delegate for the cell在 cellForRowAtIndexPath 方法中创建每个单元格时,使表视图 controller 成为单元格的委托

     cell.delegate = self
  5. Implement the delegate method in the table view controller subclass在表视图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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM