Pass data between more than two view controllers using delegation - ios

First of all, let me say that I have been using the delegation pattern to pass data back and forward between view controllers for quite some time without any issues but now I have a need to pass data between four (4) view controllers, ViewController1, ViewController2, ViewController3 and CategoriesViewController.
In the code below I'm showing the communication between ViewController1 and the CategoriesViewController, the communication between ViewController2 and CategoriesViewController will be identical as well.
My issue or what I don't quite like is the fact that I don't need to pass any data between ViewController3 and the CategoriesViewController so, my debate is how can I handle the fact that CategoriesViewController is expecting variable categoryTracker which I will not be passing when connecting ViewController3 with the CategoriesViewController.
Is there a way to make the variable categoryTracker in CategoriesViewController optional, here I'm talking in a sense that I wouldn't have to pass it when connecting from ViewController3 and NOT a Swift optional?
How would you guys do such communication in a way that CategoriesViewController becomes more modular/reusable?
ViewController1 and ViewController2
class ViewController1: UIViewController, CategoryDelegate{
#IBAction func showCategories(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "categoriesViewControllerID") as? CategoriesViewController
vc?.delegate = self
vc?.categoryTracker = self.categorySelection
self.present(vc!, animated: true, completion: nil)
}
}
CategoriesViewController
protocol CategoryDelegate {
func selectedCategory(category: Category)
}
class CategoriesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var delegate: CategoryDelegate?
var categoryTracker:Category?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate?.selectedCategory(category: categoryTracker!)
}
}
Just for reference, here is how ViewController3 would look like.
class ViewController3: UIViewController{
#IBAction func showCategories(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "categoriesViewControllerID") as? CategoriesViewController
self.present(vc!, animated: true, completion: nil)
}
}
Thanks

Related

App does not navigate to destination VC programmatically in swift

I am trying to navigate from ConversationsListVC (source) to ConversationVC (destination) programmatically by instantiating the source VC (self) as the root navigation controller VC and then instantiating the destination VC so that I can push navigate.
I get the print statements onto the console (see below) but the app does not navigate to the destination VC, the app doesn't crash or throw any error. what am I missing here?
debug 1
debug 2
function:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("debug 1")
var window: UIWindow!
guard let convoVC = storyboard?.instantiateViewController(withIdentifier: "ConversationVC") as? ConversationVC else { return }
window?.rootViewController = UINavigationController(rootViewController: self)
navigationController?.pushViewController(convoVC, animated: true)
print("debug 2")
}//end func
try this one maybe the window instance is not correctly.
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "ConversationVC") as! ConversationVC
navigationController?.pushViewController(newViewController, animated: true)
and make sure your viewcontroller is assign with the Identifier
You can't just randomly create a new UINavigationController and push the destination onto it. You need to push the destination VC onto the UINavigationController in which self is currently embedded in.
If self is embedded in a UINavigationController already, do:
self.navigationController?.pushViewController(convoVC, animated: true)
If the source VC is not embedded in a UINavigationController, you need to do that in the storyboard.
Since you are using storyboards, you can also consider adding a push segue between the source and destination, and performing that segue:
self.performSegue(withIdentifier: "someIdentifier", sender: self)

How to pass textfield data from third viewcontroller to first viewcontroller

I want to send the text which is in textfield in ViewControllerC to another textfield which is in ViewControllerA
By using delegate am trying to pass the text from ViewControllerC to ViewControllerA.
i cant get the logic what to write here delegate?.userDidEnterInformation() in ViewControllerC
could any one help me regarding this
ViewControllerC
protocol DataEnteredInDestinationDelegate: class {
func userDidEnterInformation(info: String)
}
class DestinationSearchViewController: MirroringViewController {
var delegate: DataEnteredInDestinationDelegate?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell: UITableViewCell? = tableView.cellForRow(at: indexPath)
componetsTextField.text = cell?.textLabel?.text
delegate?.userDidEnterInformation()
self.navigationController?.popToRootViewController(animated: true)
}
}
ViewControllerA
class HomeViewController: MirroringViewController, DataEnteredInDestinationDelegate
{
func userDidEnterInformation(info: String){
locationView.destination.text = info
}
}
Firstly you have to always mark delegate as weak e.g.:
weak var delegate: DataEnteredInDestinationDelegate?
and then you need to connect delegate like this:
let vcA = ViewControllerA()
let vcC = ViewControllerC()
vcC.delegate = vcA // Connect delegate
and then your delegate method in ViewControllerC will work after invoking this code:
delegate?.userDidEnterInformation(textString)
Here NotificationCentre can be a good approach instead of delegates. Make Viewcontroller A an observer to receive text information as below.
Write this code in viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(userDidEnterInformation(notification:)), name: NSNotification.Name.init(rawValue: "UserDidEnterInformation"), object: nil)
and write this anywhere in class Viewcontroller A
func userDidEnterInformation(notification: Notification) {
if let textInfo = notification.userInfo?["textInfo"] {
textField.text = textInfo
}
}
In Viewcontroller C post the notification with textInfo by writing below code
NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "UserDidEnterInformation"), object: nil, userInfo: ["textInfo": textField.text])
delegate?.userDidEnterInformation(cell!.textLabel!.text)
Also, you should set the delegate of ViewControllerC.
viewControllerC.delegate = viewControllerA
Consider the following example:-
let aVCobjA = UIViewController()
let aVCobjB = UIViewController()
let aVCobjC = UIViewController()
var aNavigation = UINavigationController()
func pushVC() {
aNavigation.pushViewController(aVCobjA, animated: true)
aNavigation.pushViewController(aVCobjB, animated: true)
aNavigation.pushViewController(aVCobjC, animated: true)
//Here you will get array of ViewControllers in stack of Navigationcontroller
print(aNavigation.viewControllers)
//To pass data from Viewcontroller C to ViewController A
self.passData()
}
// To pass data access stack of Navigation Controller as navigation controller provides a property viewControllers which gives you access of all view controllers that are pushed.
func passData() {
let aVCObj3 = aNavigation.viewControllers.last
let aVCObj1 = aNavigation.viewControllers[0]
//Now you have access to both view controller pass whatever data you want to pass
}

Swift: pass string value from VC2 to VC1 on dismissViewControllerAnimated

first time asking after learning many useful things here!
I have a VC1 with a button and label.
The button is coded to present VC2 programmatically (without segue in IB).
VC2 has a tableview with the cells containing string values.
When I click on the cell in VC2, I am trying to get the string value of the selected cell and pass it back to the label.text in VC1.
First ViewController code:
class VC1: UIViewController {
... ...
#IBOutlet weak var LabelText: UILabel!
var passedString = "Example"
override func viewWillAppear(animated: Bool) {
LabelText.text = "\(passedString)"
}
#IBAction func chooseLabelTextBtnPressed(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("VC2") as! VC2
self.presentViewController(vc, animated: true, completion: nil)
}
SecondViewController code:
class VC2: UIViewController, UITableViewDelegate, UITableViewDataSource {
... ...
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow;
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! VC2_tableViewCell!;
let valueToPass = currentCell.IBOutletLbl.text
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("VC1") as! VC1
viewController.passedString = valueToPass!
//self.presentViewController(viewController, animated: true , completion: nil)
self.dismissViewControllerAnimated(true, completion: nil)
}
I hoped func viewWillAppear() in VC1 would update the String value of the label when VC2 is dismissed, but it doesn't.
I cannot use presentViewController from VC2 to VC1, because it might open again the VC1 instead of going back, and then other variables in VC1 would be inaccessible.
Help me! Thanks!
You should pass a delegate from VC1 to VC2 and then just call a delegate method for the update.
Send a reference here to VC2.
#IBAction func chooseLabelTextBtnPressed(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("VC2") as! VC2
vc.delegate = self
self.presentViewController(vc, animated: true, completion: nil)
}
And before calling self.dismissViewControllerAnimated(true, completion: nil) in VC2 just call delegate.someMethod(someValue)
Also make sure your delegate is a weak reference.
When you do this:
let viewController = storyboard.instantiateViewControllerWithIdentifier("VC1") as! VC1
It creates a new instance of VC1 and doesn't actually reference the VC1 that presented VC2.
Instead you should add delegation between VC1 & VC2 to pass data around.

presentViewController from TableViewCell

I have a TableViewController, TableViewCell and a ViewController. I have a button in the TableViewCell and I want to present ViewController with presentViewController (but ViewController doesn't have a view on storyboard). I tried using:
#IBAction func playVideo(sender: AnyObject) {
let vc = ViewController()
self.presentViewController(vc, animated: true, completion: nil)
}
Error: Value of type TableViewCell has no member presentViewController
Then, I tried
self.window?.rootViewController!.presentViewController(vc, animated: true, completion: nil)
Error: Warning: Attempt to present whose view is not in the window hierarchy!
What am I doing wrong? What should I do in order to presentViewController from TableViewCell? Also how can I pass data to the new presenting VC from TableViewCell?
Update:
protocol TableViewCellDelegate
{
buttonDidClicked(result: Int)
}
class TableViewCell: UITableViewCell {
#IBAction func play(sender: AnyObject) {
if let id = self.item?["id"].int {
self.delegate?.buttonDidClicked(id)
}
}
}
----------------------------------------
// in TableViewController
var delegate: TableViewCellDelegate?
func buttonDidClicked(result: Int) {
let vc = ViewController()
self.presentViewController(vc, animated: true, completion: nil)
}
I receive error: Presenting view controllers on detached view controllers is discouraged
(Please note that I have a chain of NavBar & TabBar behind TableView.)
I also tried
self.parentViewController!.presentViewController(vc, animated: true, completion: nil)
Same Error.
Also tried,
self.view.window?.rootViewController?.presentViewController(vc, animated: true, completion: nil)
Same Error
It seems like you've already got the idea that to present a view controller, you need a view controller. So here's what you'll need to do:
Create a protocol that will notify the cell's controller that the button was pressed.
Create a property in your cell that holds a reference to the delegate that implements your protocol.
Call the protocol method on your delegate inside of the button action.
Implement the protocol method in your view controller.
When configuring your cell, pass the view controller to the cell as the delegate.
Here's some code:
// 1.
protocol PlayVideoCellProtocol {
func playVideoButtonDidSelect()
}
class TableViewCell {
// ...
// 2.
var delegate: PlayVideoCellProtocol!
// 3.
#IBAction func playVideo(sender: AnyObject) {
self.delegate.playVideoButtonDidSelect()
}
// ...
}
class TableViewController: SuperClass, PlayVideoCellProtocol {
// ...
// 4.
func playVideoButtonDidSelect() {
let viewController = ViewController() // Or however you want to create it.
self.presentViewController(viewController, animated: true, completion: nil)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath: NSIndexPath) -> UITableViewCell {
//... Your cell configuration
// 5.
cell.delegate = self
//...
}
//...
}
You should use protocol to pass the action back to tableViewController
1) Create a protocol in your cell class
2) Make the button action call your protocol func
3) Link your cell's protocol in tableViewController by cell.delegate = self
4) Implement the cell's protocol and add your code there
let vc = ViewController()
self.presentViewController(vc, animated: true, completion: nil)
I had the same problem and found this code somewhere on stackoverflow, but I can't remember where so it's not my code but i'll present it here.
This is what I use when I want to display a view controller from anywhere, it gives some notice that keyWindow is disabled but it works fine.
extension UIApplication
{
class func topViewController(_ base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController?
{
if let nav = base as? UINavigationController
{
let top = topViewController(nav.visibleViewController)
return top
}
if let tab = base as? UITabBarController
{
if let selected = tab.selectedViewController
{
let top = topViewController(selected)
return top
}
}
if let presented = base?.presentedViewController
{
let top = topViewController(presented)
return top
}
return base
}
}
And you can use it anywhere, in my case I used:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let vc = storyboard.instantiateViewController(withIdentifier: "WeekViewController")
UIApplication.topViewController()?.navigationController?.show(vc, sender: nil)
}
So self.presentViewController is a method from the ViewController.
The reason you are getting this error is because the "self" you are referring is the tableViewCell. And tableViewCell doesn't have method of presentViewController.
I think there are some options you can use:
1.add a delegate and protocol in the cell, when you click on the button, the IBAction will call
self.delegate?.didClickButton()
Then in your tableVC, you just need to implement this method and call self.presentViewController
2.use a storyboard and a segue
In the storyboard, drag from your button to the VC you want to go.

Destroy previous viewController when presenting a new one

I have an application with sideBarMenu which has five sections. In my storyboard I've created start MainViewController embedded in NavigationController. Then I've created same five VC's and again embed them in NavigationController. So I have 6 VC's, each of which is wrapped in its own NavigationController.
Each VC implements sideBarDelegate with function that has an index of selected menu.
Here is the code:
// SideBarTableViewController
protocol SideBarTableViewControllerDelegate {
func SideBarControlDidSelectRow(indexPath: NSIndexPath)
}
class SideBarTableViewController: UITableViewController {
// ...
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
println(indexPath.row)
delegate?.SideBarControlDidSelectRow(indexPath)
}
}
// SideBar class ------------------------------------------------
#objc protocol SideBarDelegate {
func SideBarDidSelectButtonAtIndex(index: Int)
optional func SideBarWillClose()
optional func SideBarWillOpen()
}
class SideBar: NSObject, SideBarTableViewControllerDelegate {
// ...
func SideBarControlDidSelectRow(indexPath: NSIndexPath) {
println(indexPath.row)
delegate!.SideBarDidSelectButtonAtIndex(indexPath.row)
}
}
Then in this function I want to present needed ViewController:
func SideBarDidSelectButtonAtIndex(index: Int) {
presentViewControllerByIndex(index)
}
func presentViewControllerByIndex(index: Int) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
switch index {
case 0:
var viewController = storyBoard.instantiateViewControllerWithIdentifier("SearchVC") as! SearchViewController
viewController.delegate = self
self.presentViewController(viewController, animated: false, completion: nil)
break
// the same code here for other 4 VC's with their StoryboardId's
default: break
}
}
Then in presented view controller if user select another section in menu should I destroy this VC before presenting selected ViewController? Because when I run my application and started switching between controllers the application memory in xCode increases (screenshot).
When I saw that I've started googling and found this answer. So I have created a protocol in my first menu VC just for test.
protocol myProtocol {
func dissmissAndPresend(index: Int) -> Void
}
And when I need to present new VC I just call:
self.delegate?.dissmissAndPresend(index)
Then in my MainVC I implement this protocol and trying to dismiss previous VC and present the new one:
class MainViewController: UIViewController, SideBarDelegate, myProtocol {
func dissmissAndPresend(index: Int) {
self.dismissViewControllerAnimated(false, completion: {
self.presentViewControllerByIndex(index)
})
}
Now when I launch my program and started to click on the first menu item it opens new VC but my memory increases as before. And also the navigationController for presented view has gone but in storyboard presented VC is embedded in navigationController.
What I am doing wrong? Can someone help me?
UPDATE::-------------------------------
Here is what I've tried:
class MainViewController: UIViewController, SideBarDelegate, searchVCProtocol {
var searchVC: SearchViewController!
var searchNavController: ShadowUINavigationController!
override func viewDidLoad() {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
searchVC = mainStoryboard.instantiateViewControllerWithIdentifier("SearchVC") as! SearchViewController
searchVC.delegate = self
searchNavController = ShadowUINavigationController(rootViewController: searchVC)
}
func SideBarDidSelectButtonAtIndex(index: Int) {
switch index {
case 0:
println("asda")
self.presentViewController(searchNavController, animated: false, completion: nil)
break
}
}
Then it takes the user to SearchViewController and what if the user open menu again and click on SearchViewController again. How can I dismiss it and reopen. Here is what I've tried but it doesn't work:
In SeachViewController I've created a protocol:
protocol searchVCProtocol {
func dismissAndPresent(index: Int) -> Void
}
Then I've added a variable for the delegate:
var delegate: searchVCProtocol?
Then when user selected menu item it fires this event:
func SideBarDidSelectButtonAtIndex(index: Int) {
delegate?.dismissAndPresent(index)
}
And in MainViewController I've implemented this protocol and created dismissAndPresent method but I don't know how to restart presented viewController.
class MainViewController: UIViewController, SideBarDelegate, searchVCProtocol {
func dismissAndPresent(index: Int) {
// code to restart this VC
}
}
What should I code for restarting presented VC?
You are calling instantiateViewControllerWithIdentifier every time, this creates ("instantiates") every time a new ViewController. Better would be to do this only once, keep a reference to it and then re-use it on demand.
Here how you can keep a reference. First you create a new variable on class level:
class MyClass {
var viewController: SearchViewController!
Then, in viewDidLoad, you instantiate the view controller and assign it to the variable.
func viewDidLoad() {
viewController = storyBoard.instantiateViewControllerWithIdentifier("SearchVC") as! SearchViewController
}
viewDidLoad is called only once so it will not happen anymore that you are creating multiple instances.
Then you can reuse this viewcontroller again and again:
func presentViewControllerByIndex(index: Int) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
switch index {
case 0:
// Here you can reuse the formerly created viewcontroller
viewController.delegate = self
self.presentViewController(viewController, animated: false, completion: nil)
break
// the same code here for other 4 VC's with their StoryboardId's
default: break
}
}

Resources