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
Related
Trying to perform a segue "toLogin" when the login button is pressed. Segue is named and I control-dragged the button to the block of code to make sure they are associated. Very new to coding and not sure where I went wrong.
Currently, when button is pressed it changes color but does nothing. I also previously set it to print something when pressed but that never occurs either.
Screenshot of code, storyboard
For you to connect a button to a function, the function needs to look like below
#IBAction func login(_sender: Any) {
}
You will then need to connect to the blank circle next to the function on the left.
Regarding your screenshot, each segue linked to the whole main storyboard as highlighted showing. Not linked to button. Remove those two segues from storyboard first, and linked each segue to storyboard reference or next View Controller as attached image.
Make sure you have connected your button to the #IBAction in your UIViewControllerConnect IBAction to UIButton
Looks like you are triggering segue through UIBarButtonItem,
Swift 4.2,
loginButton.action = #selector(login(sender:))
#objc func login(sender: UIBarButtonItem) {
//Perform segue
}
Thanks for answering everybody. I'm still not sure what exactly my issue was but I decided to re-create the storyboard page without using UIBarItems and it worked. I'm guessing that something about the UIBar was preventing interaction.
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.
In my XCode project, which implements MultipeerConnectivity framework, I have a segue between two ViewControllers which is triggered programatically after MPCHandler successfully establishes connection with another peer. After segue is performed, the destination VC appears being completely blank (all white). However, if I try to access the destination VC with help of a button (with segue dragged directly from the button to the destination VC), it appears as it should.
Here is the line of code, which performs the segue:
performSegueWithIdentifier("seg_wait", sender: self)
So, yeah, I know how to perform segues and, yes, I made sure identifier is unique and is the same as in storyboard. I had tons of segues in many of my projects, and that's the first time I've faced a problem like this. Did anyone face a problem like this? Any ideas on how to solve it?
UI updates always have to be done on the main thread ("the law"). So try to wrap the call:
dispatch_async(dispatch_get_main_queue(), {
performSegueWithIdentifier("seg_wait", sender: self)
})
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.
I've just updated Xxode to work with Swift 2.0. As usual, a lot of new problems showed up.
In my app I have a view controller that checks whether the user is logged in and present either login screen or the app's home screen. It's a pretty simple VC:
class WelcomeViewController : UIViewController {
override func viewDidAppear(animated: Bool) {
if PFUser.currentUser() == nil {
self.performSegueWithIdentifier("segue-require-login", sender: self)
}
else {
self.performSegueWithIdentifier("segue-start-app", sender:self);
}
}
}
That used to work perfectly, but now it doesn't. The segue segue-require-login is of type "Present modally" and it works fine. The segue segue-start-app is "Show (e.g. Push)", but the view never gets pushed, even though the code is being executed (even prepareForSegue is called).
I've tried re-creating the segue, performing a Clean, cleaning the project's build folder but nothing seems to help.
Any thoughts?
There seems to be a bug in iOS 9 and Xcode 7 where if you have a UITextView with placeholder text, it prevents the segue from being triggered.
More explanation in the following answer:
iOS 9 Segue Causes App To Freeze (no crash or error thrown)
To fix it, try removing the placeholder text for the UITextView
I think I have the same problem: I have a UITabelView with cells created from a nib file, when a user tap a cell this method is called:
and when I have the following method prepareForSegue:: the application crashes:
if I delete the line 129 Everything is ok , the method prepareForSegue:: open the right view and the label contactName is shown with its default text.
If I modify the method as follows prepareForSegue:: get exactly what you expect, without having any type of error:
let me know if you also get the same result