How to run code before initial view opens? - ios

i want to run this code before the initial view controller even opens the view so the app knows where to redirect the user if the user is logged in. how can i do that? the code is here:
if let firstCheck = NSUserDefaults.standardUserDefaults().objectForKey("username"){
self.performSegueWithIdentifier("View1ToView2Segue", sender: nil)
}
i put the code after override func viewDidLoad() but it didnt work.

Unfortunately, you cannot do that. :( In this case, I would have a load image or something similar and have it fade away if the segue is not supposed to be performed. You would run this in the viewDidAppear method.

Related

Same view controller loads twice

I've read several posts on this issue but none of them solved my problem.
I'm coding an app where I have to click on a button ("Prepare") to go to the following ViewController. When the button is clicked, it also passes data between the two view controller.
The problem is, when I click the button, the following ViewController loads twice. Thus, if I want to go back I have to go back through two same ViewController.
I've checked the segue, the class names and files names but nothing fixes it.
I've also created a new project and rewritten all the code from the beginning but in the end, it still doesn't work.
However, I've noticed that the problem showed up when I added the prepare(forSegue:) function and the performSegue function. Without it the ViewController only loads once. But of course, I can't get the data passed between the views without it...
Here is the screenshot of my two view and the code of the two functions :
First view
Second view
// Prepare the segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "prepareSegue" {
let timerViewController = segue.destination as! CountdownViewController
timerViewController.timeInterval1 = waitingTime
timerViewController.timeInterval2 = shootingTime
timerViewController.lastSeconds = lastSeconds
timerViewController.currentTimeInterval = waitingTime
}
}
// Prepare the timer when the button is pushed
#IBAction func prepareTimer(_ sender: Any) {
performSegue(withIdentifier: "prepareSegue", sender: self)
}
Probably, when two ViewControllers appear it's because you have:
A segue on Storyboard which start directly from a Button
IBAction attached to the button where you call a performSegue of the same Segue
To fix this problem, you need to create a Segue which start directly from ViewController. After you can use the IBAction and the performSegue
You have added a seague but also an IBAction. If the seague is defined well in InterfaceBuilder it will perform and call your method. The IBAction is the alternative way for connecting an Action to a button. If you use both, you have two actions.
Without the rest of your project to check, one thing it could be is a completion closure which contains the performSegue(...) statement is called twice, so the performSegue statement is literally run twice. How might this happen?
If you have a networking call inside the closure which contains performSegue command, and the networking call has a closure which calls completion(...) twice, then you could get either the segue occuring twice or even recursively!
It's rare this will be the case, or in your instance, but this type of problem can be uncovered by using lots of breakpoints and following the "bouncing ball" to see the completion() calls occuring more than once.

IOS Segue not appearing. No error

I am trying to implement a segue programmatically in IOS.
My story board looks like this:
The first segue between the first two screens is done by connecting the Login button to the next screen (so not completely programmatically), and it works fine.
The issue is with the "secondSegue", which I tried doing programmatically.
It simply does not work and there is no error message appearing.
I connected the 'Load this Screen' button to the following action in the Second Screen's view controller:
#IBAction func openThirdScreen(_ sender: Any) {
performSegue(withIdentifier: "secondSegue", sender: nil)
}
But the screen isn't opening and there is no error.
All the proper IBOutlets seem connected so I don't know what the issue could be.
I put a print statement in openThirdScreen(), and it prints, but a print statement in the third screen's View Controller's viewDidLoad() does not print, even though
I made sure the third screen was connected to a view controller.
I had overridden performSegue() earlier in order to do more with the segue but it was not working so I removed it for simplicity.
Please let me know what I messed up.
Sorry if this is a stupid question, I'm very new to XCode and I couldn't find anyone online with the same issue.
Let me know if there are more screenshots that are necessary to answer my question.
Edit: the following image was requested, in order to answer the question

How to reload a previous page when segue back

I use this line of code in my iOS app
self.navigationController?.popToRootViewControllerAnimated(false)
It takes the user back the login page, but it does not reload the login with all the most recent data.
It will not run this code again:
override func viewDidLoad() {
super.viewDidLoad()
...
}
How do I make the segue cause the viewDidLoad again?
Use viewWillAppear method for that.
You are sending a web call from viewDidLoad(). So to update your viewController add your web request method in viewWillAppear().
Note: viewDidLoad gets called only when the view controller loads which happen once. However viewWillAppear gets called before it appears which will often happen if you're hopping back and forth between views.

Segue call from viewDidAppear works, but not from viewWillAppear

I have the following scenario with the given environment:
Xcode v7.3.1
Swift 2
From within a login viewController, I initially had the following segue call in viewWillAppear method to bypass login screen if the user is already logged in:
override func viewWillAppear(animated: Bool) {
if PFUser.currentUser() != nil {
self.performSegueWithIdentifier("login", sender: self)
}
}
The segue was not working, so I set a breakpoint where the segue call is being made and verified that it is indeed being hit. I also step through the call and verify no exceptions are thrown, but for some reason the segue is not performed.
However, I put the same call within the viewDidAppear method:
override func viewDidAppear(animated: Bool) {
if PFUser.currentUser() != nil {
self.performSegueWithIdentifier("login", sender: self)
}
}
Again, I verified that the segue call is being hit, but in this case the segue is actually performed. So I'm wondering why the exact same call doesn't work when it is invoked in the viewWillAppear method but does work as expected within viewDidAppear. The most puzzling part is that it appears that the call is being made, but the expected behavior is not observed. I tried Googling for anyone else running into this problem but was not able to find anything.
I guess I can live with it being called from viewWillAppear, but there is that brief appearance of the login screen before being redirected, which is slightly annoying. If anyone has a solution or suggestions on how I might be able to get it to work from viewWillAppear I would be greatly appreciative.
You cannot perform segues in the viewWillAppear method. This is because the view has not fully appeared yet, and it cannot transition over to another view. In this scenario, I would have a load image or something similar and have it fade away if the segue is not supposed to be performed. This, of course, would be done in the viewDidAppear method.

performSegueWithIdentifier not firing from unwind segue

Code used to work with Swift 1 but now does not and here is my issue. (did check other questions but this is slightly different, and enough so that the other solututions did not work.
So I am trying to present a logon screen which I do as follows
and it works fine, the user enters in their username and password. I then programmatically check they can log in and all still good and do the unwind as follows:
self.performSegueWithIdentifier("unwindGoToTeamPage", sender: self)
But the following code does not fire even though the func is called. I know that because I had a print statement in their for debuging and it shows on the console:
#IBAction func unwindGoToTeamPage(sender: UIStoryboardSegue){
print("In here")
self.performSegueWithIdentifier("goToTeamPage", sender: self)
}
Now I know its related to the same reason that at the start of the ViewController life cycle you can not call a
self.performSegueWithIdentifier("goToTeamPage", sender: self)
as it wont work in the
override func viewDidLoad() {
super.viewDidLoad()
}
or the
override func viewWillAppear(animated: Bool) {
}
but only in the
override func viewDidAppear(animated: Bool) {
if appDelegate.tkData[0] == "1"
{
self.performSegueWithIdentifier("goToTeamPage", sender: self)
}
}
In Swift 1 and Objective-C I used to put in a timer and all was good but that no longer works and I prefer to put in a solid solution that is not a hack. I think the solution involves waiting till the logon viewcontroller is completely dismissed but not sure the correct way to achieve it.
Could a dispatch to the main queue be an option. I will try that while I wait if someone has an answer and post an update. I also noted it works just fine on a iPhone or iPod but its the iPad where the issue is unsolved.
Appreciate any pointers to solve this problem. Thanks
Update, Yes I did try the UITextField solutions but that appears not to be the issue in this instance. I am slowly identifying the issue as one view controller not yet finished before the
self.performSegueWithIdentifier("goToTeamPage", sender: self)
fires.
I also apologies that i failed to mention the console message which is:
Warning: Attempt to present <new view controller> on <current view controller> whose view is not in the window hierarchy!
So I see my issue is as needing to sort of run the logon viewcontroller in such a way as to have the orignial viewcontroller get a message that the logon view conntroller has finished and only then fire the
self.performSegueWithIdentifier("goToTeamPage", sender: self)
SO is there are way to get a call back of some sort to tell the original view controller that the child view controller has completely finished so I can then call the next function only once the child view controller is finished.
Thanks for any help on this.
I have this sort of working but feel its really just another hack and though will get me out of trouble this time disappointed I could not find the correct way of doing it.
So the issue is mainly with the modally presentation of the logon screen where once it is dismissed it takes however many microseconds to be removed from the view hierarchy. Trying to move to another viewcontroller with
self.performSegueWithIdentifier("goToTeamPage", sender: self)
will not work which is my dilemma.
So to get around it I made the logon screen a full screen (not popover or modally) and then put the code
self.performSegueWithIdentifier("goToTeamPage", sender: self)
in the
override func viewDidAppear(animated: Bool) {
self.performSegueWithIdentifier("goToTeamPage", sender: self)
}
function.
Not the best solution as the logon screen is now full screen and while ok on the iphone not the best on the iPad.
So, sort of fixed but as a hack and work around and STILL KEEN if anyone has a solution where I can go back to the modally view for the logon screen.

Resources