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
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()
}
performSegueWithIdentifier Not Working - Swift
That's because you can't push view controller without UINavigationController. Please put UINavigationController or please set segue Kind "Modal" not "Push"
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)
})
Storyboard performSegueWithIdentifier not working
EDIT
I noticed you have a segue going the wrong way from your brandButton
. Connect you segue from your brandButton and drag to your VC, not from your VC to your brandButton.
RE-EDIT
You don't appear to have anything that will deliver a response to call the code correctly; A button. If you did, you would not have to code anything to demand a transition back, because the segue would automatically perform.
Try embedding your TableViewController in a UINavigationController by selecting your TableView --> Select from the Menu at the top of screen Editor --> Select Embed In --> Select NavigationController.
This way you won;t have to worry about coding a transition back, you will have the option to return built in.
Otherwise, continue with code below.
THEN
Either try:
//note YOU HAVE NOT INCLUDED .storyboard
// Get the storyboard from the main bundle - check the name to confirm:
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
[storyboard performSegueWithIdentifier:@"imagePreviewVCtoBrandSearchVC" sender:self];
OR Try presenting the view controller. Name your view you wish to change to - Select your second view. Then select the StoryBoard editor and name your selected VC:
Then code your button to change views like so:
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
[self presentViewController:vc animated:YES completion:nil];
//animated can be NO
Swift: performSegueWithIdentifier during unwind not working
In VC1 you need a function:
@IBAction func unwindToVC1(segue:UIStoryboardSegue) {
}
Then in your storyboard ctrl drag from the yellow view controller icon to the Exit icon and select unwindToVC1:
from the pop up
Give the unwind segue an identifier, say unwindToVC1
Now, in VC2, create your button touchUpInside
handler:
func buttonTapped(sender:UIButton) {
self.performSegueWithIdentifier("unwindToVC1")
}
when you set up your button programatically, add this method as the action handler:
button.addTarget(self, action: "buttonTapped:", forControlEvents: .TouchUpInside)
performSegueWithIdentifier not showing view controller as expected
Try to add:
super.performSegueWithIdentifier(identifier, sender: sender)
In your performSegueWithIdentifier
method.
PerformSegueWithIdentifier not working Swift
You should use didSelectRowAtIndexPath
instead of didDeselectRowAtIndexPath
. The latest is often propose first when using autocompletion, it's easy to make the mistake.
Related Topics
Swift Merge Audio and Video Files into One Video
Uisearchbar: Clear Background Color or Set Background Image
Getting the Value of a Uitextfield as Keystrokes Are Entered
How to Create a String with Format
Using Existing System Sounds in iOS App [Swift|
Add Uipickerview in Uiactionsheet from iOS 8 Not Working
Get Current iPhone Device Timezone Date and Time from Utc-5 Timezone Date and Time iPhone App
After Switching to Xcode 7, App Size Grew from 9 Mb to 60 Mb, Is There a Fix
When Do I Need to Call Setneedsdisplay in iOS
Uibutton Touch Is Delayed When in Uiscrollview
Swift Modal View Controller with Transparent Background
How Is the Relation Between Uiview's Clipstobounds and Calayer's Maskstobounds
Uibutton Doesn't Listen to Content Mode Setting
Xcode 6 with Swift Super Slow Typing and Autocompletion
Uiscrollview with Centered Uiimageview, Like Photos App
How to Show a View on the First Launch Only