Swift - access UIButton inside ContainerView in another ViewController - ios

I am having a problem accessing my UIButton that's inside a ContainerView from another ViewController.
If the user taps the button a SubView should appear inside of my ViewController. I tried dragging the button in my ViewController file to create an #IBAaction func tapped() but that is not working.
It only works inside of the ContainerView.swift file. But I can not create my Subview there...
I hope you get my problem, I am grateful for every help!
import UIKit
class ContainerViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func addButtonTapped(_ sender: Any) {
print("add Button")
}

You can reach your ViewController inside your ContainerView using its parent property. So, inside your ContainerView class:
if let parentVC = self.parent as? ViewController {
parentVC.showSubView() // your method to show the sub view.
}

You can achieve this using NotificationCenter and Delegates. Here is how you can achieve it with NotificationCenter
In your ContainerViewController you can post notification on button click
#IBAction func yourBtnTap(sender : UIButton) {
NotificationCenter.default.post(name: Notification.Name("myBtnTapped"), object: nil)
}
In your CurrentViewController you have to first register to receive this notification. So in your viewDidLoad, do this:
NotificationCenter.default.addObserver(self, selector: #selector(self.myBtnTappedAction(notification:)), name: Notification.Name("myBtnTapped"), object: nil)
Now in your CurrentViewContoller create myBtnTappedAction function that will be called whenever the button is tapped.
#objc func myBtnTappedAction(notification : Notification) {
// now you can perform all your actions here. this function will be called everytime the button in the container view is tapped.
}

Related

iOS delegate is nil when trying to pass data back with Swift 5

I know this is a pretty common question but I've tried the various solutions offered here (that are not too old) and in numerous tutorials and I just can't seem to find out why it's still failing for me. Basically setting sendingViewController.delegate to self ends up being nil in sendingViewController. I understand this is very likely because the reference to the sendingViewController is being disposed of. But here is why I'm asking this again.
First, almost every tutorial and every other StackOverflow post is wiring up the mainViewController and the sendingViewController differently. I'm trying to make this work through a Navigation Controller, what one would think is the most common pattern for this.
In the app I'm building (which is more complex than the sample I'm going to show), the mainViewController calls the Settings viewController through a right navbar button. Then the user can select items from a list, which opens a controller with a searchBar and a tableView of items to select from. I need that third view controller to return the selected item from the table view to the settings screen. I'm using storyboards as well. I'm fairly new to Swift and I'm not ready to do all this "programmatically". Any way in the sending view controller, my delegate which should have been set in the calling view controller is nil and I can't invoke the protocol function in the main view controller to pass the data back.
I did a tutorial directly (not using Nav controllers) and I got that to work, but the moment I deviate away, it starts failing. I then put together a streamlined project with two view controllers: ViewController and SendingViewController. ViewController was embedded in a navigation controller and a right bar button was added to go to the SendingViewController. The SendingViewController has a single UI Button that attempts to call the protocol function and dismiss the SendingViewController. I'm not using Seque's, just a simple buttons and protocol/delegate pattern as I can.
My question is what am I missing to actually set the SendingViewController.delegate correctly?
Here's some code:
//ViewController.swift
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var showDataLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func fetchDataButton(_ sender: UIBarButtonItem) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "SendingViewController") as! SendingViewController
controller.delegate = self
print("fetching data")
present(controller, animated: true, completion: nil)
}
}
extension ViewController: SendingViewControllerDelegate {
func sendData(value: String) {
print("got Data \(value)")
self.showDataLabel.text = value
}
}
and
// SendingViewController.swift
import UIKit
protocol SendingViewControllerDelegate {
func sendData(value: String)
}
class SendingViewController: UIViewController {
var delegate: SendingViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func sendDataButton(_ sender: UIButton) {
print("attempting to send data \(self)")
print("to \(self.delegate)")
self.delegate?.sendData(value: "Hello World")
self.navigationController?.popViewController(animated: true)
}
}
Here is a screenshot of the Storyboard:
The ChildViewController does have a storyboard id name of "ChildViewController". All buttons and labels have their appropriate IBOutlet and IBAction's set up.
Help!
i copy paste your code .. its working perfect .. i make just one change
instead of pop you need to use dismiss as you are presenting from your base viewController
#IBAction func sendDataButton(_ sender: UIButton) {
print("attempting to send data \(self)")
print("to \(self.delegate)")
self.delegate?.sendData(value: "Hello World")
self.dismiss(animated: true, completion: nil)
}
here is the project link we.tl/t-NUxm9D26XN
I managed to get this working. In the receiving/parent view controller that needs the data:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let controller = segue.destination as! sendingViewController
controller.cityDelegate = self
}
Then in the sending view controller in my tableView did select row function:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let city = filtered[indexPath.row]
searchBar.resignFirstResponder()
self.navigationController?.popViewController(animated: true)
self.cityDelegate?.addCity(city)
self.dismiss(animated: true, completion: nil)
}
I don't think I should be both popping the view controller and dismissing it, but it works. Also in the view controller I did this:
private var presentingController: UIViewController?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
presentingController = presentingViewController
}
override func didMove(toParent parent: UIViewController?) {
super.didMove(toParent: parent)
if parent == nil {
}
}
I don't know if I really need this didMove() or not since it doesn't really do anything.
But some combination of all this got it working.
In my other app I'm not using a navigation bar controller and the standard delegate/protocol method works like a charm.

how to pass data to previous viewController in iOS swift?

My question is duplicate but I need not a suitable answer. Also, I have raised the same before how to callback the array of data to another viewController in iOS Swift
I have a parent viewController called CreateCardViewController and a child controller called webViewController.
In parent viewController, I have used carbonKit for showing the tab bar menu. When the tab bar menu first index is webViewController (that's a child controller).
My question is: How to send data to the parent controller from the child controller?
For example: From a child, viewController will get a list of tab bar menu items. After getting tab bar menu items, I need to send menu items to parent viewController to show tab bar.
Here is the clear picture, which I am trying to do:
you can use delegate like what #Aqua said. or use observation for this.
class ParentViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.updateParentViewController(_:)), name: Notification.Name(rawValue: "updateParentViewController"), object: nil)
}
#IBaction func updateParentViewController(_ notification: NSNotification){
if let receivedData = notification.userInfo?["data"] as? Any {
//use received data
// update your parentViewController.
}
}
}
//.............
class ChildViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func sendDataToParentViewController() {
let dataDict:[String: Any] = ["data"://what you want to send.]
NotificationCenter.default.post(name: . updateParentViewController, object: nil, userInfo: dataDict)
}
}
this works for me.
On your custom ChildViewController add propertyparentViewController and set it when you create that child view controller. Then, implement specific method on parent viewcontroller, that receives data from child view controller.
protocol ParentViewControllerProtocol {
func receiveChildData(_ child: UIViewController, data: Any)
}
class ChildViewController: UIViewController {
var parentViewController: ParentViewControllerProtocol!
func timeToSendDataToParentViewController() {
parentViewController.receiveChildData(self, data: self.data)
}
}
class ParentViewController: UIViewController, ParentViewControllerProtocol {
func receiveChildData(_ child: UIViewController, data: Any) {/*handle data*/}
func addChildViewController() {
let child = ChildViewController();
child.parentViewController = self
// do the rest of adding child to parent
}
}

Swift: Call a function in another ViewController

I have a TableViewController to handle my data and a sortFunction. What I want to do is to call the sortFunction in a SortViewController and when the Button "done" is clicked go back to my TableViewController (which should show the sorted data now)
My Question is: how can I call the sortFunction() which is included in my TableViewController in the SortViewController?
This what I have:
TableViewController:
func sortFunction(){
self.data?.sortInPlace({ $0.clicks > $1.clicks })
self.tableView.reloadData()
}
SortViewController:
When the specific sort Button is clicked:
#IBAction func sortBestClicks(sender: AnyObject) {
// how to call the function from TableViewController here??
}
When Done is Clicked:
#IBAction func doneButton(sender: AnyObject) {
self.performSegueWithIdentifier("unwindToStart", sender: self)
}
In TableViewController viewDidLoad add
NSNotificationCenter.defaultCenter().addObserver(self, selector: "methodOfReceivedNotification", name:"NotificationIdentifier", object: nil)
and add a method in TableViewController like
func methodOfReceivedNotification() {
sortFunction()
}
in SortViewController call the method with
NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
Hope this helps you.
You could also use a delegate protocol.

Update Label On Another View

I have two views with a label on one of them. On the second view, there is a button. What I want to achieve here is to be able to press the button and it updates the label on the first view. How do I do that? I can't access the IBOutlet from the second view. Is there something that I have to do to the IBOutlet to make it public etc?
You can use NSNotificationCenter for that.
First of all in your viewDidLoad method add this code in your firstViewController class:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "refreshlbl:", name: "refresh", object: nil)
which will addObserver when your app loads.
And add this helper method in same viewController.
func refreshlbl(notification: NSNotification) {
lbl.text = "Updated by second View" //Update your label here.
}
After that in your secondViewController add this code when you dismiss your view:
#IBAction func back(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: nil, userInfo: nil)
self.dismissViewControllerAnimated(true, completion: nil)
}
Now when you press back button from secondView then refreshlbl method will call from firstView.
use custom delegate method create a delegate in second view and access that view delegate function in first view. or use NSNotification or use NSUserdefaults

iOS Swift Programmatically Trigger button from previous ViewController

When BackgroundClicked is triggered go back to the previous viewcontroller and programatically click on a button on the previous viewcontroller on the load. But only if BackgroundClicked was clicked from the child view controller
for example
//this button is on child controller
#IBAction func BackgroundClicked(sender: AnyObject)
{
self.navigationController?.popToRootViewControllerAnimated(true)
// when im on the root controller click button abc
// button abc resides on the root controller
}
I only want button abc clicked on the load of the root controller only if BackgroundClicked was clicked. I'm not sure how to go about this or if it's even possible
You can send a notification when button is pressed into your ChildViewController like show in below:
Add this code in your ChildViewController when you press a button:
#IBAction func BackgroundClicked(sender: AnyObject)
{
NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: nil)
self.navigationController?.popToRootViewControllerAnimated(true)
}
In your ParentViewController add this code into your viewDidLoad method:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "refreshList:", name:"refresh", object: nil)
}
and here is your helper function:
func refreshList(notification: NSNotification){
println("parent method is called")
}
Now when ever you press back button from your ChildViewController refreshList method will call from your ParentViewController.
Check THIS sample project for more Info.
Hope this will help.
So that will go back to the root view controller, but what you want to do is you want to create a function in the previous view controller. In the code in the viewController that you are going back to run the name of the function in the viewDidLoad() method. Also whenever you click the back button you want to change the value of a variable because you don't want to run the function every time int he viewDidLoad(). This is what the code to the previous view controller should look like:
var decideInt: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
if decideInt == 0 {
// Do Nothing
} else if decideInt == 1 {
functionYouWantToRun()
decideInt = 0
}
}
func functionYouWantToRun() {
//Write the code to the function you want to run
}
Here is the code that you need to write to go back:
//this button is on child controller
#IBAction func BackgroundClicked(sender: AnyObject)
{
self.navigationController?.popToRootViewControllerAnimated(true)
var goToView: DestinationViewControllerName = DestinationViewControllerName()
goToView.decideInt = 1
}

Resources