Hello learning swift and am stuck with calling a method through delegate. Checked multiple answers with similar issues and have tried the solutions but have not been able to successfully apply them to my own situation however I am close.
I have a delegator class named ViewController that holds a variable I would like to change. I have another view called MoodScroll which serves as the delegate. Moodscroll has a button being used to change the value for the variable in ViewController.
ViewController :
class ViewController: UIViewController, AVAudioPlayerDelegate, MoodScrollDelegate {
var alarmSoundType: String?
func acceptData(data: String?) {
alarmSoundType = "\(data)"
print(data)
}
}
MoodScroll :
protocol MoodScrollDelegate {
func acceptData(data: String?)
}
import UIKit
class MoodScroll: UIViewController {
#IBAction func WTF(sender: AnyObject) {
self.delegate?.acceptData("hello")
print("function called")
}
}
The IBAction calls fine as it prints "function called" in the console however it doesn't pass the value to ViewController as alarmSoundType remains nil and also the print command is not called in ViewController as well.
It seems you still have some confusion about delegation : if ViewController conforms to MoodScrollDelegate protocol, then your ViewController object will be the delegate, not the MoodScroll object.
Where do you set the delegate property of your MoodScroll object ?
If this object is created programmatically from your ViewController object, you should set it after initialization :
myMoodScrollObject.delegate = self
Is the object is created using Interface Builder, you can either use an outlet variable for delegate, or set it in prepareForSegue:sender of your ViewController class :
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let scroll = segue.destinationViewController as? MoodScroll{
scroll.delegate = self
}
}
One picky note: the way you have described your problem, it's actually ViewController what you should call the delegate of MoodScroll. Most likely you're probably forgetting to set the delegate property of MoodScroll.
I don't know how these two view controllers relate to each other in your code, but very often you would set the delegate property in the prepareForSegue method of ViewController, for example:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SegueToMoodScroll" {
let moodScrollController = segue.destinationViewController as! MoodScroll
moodScrollController.delegate = self
}
}
Related
I'm trying to send data from one ViewController to another with delegate, but can't seem to get the right instance
I've tried setting the delegate at different places within the receiving ViewController including ViewDidLoad, but the delegate in the sending ViewController is always nil.
From what I've learned, it's an average problem everybody seems to go through, and I've read quite a number of samples, tried them, but to no avail. I don't know if I'm leaving something out or not. Please shed some light if you will.
Below is what I ended up with.
The sending ViewController:
protocol CreateChatDelegate: class{
func appendChatData(_ sender: CreateChatViewController)
}
class CreateChatViewController: UIViewController {
weak var delegate: CreateChatDelegate!
#IBAction func createChat(_ sender: AnyObject) {
delegate?.appendChatData(self)
if delegate == nil {
print("delegate unsuccessful")
} else {
print("delegate successful")
}
}
The receiving ViewController:
class ViewController: UIViewController{
var createChatViewController: CreateChatViewController!
override func viewDidLoad() {
super.viewDidLoad()
...
}
}
extension ViewController: CreateChatDelegate {
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// get the reference to the ViewController
self.createChatViewController = segue.destination as? CreateChatViewController
// set it as delegate
self.createChatViewController?.delegate = self
print("ViewController: delegate successful")
}
}
func appendChatData(_ sender: CreateChatViewController) {
print("ViewController: CreateChatDelegate called")
}
}
this code outputs "delegate unsuccessful", because delegate is always nil
The method you are using is incorrect. You should use the new one:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
....
}
Notice the override keyword? if you don't see this when you are writing the viewController methods, It means that you are NOT calling the original method and misspelled the function signature.
NOTE: if you are targeting older iOS and using older Xcode, the method name may be different, you should write the name of the method and let the AutoComplete help you with the correct one.
To successsfuly configure segue you need to make sure that
1- Navigation is triggered by
self.performSegue(withIdentifier:"segue",sender:nil)
2- Force-unwrap
self.createChatViewController = segue.destination as! CreateChatViewController
as as? cast may fail silently for some reason
First make sure that prepareForSegue() method is called. Then make sure that it is called for the CreateChatViewController i.e.
if let destination = segue.destination as? CreateChatViewController {
//Do all the stuff there e.g.
destination.delegate = self
}
If your prepareForSegue() method is not called then set the action properly so it will fire the prepareForSegue() method then you will get the delegate value in the CreateChatViewController.
I have gone through all the other posts about this topic but they don't seem to help me.
I have a UITabBarController that is launching two tabs. I want to pass data collected in tab1 to the UITabBar ViewController. I am trying to use delegete protocol for this but I am having trouble setting the delegate variable in the sending ViewController. The prepare for segue never gets called. I cannot even cycle through the viewcontrollers of the tabs inside the ViewDidLoad() of the Tabbar controller as they are not created yet and so nil.
I have used delegates before and it seems rather straightforward. Does it matter that I am using it in a Tabbar?
When I run the code the viewDidLoad() in TabBarViewController is called but not the preparefor segue.
The IBAction donePressed in the MeViewController is called but the delegate is not called as its not set.
Here is the code --
protocol DetailsDelegate: class {
func myDetailsGathered( myDetails: MyDetails )
}
/// RECEIVING VIEW CONTROLLER
class TabBarViewController: UITabBarController, DetailsDelegate
{
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
print("prepare for segue called\n");
if let destinationViewController = segue.destination as? MeViewController
{
destinationViewController.delegate = self
}
}
override func viewDidLoad()
{
print("ViewDidLoad Called \n")
}
func myDetailsGathered(myDetails: MyDetails)
{
self.myDetails = myDetails
print("My details gathered \n")
}
}
---------------
/// SENDING VIEW CONTROLLER
class MeViewController: UIViewController
{
weak var delegate: DetailsDelegate?
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}
// I have UIButton in the view and this is invoked when its pressed.
#IBAction func donePressed(_ sender: Any)
{
var infoToPass = MyDetails()
print("looks like we are done")
delegate?.myDetailsGathered(infoToPass: myDetails)
}
}
prepareForSegue is called when you perform a segue. Which you don´t do and that´s why it does not get called.
A segue defines a transition between two view controllers in your
app’s storyboard file.
You should use a singleton class to store variables and access them between different controllers. You declare one like this:
class Singleton {
static let sharedInstance = Singleton()
var name = ""
}
Assign to Singleton:
Singleton.sharedInstance.name = "Some name"
To read from it from whatever controller:
let name = Singleton.sharedInstance.name
First of all, why do you want your tabbarController to receive some info/data though?
The prepare for segue never gets called.
prepareForSegue method will be invoked right after the performSegue. So where's your performSegue method? Or are you sure that that kind of segue going to MeViewController is being performed?
One more option you have is to use NotificationCenter.
I have a button on 2nd viewController, after pressing that button, I would like to dismiss the 2nd viewController and go back to the 1st view controller and immediately call a function that coded inside 1st ViewController swift file.
May I know how can I do that? By segue?
There are many way to do this one of the best way is using protocol and delegate.
You can create one protocol and extend that protocol in your ViewController1. Now create the delegate of protocol in ViewController2 and pass reference of that delegate in the ViewController1's prepareForSegue method.
First create one protocol like this
protocol PassdataDelegate {
func passData()
}
Now extend this protocol in ViewController1 like this and pass the reference of delegate in prepareForSegue method
class ViewController1 : UIViewController, PassdataDelegate {
func passData() {
//Here call your function
self.callMyFunction()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "SegueIdentifier") {
let destVC = segue.destinationViewController as! ViewController2
destVC.delegate = self
}
}
}
Now create the delegate object of protocolin ViewController2 like this
class ViewController2 : UIViewController {
var delegate: PassdataDelegate?
//Now call the method of this delegate in Button action
#IBAction func buttonClick(sender: UIButton) {
self.delegate.passData()
//Now dismiss the controller
}
}
Note: - Here i am passing stringbut you can pass any type of object that you have declare in your delegate method.
You can refer unwind segue.
class ViewController1 {
#IBAction func doSomeStuffAfterReload(segue: UIStoryboardSegue) {
// do whatever you need to do here.
}
}
On storyboard, from ViewController2 Ctrl+Drag from the button to the exit outlet and select doSomeStuffAfterReload.
You can see it in action here: https://spin.atomicobject.com/2014/10/25/ios-unwind-segues/
Happy coding^^
So I created a delegate in the following way:
class MessengerTableViewController: UITableViewController, MessengerTableViewControllerDelegate, UITableViewDataSource, UITextFieldDelegate {
//..Insert Various functions Here..
func messengerScrollToBottom() {
self.tableView.setContentOffset(CGPointMake(0, CGFLOAT_MAX), animated: true)
}
}
The protocol is :
protocol MessengerTableViewControllerDelegate {
func messengerScrollToBottom()
}
And the class using messengerScrollToBottom() is the superView :
class ContainerViewController: UIViewController, UITextFieldDelegate {
#IBOutlet var containerViewController : UIView
init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
self.msgDelegate = MessengerTableViewController(coder: nil)
}
func keyboardWillShow(aNotification: NSNotification) {
self.msgDelegate!.messengerScrollToBottom()
}
}
This will not work because within the ContainerViewController init() function, msgDelegate is not assigned to the proper MessageTableViewController. It does not point to the MessengerTableViewController that is already created by the storyboard in memory. Therefore the code executes fine, however I see no change in the tableView onscreen. My question is, how can I modify this code to have it occur as intended?
Here is an image of my storyboard:
http://imgur.com/z09dapF
Help would be appreciated!
EDIT:
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController ].
if segue?.identifier == "tableView" {
let child = segue!.destinationViewController as? MessengerTableViewController
self.msgDelegate = child
}
// Pass the selected object to the new view controller.
}
I implemented the code above and now it works..except with one catch. The tableView is blank upon executing messengerScrollToBottom() . Any know why?
Second edit: Turns out CGFLOAT_MAX was the problem, thanks.
You need to set the delegate when you segue to the MessengerTableViewController. Since you're using a segue, you should do that in prepareForSegue. Get a reference to the MessengerTableViewController with segue.destinationViewController.
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
let messengerTVC: MessengerTableViewController = segue.destinationViewController as ViewController
self.msgDelegate = messengerTVC;
}
Maybe try declaring a MessageTableViewController property in your header file, and then bind it to the MessageTableViewController in your storyboard? Then in the init() you could assign that property to be the object's delegate?
I have 5 instances of CalculatorViewController in my storyboard, but only one with identifier "secondOperationViewController". 3 of them connected with my initial ViewController named ContainerViewController using embed segues.
I want ViewController with identifier "secondOperationViewController" to call method from my ContainerViewController, but I cant correctly instantiate it, so delegate method not being called. Here is part of my code:
override func viewDidLoad() {
super.viewDidLoad()
secondOperationViewController = storyboard.instantiateViewControllerWithIdentifier("secondOperationViewController") as? CalculatorViewController
secondOperationViewController!.delegate = self
println(secondOperationViewController)
}
Any suggestions what's wrong here?
Also I found similar question:
Delegate in Swift-language
I rewrited instantion of my CalculatorViewController to:
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
secondOperationViewController = segue!.destinationViewController as? CalculatorViewController
secondOperationViewController!.delegate = self;
}
It works, but my delegate method is being called from all 3 ViewControllers, connected to initial ViewController, so I need another solution.
So how can I call delegate method exactly from ViewController with specified identifier? Any ideas?
So, embed segue doesn't work with "instantiateViewControllerWithIdentifier" (but works with modal presented segue). I dont know why. Here is solution for embed segue:
override func viewDidLoad() {
super.viewDidLoad()
let secondOperationViewController:CalculatorViewController = childViewControllers[0] as CalculatorViewController
secondOperationViewController.delegate = self
}
In the storyboard give your segue a unique identifier:
Then in your prepareForSegue check against that before you set the delegate, this way you know exactly when you will be setting up this delegate.
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
// Checking to make sure this is the correct segue. This is also protection for the "as?"
if segue?.identifier == "SecondOVCSeuge" {
secondOperationViewController = segue!.destinationViewController as? CalculatorViewController
secondOperationViewController!.delegate = self;
}
}