Delegate function not being called iOS Swift - ios

Here I am trying to pass value from one class LanguageSelectionTVC to another RegistrationVC by using a Protocol.
When I try and call the method self.delegate?.setSelectedLangauges(self.languagesSpokenArray) inside LanguageSelectionTVC it doesn't call the method setSelectedLangauges inside the class Registration VC could someone please suggest where I am going wrong ?
protocol LanguageSelectionTVCProtocol {
func setSelectedLangauges(_ valueSent: [String])
}
class LanguageSelectionTVC: UITableViewController {
var delegate : LanguageSelectionTVCProtocol? = nil
func saveAndClose() {
self.delegate?.setSelectedLangauges(self.languagesSpokenArray)
dismiss()
}
}
class RegistrationVC: UIViewController,
UITableViewDelegate,
UITableViewDataSource,
LanguageSelectionTVCProtocol{
func setSelectedLangauges(_ valueSent: [String]){
self.showLanguagesSpoken(valueSent)
}
}
Moving to LanguageSelectionTVC from RegistrationVC . The below tableView method is in my RegistrationVC
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let row = indexPath.row
let section = indexPath.section
let currentCell = tableView.cellForRow(at: indexPath) as! UITableViewCell
if section == 4 && row == 0 {
// The user has clicked on languages spoken cell
self.performSegue(withIdentifier: "LanguageSelectionTVC", sender: self)
}
}

You need to access LanguageSelectionTVC from preparForSegue to set delegate of it. So override the prepare(for:sender:) in your RegistrationVC.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "LanguageSelectionTVC" {
let vc = segue.destination as! LanguageSelectionTVC
vc.delegate = self
}
}

You need to set the delegate in RegistrationVC to LanguageSelectionTVC, for example you could set it when performing a segue:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let targetVC = segue.destination as! LanguageSelectionTVC
targetVC.delegate = self
}

You need to set delegate. can you please see below ready example for pass data between two view controller.
import UIKit
protocol SenderViewControllerDelegate
{
func messageData(str1:NSString)
}
class tableViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource
{
var delegate: tableViewController?
override func viewDidLoad()
{
super.viewDidLoad()
self.delegate?.messageData(str1: “Hello world”)
}
}
receive data in below controller
import UIKit
class HomeViewController: UIViewController ,SenderViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let objtableview = tableViewController(nibName: "tableViewController",bundle: nil)
objtableview.delegate = self
self.navigationController?.pushViewController(objtableview, animated: true)
}
func messageData(str1:NSString){
print(str1)
}
}

Related

how to update label of other viewController in swift

I'm trying to set UIbutton's text in firstVC as selectedCity variable when it selected in tableViewCell in SecondVC . I cannot use segue or tabBarController. Updated photo of ui
FirstVC
import UIKit
class homeView: UIViewController{
#IBOutlet weak var selectRegion: UIButton!
}
SecondVC
import UIKit
class selectCityPage: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var selectedCity: String!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0{
selectedCity = "Almaty"
}
if indexPath.row == 1{
selectedCity = "Усть-Каменогорск"
}
dismiss(animated: true, completion: nil)
// when this view controller is dismissed, uibutton's text in next viewcontroller should equal to selectedCity
}
}
You can use delegation. These are the required steps:
Create a delegate protocol that defines the messages sent to the delegate.
Create a delegate property in the delegating class to keep track of the delegate.
Adopt and implement the delegate protocol in the delegate class.
Call the delegate from the delegating object.
SecondVC
import UIKit
//1. Create a delegate protocol
protocol CitySelectionDelegate {
func pickCity(with selectedCity:String)
}
class SelectCityPage: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var selectedCity: String!
//2. Create a delegate property in the delegating class
var delegate:CitySelectionDelegate?
//other stuff
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
if indexPath.row == 0{
selectedCity = "Almaty"
}
if indexPath.row == 1{
selectedCity = "Усть-Каменогорск"
}
4. Call the delegate from the delegating object.
delegate?.pickCity(with: selectedCity) //call your delegate method
//dismiss(animated: true, completion: nil) when you dismiss is up to you
}
}
HomeViewController
UPDATE: Since you have another VC embedded inside a UINavigationController, both the Home and Select Region Page MUST conform to the delegate and you have to set the delegate inside prepareForSegue method.
// 3. Adopt and implement the delegate protocol
class HomeViewController: UIViewController, CitySelectionDelegate{
#IBOutlet weak var selectRegion: UIButton!
func pickCity(with selectedCity: String) {
self.selectRegion.text = selectedCity
}
/*please pay attention. In this case you must reference the
navigation controller first and the your can get the right
instance of the delegate variable inside your firstVC (I called
firstVC but in your case is Select Region Page*/
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// you MUST set the right identifier
if segue.identifier == "showFirst" {
if let navController = segue.destination as? UINavigationController {
if let firstVC = navController.topViewController as? FirstViewController{
firstVC.delegate = self
}
}
}
}
}
since you have another VC (embedded in a navigation controller), also this one must conform to the delegate like so:
class FirstViewController: UIViewController, CitySelectionDelegate {
var delegate: CitySelectionDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
func pickCity(with selectedCity: String){
// here you simply call the delegate method again and you dismiss the navigation controller
self.delegate?.pickCity(with: selectedCity)
self.navigationController?.dismiss(animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showSecond" {
if let controller = segue.destination as? SelectCityPage {
controller.delegate = self
}
}
}
you can use delegate or block or notification when secVC action to change the firstVC value you needed.
Or you set a property in secVC and pass the value in firstVC you want to change, So you can change value passed from firstVC in secVC.

Passing image between two VC using Modal Segue

I'm trying to pass data, using delegate between two VC, but I can't get why it's not working.
My first VC
class ViewController: UIViewController {
#IBOutlet weak var profileImage: UIImageView!
func downloadPhoto() {
self.performSegue(withIdentifier: "showPhotoFromInternet", sender: nil)
}
}
extension ViewController: IPresentaionPhotoFormInternetDelegate {
func setNewImage(imageToShow: UIImage) {
profileImage.image = imageToShow
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? PresentPhotoFromInternetViewController {
destination.delegate = self
}
}
My second VC
class PresentPhotoFromInternetViewController: UIViewController {
var imageToSend: UIImage?
var delegate: IPresentaionPhotoFormInternetDelegate?
#IBOutlet weak var photoCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
photoCollectionView.allowsMultipleSelection = false
}
#IBAction func sendPhotoToPreviousController(_ sender: Any) {
delegate?.setNewImage(imageToShow: iamgeToSend!)
performSegue(withIdentifier: "sendPhoto", sender: nil)
}
extension PresentPhotoFromInternetViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! photoCollectionViewCell
print("Cell is selected")
iamgeToSend = cell.photoImageView.image
cell.selectedView.backgroundColor = UIColor.green
}
protocol IPresentaionPhotoFormInternetDelegate {
func setNewImage(imageToShow: UIImage)
}
I use present modaly segue for from the first VC to the second, and show form the second to the first
When I perform segue from the second VC there is no updates in my first one, although it passes all breakpoints.
The problem is instead of popping controller you are loading completely new controller in your button action
#IBAction func sendPhotoToPreviousController(_ sender: Any) {
delegate?.setNewImage(imageToShow: iamgeToSend!)
//Comment or remove the performSegue
//performSegue(withIdentifier: "sendPhoto", sender: nil)
//Now simply pop this controller
_ = self.navigationController?.popViewController(animated: true)
// If you are presenting this controller then you need to dismiss
self.dismiss(animated: true)
}
Note: If you are segue is kind of Push then use popViewController or is it kind of Modal than you need to use dismiss.

How to pass data between container view and main view controller properly

I'm dealing with following problem: On main VC I have a TableView, and on container view controller I have a textField. I want to add every text, I'm typing in container automatically appears as a new row on tableView in main VC
By now I'm using segue to send data from main VC to container. But what should I implement to do the same in a reverse order? I though of implementing delegate of main VC in my container view, but I have no idea how to do that properly. Or maybe there is exist more common solution.
Anyway, here is my code:
class MessageViewController: UIViewController {
var currentUser: User!
var containerViewController: InputTextViewController?
#IBOutlet weak var enterMessageView: UIView!
#IBOutlet weak var messageTableView: UITableView!
}
extension MessageViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "moveToInputText" {
let connectContainerViewController = segue.destination as? InputTextViewController
containerViewController = connectContainerViewController
containerViewController?.userSendMessageTo = currentUser
}
}
}
extension MessageViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return currentUser.mesaageHistory.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "messageCell") as! ChatMessageTableViewCell
let data = currentUser.mesaageHistory[indexPath.row]
cell.messageLabel.text = data.messageText
return cell
}
}
class InputTextViewController: UIViewController {
#IBOutlet weak var messageTextField: UITextField!
var userSendMessageTo: User!
weak var delegate = MessageViewController()
#IBAction func sendMessge(_ sender: Any) {
handleSend()
userSendMessageTo.mesaageHistory.append(message)
print(userSendMessageTo.mesaageHistory[0].messageText)
let row = userSendMessageTo.mesaageHistory.count - 1
let insertIndexPath = IndexPath(item: row, section: 0)
print(userSendMessageTo.mesaageHistory.count)
delegate?.messageTableView.beginUpdates()
delegate?.messageTableView.insertRows(at: [insertIndexPath], with: UITableViewRowAnimation.automatic)
delegate?.messageTableView.endUpdates()
}
}
Here's how to use the 'delegate' pattern properly
Protocol declaration & delegate member
protocol InputTextViewControllerDelegate: class {
func someFunc()
func anotherFunc()
}
class InputTextViewController: UIViewController {
weak var delegate: InputTextViewControllerDelegate?
}
Protocol implementation & setting the delegate property
extension MessageViewController, InputTextViewControllerDelegate {
// MARK: InputTextViewControllerDelegate
//
func someFunc()
{
}
func anotherFunc()
{
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "moveToInputText" {
let connectContainerViewController = segue.destination as? InputTextViewController
containerViewController = connectContainerViewController
containerViewController
}
}
}

Passing data between segue with Swift 2.0

I seem to be following tutorials to the tee yet I can't get my data to pass to my second view controller without being nil.
I am new to Swift so I'm probably also not using optionals correctly. Here is how I have my code set up.
import UIKit
class DetailsViewController: UIViewController {
var book : PLBook?
override func viewDidLoad() {
super.viewDidLoad()
//View Did Load
print(self.book)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}
}
Then this is how I am presenting the second view controller and preparing for segue.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("toDetailVC", sender:self)
}
//MARK: Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "toDetailVC"){
//prepare for segue to the details view controller
if let detailsVC = sender?.destinationViewController as? DetailsViewController {
let indexPath = self.tableView.indexPathForSelectedRow
detailsVC.book = self.books[indexPath!.row]
}
}
}
In the second view controller, the book var is always nil. Any ideas how I can get the data to pass?
Try This:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "toDetailVC") {
//prepare for segue to the details view controller
let detailsVC = segue.destinationViewController as! DetailsViewController
let indexPath = self.tableView.indexPathForSelectedRow
detailsVC.book = self.books[indexPath!.row]
}
You should be using segue.destinationViewController, not sender?.destinationViewController.

On passing prepareForSegue , how do I pass my model object to my VC?

My prepareForSegue code looks like this :
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowVerse" {
let pageDetailViewController = segue.destinationViewController as! PageViewController
// Get the cell that generated this segue.
if let selectedPageCell = sender as? PageTableViewCell {
let indexPath = tableView.indexPathForCell(selectedPageCell)!
let selectedPage = pages[indexPath.row]
pageDetailViewController.page = selectedPage
}
}
}
However, this line :
let selectedPageCell = sender as? PageTableViewCell
returns nil, and thus so, no information is passed to the ViewController.
How can I determine why this returns nil? And ultimately, how can I successfully assign selectedPageCell to my sender as PageTableViewCell.
My TableViewCell.swift :
import UIKit
class PageTableViewCell: UITableViewCell {
// MARK: Properties
#IBOutlet weak var chapter: UILabel!
#IBOutlet weak var verse: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
I'm basing my app off of this Apple Tutorial
all you really need here is the index of the selected row - so you could have that as a class-level variable, and assign it in didSelectRowAtIndexPath
Here's an outline of the code - with all sorts of assumptions about your class names
class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
var selectedIndexPathRow = -1
... more code ...
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
selectedIndexPathRow = indexPath.row
}
... more code ...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "ShowVerse"
{
if selectedIndexPathRow != -1 // default value to show that something has been selected
{
pageDetailViewController.page = pages[selectedIndexPathRow]
}
}
}
... more code ...
}
Hope this will help you .Replace your code with below code .
you need to replace (sender: AnyObject?) to (sender: AnyObject!) and (sender as? PageTableViewCell) to (sender as PageTableViewCell)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "ShowVerse" {
let pageDetailViewController = segue.destinationViewController as? PageViewController
// Get the cell that generated this segue.
if let selectedPageCell = sender as? PageTableViewCell {
let indexPath = tableView.indexPathForCell(selectedPageCell)!
let selectedPage = pages[indexPath.row]
pageDetailViewController.page = selectedPage
}
}}
Happy coding .Provide your feedback.

Resources