Perform Segue with Identifier Wont Work in Swift 2

perform segue with identifier wont work in swift 2

Make sure that in your storyboard the segue type is not set to custom. If you set it to custom you'll need to provide your own custom segue class.

Swift segue not working?

Generally, any UI updating has to be in main thread. I think the block for PFFacebookUtils.logInInBackgroundWithAccessToken is still in the background state in above situation. Maybe trigger the showViewController in dispatch_async(dispatch_get_main_queue(), {}) and see if there is any difference.

dispatch_async(dispatch_get_main_queue(), {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SignUpViewController")
self.showViewController(vc, sender: self)
})

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

how can I delay perform Segue with identifier

Use asyncAfter method of DispatchQueue to delay calling any code. Whatever that is inside the closure/block will get executed after the delay you specify, in the below case I've set it to 2 seconds from the current time.

DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
// Place your code to execute after a delay of 2 seconds here.
// Before calling `asyncAfter` perform showing loader or anything you want.
}

Unable to perform segue due to no segue with identifier error

Thanks to @MikeG's guidance I was able to get it to work.

The problem was that I created a segue from the main view controller to the second view controller.

Solution

Create a segue from the second view controller to the main view controller and if you have a navigation controller, create the segue from the second view controller to the navigation controller.

The code remained the same.

performSegue not working and result crash

Not sure if it was my mistake or something changed in segue connection. Earlier I used to drag the connection from destination to the source view controller but I feel it is not working now. It starts working when I changed the direction of connection i.e. source to destination ViewController.

self.performSegue cannot find storyboard-segue-ID even though it has been named

You need to have the segue from LiveController, not from Navigation Controller

Not performing segue

You need to call this self.performSegue method in dispatch_async block:

override func viewDidLoad() {
super.viewDidLoad()

DispatchQueue.main.async {
self.performSegue(withIdentifier: "seg", sender: self)
}
}

That's because viewDidLoad() runs in background thread and all UI updates and transitions must be done on Main Thread.



Related Topics



Leave a reply



Submit