performSegueWithIdentifier not working if called from viewDidLoad
EXPLANATION:
Your View hasn't appeared yet when you call your checkStoredUser()
.
EASY FIX:
Put it in viewDidAppear()
like this:
override func viewDidAppear(animated:Bool) {
super.viewDidAppear(false)
checkStoredUser()
}
Why doesn't performSegueWithIdentifier work inside viewDidLoad?
You can not dismiss a view controller that isn't presented yet. didLoad
has purely memory management functions, you can use it as (part of a) constructor.
What may work, is to start a segue in viewDidAppear
, however I would suggest to start with the view you want at the first time.
performSegueWithIdentifier and viewDidLoad
viewDidLoad is called when the ViewController object is created loading nib file. It is not yet attached to the window. You should call performSegue... call in viewDidAppear.
Finally, If you are calling performSegue directly without any action being performed. I'd suggest you take a look at the flow again.
Segue launching but viewDidLoad not firing for new ViewController
In your storyboard, select your mapViewController, then click on the Identity Inspector. The first item should be Custom Class, and the Class name should be set to the name of your mapViewController class.
Swift performSegueWithIdentifier not working
[Assuming that your code is not crashing, but rather just failing to segue]
At least one problem is:
self.performSegueWithIdentifier("Test", sender: self)
should be:
dispatch_async(dispatch_get_main_queue()) {
[unowned self] in
self.performSegueWithIdentifier("Test", sender: self)
}
Remember that all UI operations must be performed on the main thread's queue. You can prove to yourself you're on the wrong thread by checking:
NSThread.isMainThread() // is going to be false in the PF completion handler
ADDENDUM
If there's any chance self
might become nil, such as getting dismissed or otherwise deallocated because it's not needed, you should capture self weakly as [weak self]
not unowned
, and use safe unwrapping: if let s = self { s.doStuff() }
or optional chaining: self?.doStuff(...)
ADDENDUM 2
This seems to be a popular answer so it's important to mention this newer alternative here:
NSOperationQueue.mainQueue().addOperationWithBlock {
[weak self] in
self?.performSegueWithIdentifier("Test", sender: self)
}
Note, from https://www.raywenderlich.com/76341/use-nsoperation-nsoperationqueue-swift:
NSOperation vs. Grand Central Dispatch (GCD)
GCD [dispatch_* calls] is a lightweight way to represent units of work that are going to be executed concurrently.
NSOperation adds a little extra overhead compared to GCD, but you can add dependency among various operations and re-use, cancel or suspend them.
ADDENDUM 3
Apple hides the single-threaded rule here:
NOTE
For the most part, use UIKit classes only from your app’s main thread.
This is particularly true for classes derived from UIResponder or that
involve manipulating your app’s user interface in any way.
SWIFT 4
DispatchQueue.main.async(){
self.performSegue(withIdentifier: "Test", sender: self)
}
Reference:
https://developer.apple.com/documentation/uikit
Related Topics
Debug View Hierarchy Does Not Render UI
A Concise Way to Not Execute a Loop Now That C-Style for Loops Are Going to Be Removed from Swift 3
How to Call Completionhandler for Performfetchwithcompletionhandler in Swift
Swift 4 - Hmcharacteristictypeserialnumber Deprecated
Animation Delay on Left Side of Screen in iOS Keyboard Extension
How to Completely Remove All Xcode Program and Cache Files
How to Spin and Add a Linear Force to an Entity Loaded from Reality Composer
Arrayliteralconvertible: Just a Normal Protocol
Get Png Representation of Nsimage in Swift
Swift Difference Between Double and Float64
Swift Display Image UIimageview
Cocoa: Simulating Command+Tab in Cgevent
Nscalendar in Swift - Init Can Return Nil, But Isn't Optional
Swift Switch Case Compiler Error
How to Change Orientation for Avcapturemoviefileoutput in Swift