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
). 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
). Next to the list of contacts, there is a button ( customButton
) that the user can click to choose which friends to send it to. 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.
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. I would like for a new UIViewButton
(in my MainViewController
) to appear when I click the customButton
.
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
.
I tried using a delegate on the UITableViewCell
class below:
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:
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. 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. 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
:
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
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() }
Make the first view controller the delegate for the table view controller class
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) }
When creating each cell in cellForRowAtIndexPath method, make the table view controller the delegate for the cell
cell.delegate = self
Implement the delegate method in the table view controller subclass
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() }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.