Button not performing segue - ios

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.

Related

How can i use the IBAction botton to go to the next storyboard using Swift?

Im trying to write a function that will take me from on storyboard to the next using a button. I am new to iOS app dev and Swift.
I have a button with this IBAction but im not sure how to connect pages. Meaning going to the next page using a button. thanks.
#IBAction internal func nextBtn(_ sender: Any)
}
Drag from the View in storyboard we’re you have the brutton on , crt drag from the yellow dot on the top of the view, to next view you want to navigate to. Give it a name in and put this in your button function.
// segueName is your name on segue
performSegue(withIdentifier: "segueName", sender: self)
Difference between drag it straight from the button or the view controller is that you can’t have any if statement if you drag it from the button.
Exp you only want to execute segue if user did something and the pressed the button you need to disable the button. Otherwise it will always be executed.
But if you use a performsegue in the button action you could easy make a statement and and execute the segue.
No need to write any code for this, it can all be done in Interface Builder. Create a storyboard reference in IB, and control-drag from the “next” button to the storyboard reference. Done.
There is a nice tutorial with pictures here:
https://medium.com/#wilson.balderrama/how-to-segue-between-storyboards-86c582f976f7

Do something on button tap then segue to another view controller

I turned an image into a UIgesturerecognizer so that I can press it like a button. When I press it I want it to segue out, but the next view loads a camera so I'm getting kind of a freeze while it's loading. I can't really run an activity indicator because it's going between view controllers. So I made a label that says "please wait..." and I set it to appear on the button press before the segue. The problem is that it isn't actually appearing because the freeze is happening before it actually shows up. Then on the new vc you briefly see it flash, so it is appearing but not quick enough to be efficient. How can I make the label appear, then begin the segue? This is the code right now
#IBAction func buttonTapped(sender: AnyObject) {
//I want this label to show up before the segue starts happening
self.loadingLabel.hidden = false
//Segue
self.performSegueWithIdentifier("profilePicCamera", sender: self)
}
Call your camera load function from the viewDidAppear method.

Swift - Impossible to use performSegueWithIdentifier

I am struggling with this problem for a few hours now.
I have a view with a tableView and custom cell in it. What I am trying to do is to perform a segue which is in my viewController from the custom cell. The first thing I did was to create a variable called "parent" in my cell.swift with the type "viewController".
weak var parent : ViewController!
After that, I'm just trying to perform the segue like this :
parent.performSegueWithIdentifier("eventDetails", sender: parent)
And I have the following error : Receiver (<Deevent.ViewController: 0x7fb99074f5d0>) has no segue with identifier 'eventDetails'
So I tried something else... I created a button in my ViewController and connected a segue to the next view (everything from the storyboard file)
In my viewController.swift I created the following function
func viewEventDetails() {
performSegueWithIdentifier("eventDetails", sender: self)
}
and this one in my cell.swift :
func viewEvent(sender: AnyObject) {
parent.viewEventDetails()
}
So when I call it from the cell, it crashes with the same error than before but when I click on the button it's working. I even tried to click on the button programmatically btnDetails.sendActionsForControlEvents(UIControlEvents.TouchUpInside) and I had the exact same error.
I've already tried to clean my project and delete it from the simulator. I'm really missing something here... hope somebody can help me.
Thanks !
It sounds like you need to give your segue an Identifier.
A view controller can have multiple segues, and so you need to define which one you want to use. In your case, in your code you are using the Identifier "eventDetails".
Assuming you are using a Storyboard, you need to click on the segue arrow between the two view controllers, and in the Attributes Inspector on the right, set the Identifier value to "eventDetails".

Dragging onto the exit of the viewcontroller not working

I have looked for this problem everywhere.
I know how the unwind scene works. I have implemented the following code in the A VC
-(IBAction)returned:(UIStoryboardSegue *)segue {
try.text = #"Returned from Scene 1";
}
But when I go to my B VC(Which is the VC I want to go back to A VC from) and ctrl drag a button onto the exit at the bottom it will not allow me to. And no function pops up in the exits option. Anyone else had this issue or can help?
To set up an unwind segue, you should
Set up your two scenes with the standard modal or push segue between them:
Define the custom classes for those two scenes to be your respective view controllers.
Implement the action in the .m for view controller A:
- (IBAction)unwindToA:(UIStoryboardSegue *)segue
{
//returning to A
}
Then you can create a segue in B by control-dragging (or right-click-dragging) from some control down to the exit outlet in the bar below scene B:
And if everything above is configured correctly, you will now see your unwind segue listed in a popover:
To make Exit outlet active, add this code to viewController. Now you should be able to ctrl-drag to exit outlet.
It is actually quite odd, that there should be empty method to make it work.
swift
#IBAction func unwindSegue(unwindSegue:UIStoryboardSegue)
objC
- (IBAction)unwindSegue:(UIStoryboardSegue *)sender;
I also had a problem with this even though everything was set up correctly. Eventually I tried closing the project and reopening it and that did the trick. Must have been some bug in Xcode.
This doesn't seem to have been the cause of your problem but I thought I'd add the answer in case someone else has the same problem.
#LoPoBo, #hozefam, & #Van Du Tran
If closing down isn't working, perhaps you were dragging in the wrong manner. Instead of clicking on the Bar Button Item and dragging from Action Segue to the exit try just right click -> hold & drag to exit segue. This was messing with me for a good 20min before I figured it out. Hope this helps someone else too. Using xcode 6.1

Fire a segue conditionally to multiple View Controllers

After struggling for days on firing a segue conditionally, I managed to solve it thanks to Simon's answer here. Please take a moment to have a look or you might not understand what I'm talking about below. I didn't copy paste his answer because he's already explained it nicely over there.
Now I've faced a new question. What if I have multiple View Controllers that I want to segue to from one View Controller?
To explain it further : Say I have one MainViewController with 2 buttons. When clicked upon each button, it should segue to their respective View Controller. First button to FirstViewController and the second button to SecondViewController.
The method described in Simon's answer can be used when you segue from one View Controller to another View Controller. Since in that method, you tie the segue to the View Controller itsrlf and not to the button, you have only one segue with an identifier for that particular View Controller. Therefore I cannot distinguish between the button taps separately.
Is there a workaround to solve this problem?
Thank you.
It might be bit premature to say this but I guess you should look into Segue more deeply.
Yes you can perform segure from button. Just control click the button and drag the cursor to view controller you want it SEGUE'd. And from my understanding only condition there is each button tap results a segue to a fixed view. There is no condition there.
Also, you can push the navigation controller manually by
YourViewController *destViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"YourDestinationViewId"];
[self.navigationController pushViewController:destViewController animated:YES];
UPDATE:
prepareForSegue is too late to stop a segue from proceeding. Yes you can create multiple segues from your view to other view controllers. And in this case you have to do so. Don't reate a segue from button, just define a IBACtion on the button click you can do the validation from there,
if(validationSuccess) {
[self performSegueWithIdentifier:#"segue1" sender:self];
}
if you are using ios6
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
return YES on validation success and NO on failure to stop it from proceeding.
I suggest you look a bit at reworking your code logic.
If I understand correctly, you have a VC (embedded in a Nav. Controller) with 2 buttons and you have figured out how to segue each button to a different VC.
Your problem is you want to make sure that even if one of the buttons are pressed, a validation is done before an action takes place. I would advise this is bad User Interface design because the user has the illusion that this button might do something and then they click it and nothing happens.
UIButton can be connected to IBActions (to initiate actions) and IBOutlets (to set their properties). If this is a button created in IB directly, I would connect it to your class as an Outlet property:
#property (nonatomic,weak) IBOutlet UIButton* myButton;
And then set its enabled value:
self.myButton.enabled=NO;
This will keep the button and dim it. This is much better UI design and the user knows they should not press the button because some condition is not satisfied.
I would rework the code so that you set this value as disabled by default for example and enable it appropriately in your code whenever your "condition" is satisfied.
Obviously if this button is created programmatically (in your code without IB) then it is easy to just use the second command above.
Hope this helps.
I just wrote another way to call multiple detail views from a single table. Each cell could essentially make a different view be displayed. The code is similar to what you see in this post but you essentially use identifiers and attributes on the list item to determine which view to show.
https://codebylarry.com/2016/07/15/multiple-detail-views-in-swift/
override func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 1 {
self.performSegueWithIdentifier("secondView", sender: self)
} else {
self.performSegueWithIdentifier(“others", sender: self)
}
}

Resources