In my app I have a “main” ViewController. On app launch in the body of its viewDidAppear, I retrieve the value of a UserDefaults boolean: if the user is not logged in, I present modally another viewcontroller this way:
self.performSegue(withIdentifier:"loginView", sender:self)
This launches modally AuthViewController, a viewcontroller that contains a “Container View”, a region of AuthViewController that includes a UIPageViewController (AuthPageViewController).
This last one has two “pages”: LoginViewController and RegisterViewController.
LoginViewController is used to login users: once the user is logged in I want to call a function (no matter what it does) on the main ViewController.
So, to summarize, I’m in LoginViewController and I want to call a method of ViewController.
While trying a solution I discovered the following:
I can’t get a reference to presentingViewController because LoginViewController has not been opened directly from ViewController. Moreover, AuthViewController was not launched with present(_:animated:completion:) but with self.performSegue as I said before;
instantiating a ViewController from LoginViewController and calling a method works but it’s not a good idea;
NSNotification works like a charm;
apparently delegates aren’t working or I’m missing something about their implementation in the contest of this app;
Can you help me understand how to call a ViewController method from my LoginViewController? Code examples are very well received, but I appreciate any advice.
Let me explain it with code of how to use delegate in this case,
First you need create a delegate in LoginViewController like this
protocol LoginViewControllerDelegate: class {
func goToContent(animated:Bool)
}
then create a variable for delegate like this
weak var delegate: LoginViewControllerDelegate?
Now in you ViewController you have to assign the delegate like this in your prepare prepareforsegue method, as you are using segue like this
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourSegueIdentifier" {
let nextScene = segue.destination as! LoginViewController
nextScene.delegate = self
}
}
and implement that method like this
func goToContent(animated: Bool) {
print("test")
}
in this way you are able to call goToContent from LoginViewController
Related
How can I invoke a function/method in another firstViewController swift file while the control/state is in secondViewController.
In Second ViewController when a button is pressed the secondViewController should invoke a function in firstViewController and transfer the control/state to thirdViewController to which it was pushed from secondViewController.
secondViewController Button Action
#IBAction func EnterGallery(_ sender: Any){
// Want to invoke EnterGallery function in firstViewController and dismiss from secondViewController
self.dismiss(animated: true, completion: nil)}
firstViewController pushViewController function
func EnterGallery() {
let dest = self.storyboard?.instantiateViewController(withIdentifier:
"GalleryViewController") as! GalleryViewController // thirdViewController
self.navigationController?.pushViewController(dest, animated: true)
}
Please Note: I am not passing any Data from secondViewController to firstViewController. I just want my firstViewController to push to thirdViewController while I just dismiss from secondViewController which was presented from firstViewController with the present function.
Once I dismiss from secondViewController I want my screen to go directly to thirdViewController.
Basically I just want to invoke a function in another ViewController without any data passing from initial ViewController. So I cannot use Protocols and Delegates or Notifications and Observers. How should I approach this?
There are many other cases where I need to use this similar functionality. So I am not sure how to exactly perform this.
As I am new to Swift, any help will be appreciated.
Thanks in advance.
Your viewController should know about other viewControllers and should be able to interact with them.
Here is a good article about passing data between viewControllers (or just interacting between viewControllers - as you like)
The most common practice is delegation pattern. In two words about delegation:
Create a delegate protocol:
protocol MyDelegate {
func doSmth()
}
Add delegate property to viewController that will trigger something in anotherViewController:
var delegate: MyDelegate?
anotherViewController should conform MyDelegate protocol:
class anotherViewController: MyDelegate {
func doSmth() {
print("I am doing something")
}
}
And then assign your class that is conformed to MyDelegate protocol into this property
viewController.delegate = anotherViewController
Thats it! Now you can trigger delegate method inside viewController
delegate.doSmth()
Google delegate pattern for this. YT: https://youtu.be/DBWu6TnhLeY
Hopefully this helps you out. By the way delegate pattern works even if you don’t want to pass data in between.
I have a pretty complicated setup in terms of view controllers. I have reasons for it that are kind of out of the scope of this question. So I have 3 view controllers.
ViewControllerA is the main view controller in this case. ViewControllerB is a container view controller that is displayed from ViewControllerA. ViewControllerB has a button that has a segue to display ViewControllerC. Then in ViewControllerC there is a button to dismiss to go back.
ViewController's A and B can be different. Depending on if the user is editing an object or creating a new object. The things I'm talking about remain constient between those two cases.
Basically my goal is when a user dismisses ViewControllerC it changes a button text on ViewControllerB. Depending on the users actions on ViewControllerC.
I was thinking about using self.presentingViewController somehow or something along those lines but I can't figure out how to access that specific button within ViewControllerB.
Any ideas of how I can achieve this?
I suggest you use a protocol to define a common method to update button text. Both ViewControllerB's can then conform to this protocol. Then use a delegate callback approach to call these methods from your ViewControllerC.
When you present ViewControllerC from ViewControllerB you can set the delegate property to self before presenting it. You would do this in different places depending on how you are presenting ViewControllerC. As you said you're using a segue to do it, then you should do this in the prepareForSegue method.
Declare a protocol that defines a method to update the button's text like this:
protocol ChangeableButtonTextViewController {
func updateButtonText(newText: String)
}
Then make your EditViewControllerB and CreateViewControllerB conform to this protocol to update the button text:
class EditViewControllerB: UIViewController, ChangeableButtonTextViewController {
func updateButtonText(newText: String) {
button.text = newText
}
// Other stuff in your ViewController
}
Add a delegate property to ViewControllerC like this:
var delegate: ChangeableButtonTextViewController?
Add a prepareForSegue method to EditViewControllerB and CreateViewControllerB which would look something like:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
segue.destination as! ViewControllerC).delegate = self
}
You can then do something like this in ViewControllerC:
func dismiss() {
delegate.updateButtonText("NewText")
}
Let me know if you need any further clarifications.
So, I have Navigation Controller. there are segue from Root View Controller to other View Controller.
When I want to get access to other View Controller I override prepareForSegue method and use destinationViewController property.
But that's not ok for me. All my stuff in prepareForSegue will be execute every time when segue is called, but I don't want it. Secondly, it destroys logic of my code: after performSegueWithIdentifier(actually before) execution jumps to other place in code.
It would be great if I can get access to other View Controller like I did it with Root ViewController - by keyword self, for example.
That's code example to make my question more clearer:
func startWorking() {
/*here we made some stuff for current VC
...
...
*/
//next we go to new View Controller
performSegueWithIdentifier("newVC", sender: nil)
//then all actions that I want to do begin at another method - prepareForSegue
//But I want get access to View Controller that user sees now!
//For example present some view:
let someView = UIView(frame: someFrame)
/*question subject*/.view.addSubview(somView)
}
/question subject/ - is the current ViewController that I have presented by segue and point of my question.
Sergey Gamayunov,
You can always access the top mostViewController in navigation stack using,
let viewCOntroller = self.navigationController?.topViewController
EDIT
I believe if you cant get your logic around the prepareForSegue or self.navigationController?.topViewController you must take a look into your design pattern :)
That being said I understand all you want to do is to access the ViewController after performSegue without using prepareForSegue, you can use this code
func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
if viewController is YourDestinationViewControllerClass {
print("You have access to viewController loaded do whatever you want")
}
}
The function stated above is a navigation controller delegate :) So you will have to declare your viewController to confirm UINavigationControllerDelegate. like
class ViewController: UIViewController,UINavigationControllerDelegate
and in
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.delegate = self
}
Thats it you are good to go :) Happy coding buddy :)
I've recently been playing around with swift segues and I'd love to incorporate one in my latest app, the problem is I can't seem to get them to work. So far I've created another view controller SecondViewController and referenced in my ViewController & SecondViewController files as so:
ViewController.swift
import UIKit
class ViewController: UIViewController {
var secondViewController: SecondViewController!
var viewController: ViewController!
override func viewDidLoad() {
//lots more code here
SecondViewController.swift
import UIKit
class SecondViewController: UIViewController {
var secondViewController: SecondViewController!
var viewController: ViewController!
override func viewDidLoad() {
Them in storyboard view I've crtl+dragged a segue from viewController to secondViewController and once that's been created given that segue an identifier using the right hand panel, the segue identifier is GameOver and the segue type is show.
Now I want to call the segue automatically with no interaction from the user, in the final app once the user hits the game over func it would trigger the segue and display a new UIView where the highscore could be displayed with a few other items.
The code I'm using to call the segue is:
self.viewController.performSegueWithIdentifier("GameOver", sender: self)
I receive the following error...
Thread 1:EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0
I also have this error in the output field...
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
I've played around with the names of the segues and the file names and I still get the same error, I'm sure I'm missing something fundamental so hopefully someone can help me work this out.
I've created a new project and uploaded it to GitHub, if anyone could tell me what I'm missing that would be great, here is a link to my GitHub repository https://github.com/rich84ts/TestSingleView
Thank you.
You cannot just throw in some instance properties and expect them to magically do something:
class ViewController: UIViewController {
var secondViewController: SecondViewController!
var viewController: ViewController!
}
Those properties are nil, and sending a message to them will crash your app. You have to give them values.
In your case, the segue emanates from this view controller, so what you actually want to say is
self.performSegueWithIdentifier("GameOver", sender: self)
The other big mistake you are making is that you are saying all this in viewDidLoad. That is way too early! You can't do any segue-ing yet; your view is not even in the interface! Move your code into viewDidAppear: and it will actually work:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.performSegueWithIdentifier("GameOver", sender: self)
}
Your code is still silly and useless, but at least you will see something happen and you can continue developing from there.
What I actually recommend is that you delete your viewDidLoad implementation and put this:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
delay(1) {
self.performSegueWithIdentifier("GameOver", sender: self)
}
}
That will allow the first view controller to appear, wait one second, then summon the second view controller. And so you will learn that everything is hooked up correctly, and can proceed to do something more practical.
You can create a manual segue from the storyboard by control-clicking the ViewController object and dragging from the manual segue to the destination view controller. You can then call this segue with the designated identifier from your source controller. You don't need a reference to the destination view controller to achieve this.
To reference anything from the storyboard in your view controller you need to declare your properties like this:
#IBOutlet var someProperty : UIView?
The #IBOutlet bit makes the property visible on the storyboard and you can control-drag from it to a corresponding object in a view. You can't do this with view controllers though. To access the destination view controller in your source view controller before the segue you need to override func prepareForSegue(_ segue: UIStoryboardSegue,
sender sender: AnyObject?). This allows you to access the destination view controller from the segue-instance before the actual segue (if you need to pass it data for example).
Firstly your self.viewController is a nil object as you only created the variable and didn't initialize it. You can't call a method with nil object. Secondly you have created a push segue from storyboard but you don't have navigation controller in storyboard so self.performSegueWithIdentifier("GameOver", sender: self) will also not work. To use push segue you should have you current viewcontroller in UINavigationController's stack, so first add a UINavigationController in storyboard and make that initial view controller and set ViewController to the rootViewController of the navigation controller then call self.performSegueWithIdentifier("GameOver", sender: self)
Then the code will work. Hope this help.
I am developing an app in Swift that, in gist, tells people the price of Bitcoin in various currencies. To select the currency, the user chooses from a list in a view controller with a UITableView. This is currencyViewController, and it is presented from my main screen, viewController.
What I want to happen is that, when the user dismisses currencyViewController, it passes a string to a UIButton in the main viewController.
Here's the prepareForSegue function that should pass the data:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "presentCurrency") {
currencySelector.setTitle("\currencySelected", forState: UIControlState.Normal)
}
}
CurrencySelector is a UIButton in the main viewController, and currencySelected is a variable in the second view controller, currencyViewController.
It gives the error "Invalid Escape Sequence In Literal"
So, I've narrowed it down to one of two issues:
The variables from viewController can't be "seen" from currencyViewController. If so, how can I modify the text of CurrencySelector from CurrencyViewController?
For some reason, when the user exits the pushed CurrencyViewControler, prepareForSegue isn't called.
What is going on here? Thanks, and apologies - I am but a swift newbie.
2 - "prepareForSegue" is called when you push a new view controller via the segue, but not when you dismiss it. No segue is called upon dismissal.
1 - A good way to do this would be the delegate pattern.
So the main view controller would be the delegate for the currencyViewController, and would receive a message when that controller is dismissed.
In the start of the currencyViewController file you prepare the delegate:
protocol CurrencyViewControllerDelegate {
func currencyViewControllerDidSelect(value: String)
}
and you add a variable to the currencyViewController:
var delegate : CurrencyViewControllerDelegate?
Now, the mainViewController has to conform to that protocol and answer to that function:
class MainViewController : UIViewController, CurrencyViewControllerDelegate {
//...
func currencyViewControllerDidSelect(value: String) {
//do your stuff here
}
}
And everything is prepared. Last steps, in prepareForSegue (MainViewController), you will set the delegate of the currencyViewController:
var currencyVC = segue.destinationViewController as CurrencyViewController
currencyVC.delegate = self;
And when the user selects the value in currencyViewController, just call that function in the delegate:
self.delegate?.currencyViewControllerDidSelect("stuff")
A bit complex maybe, but it's a very useful pattern :) here is a nice tutorial with more info if you want it:
http://www.raywenderlich.com/75289/swift-tutorial-part-3-tuples-protocols-delegates-table-views
You have to use the parantheses to eval variables in strings, i.e. println("\(currencySelected)")
To access variables in the second view controller (the one which is the destination of the segue) you have to get a reference to it:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "presentCurrency") {
let currencyViewController = segue.destinationViewController as CurrencyViewController // the name or your class here
currencySelector.setTitle("\(currencyViewController.currencySelected)", forState: UIControlState.Normal)
}
}