performSegueWithIdentifier not firing from unwind segue - ios

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.

Related

Should I use a segue to move between a sign up screen and the rest of my app

I have created a login/sign up screen for my application using firebase and now when a user successfully logs in or signs up I want to transfer them to the rest of my application.
I am currently using a segue to do this, so whenever a user enters information that is valid in the firebase database, or whenever they sign up and create their information in the database, a segue takes them to the tab bar controller of the app itself.
I am new to Xcode and don't know if this is a safe thing to do or not. For example, does this create a danger of users being able to get into my actual app without being properly identified in the database if they somehow manipulate the segue into taking them there? I don't want any dangers like this in my app and want to know if there is any other way in which this could, or maybe should, be done.
Thank you very much.
Using a segue is fine, but you might want to consider doing it in reverse.
If the user isn't signed up, when the application is launched, present a modal sign up viewcontroller from the main application viewcontroller. Once they've entered their info and have been validated, do an unwind segue to return to the main application screen.
The reason to do it this way is that it frees the memory used by the login/signup screen. The way you are doing it keeps the login/signup VC in memory the whole time the app is running.
Your main viewController in your UITabViewController would look like this:
class FirstViewController: UIViewController {
var loggedIn = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if !loggedIn {
self.performSegue(withIdentifier: "LogInSignUp", sender: self)
}
}
// This is the target of the unwind segue
#IBAction func finishedLoggingIn(_ segue: UIStoryboardSegue) {
loggedIn = true
print("logged in and ready to go!")
}
}
If you uncheck the Animates checkbox in the Attributes Inspector for the "LogInSignUp" segue, the login screen will appear immediately without animation, but it will still drop down once the login is complete (during the unwind segue).
There is no way for users to manipulate a segue. It's up to you, of course, to make sure that users are properly authenticated before taking them into your application.

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 run code before initial view opens?

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.

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 very slow when segue is modal

I have a simple table view where I handle the select action on the table view. This action follows a segue.
If the segue is a push segue, the next view shows immediately.
If the segue is a modal segue, the next view either:
takes 6 seconds or so to display
shows immediately if I tap again (second tap)
I tried looking around for some ideas, but none seem applicable to my situation. In particular:
I'm performing the segue on the main UI thread
My view is very simple (so there's no issue in viewDidLoad). Plus the fact that it shows up near instantaneous when the segue is push indicates that there is no problem loading the target view
I tried passing nil to the sender; same effect.
Does anyone have any ideas on this?
Trust me and try this. I have run into this problem a few times.
In Swift 2:
dispatch_async(dispatch_get_main_queue(),{
self.performSegue(withIdentifier:mysegueIdentifier,sender: self)
})
or for Swift 3:
DispatchQueue.main.async {
self.performSegue(withIdentifier: mysegueIdentifier,sender: self)
}
As discussed here and here.
It seems (to me...) that this problem happens only when the cell selectionType is not .none.
You may change it to any other option (at the storyboard Attribute inspector, set the Selection field) and it will work fine (working for me...).
The cons is that it messing up the cell UI.
You can call the segue in DispatchQueue.main.async{} block at the didSelect delegate function of UITableViewDelegate as people mention before.
I used the first solution and added at the cell itself -
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(false, animated: false)
}
This will make the cell 'highlight' at the tap, but it will return to its usual UI immediately and it fine for me...
There seem to be various situations when performing a segue will not work properly. For example, if you call performSegue from the action handler of an unwind segue, you will run into various issues, even though you are on the main thread. On my current project, I am calling performSegue from the didSelectRowAt method of a table view. This is one of the most basic segues there is and of course I am on the main thread, yet I was seeing the exact symptoms that the OP described.
I do not know why this happens in some cases and not others, but I have found that deferring the performSegue call using async fixes any potential issues. This used to seem like a hack and made me nervous, but at this point I have several mature projects using this approach and it now seems like the "right" way to do a manual segue.
Here is the Swift 3 version of the code (see the other posts for Swift 2 and Obj-C versions):
DispatchQueue.main.async {
self.performSegue(withIdentifier: "theIdentifier", sender: theSender)
}
The accepted solution worked for me. Code updated for Swift 2.0 below:
dispatch_async(dispatch_get_main_queue(),{
self.performSegueWithIdentifier(mysegueIdentifier, sender:self)
})
Hope help this yo can create programatically modal transition like this in Swift:
let myStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let modalViewController = myStoryboard.instantiateViewControllerWithIdentifier("myModalViewController") as! myModalViewController
modalViewController.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
let navController = UINavigationController(rootViewController: accountManager)
dispatch_async(dispatch_get_main_queue(),{
self.presentViewController(navController, animated: true, completion: nil)
})
For developers organizing their code through subclassing, I've come to quite simple a solution I'd like to share (Swift 4):
import UIKit
class ABCViewController: UIViewController {
// ... Other useful methods like overriding deinit and didReceiveMemoryWarning
// Performs a segue on the main thread to ensure it will
// transition once a UI-slot is available
func performSegueOnMainThread(with identifier: String, sender: Any?) {
DispatchQueue.main.async {
self.performSegue(with: identifier, sender: sender)
}
}
}
Then, simply call it from your implementation:
myViewController.performSegueOnMainThread(with: "ShowDetailsSegue", sender: self)
For me it was the that I had too many "Clear" colored views in my next view, so when the segue was animated, it seemed like it was delaying because of this. I went through the view controller's UI hierarchy looking for clear colors and replacing them with solid black or white, and making sure the alpha is 1 (if it didn't need to be otherwise). My delay is now gone and my modal presentation is smooth.
I tried fixing this multiple ways including moving it to the main thread above. This worked for me:
In storyboard, select the table view in question, (select it in document outline to make sure you've got the right thing. Then in attributes inspector you will be able to see the attributes for the table view as well as the containing scrollview underneath it (all table views are based on scroll view). There is this stupid little box there called "delays content touches". Uncheck it. There is also one "allows cancellable touches" which I'll imagine you want to make sure is unchecked as well, as I think double touches were messing mine up.

Resources