Connection does NOT display Action - swift

When I ctrl-drag from a UIButton in storyboard to the viewcontroller class,
Xcode is not showing the option to create an IBAction.
I have already tried to start a new project and it still does not have the Connect: Action option available.

Please follow below steps :-
Open storyboard and yourViewController side by side after clicking on Assistant Editor.
Now Drag and Drop from UIButton to class file. It will open one popup menu.
Now click on connection it will open option menu with Outlet, Action, Outlet Connection.
Now click on Action. It will set within the same popup menu, and ask for method name. Here you have to add your Method Name, and click on connect button.
At the last point you will find your method within your class, with #IBAction connection.
Hope above steps is useful for you.

Check that the ViewController cocoa class is correctly connected to the ViewController in the Interface builder, then,try with this answer: https://stackoverflow.com/a/37322981/8038084.

Sometimes, even restarting and cleaning does not help me. What I do is make it the other way round.
Create an IBAction manually in your code.
#IBAction func buttonPressed() {
[...]
}
The function should appear inside your IB under Connections inspector/Received Actions...
where you can simply connect it to your button/gesture recognizer etc.

Related

Why is the IB Action set to exit as object?

I am creating an app that requires notifications. I created a settings page with a switch to toggle notifications for the application. I tried to link the switch to ViewController2 using an #IBAction, but it keeps connecting the object to Exit, instead of ViewController. I am getting really frustrated because the #IBAction won't stay connected. I am not sure what is going on, but if I could get help that would be amazing.
The images are linked down below to Imigur.
This is the connection to ViewController2
This is what occurs when I connect the IBAction to ViewController2
You can see there is no action option for the connection in this image.
When you attach #IBActions (or #IBOutlets), you do not want to attach them to Exit on the View Controller.
Follow these steps to add an #IBAction:
1) Make sure your View Controller's class is linked to your file, then go into the Assistant Editor making sure you selected Automatic.
2) Hold Control (^) and drag the button to your ViewController class.
3) Add the button as an #IBAction. - It's also recommended to use UIButton as the sender, instead of Any.
It should look like this:
If you have questions, let me know!
The problem might be that you are supposing you can form an action connection from a UISwitch in the scene of one view controller to a different view controller. You can't. You can form outlets and actions only between a view controller and the interface inside the same scene of the storyboard.
Click on the top bar of your problematic ViewController (the overview of what is getting displayed on screen), click the top left icon (called View Controller).
On the right click "Show The Identity Inspector" icon and you should see class. Change the text to 'ViewController'. Hopefully it should auto fill.
This will solve the problem.

"No #implementation found for the class" error in Storyboard in Swift

I'm trying to create in IBOutlet in Swift using my Storyboard and assistant editor, but I'm receiving a strange error I've never seen before. Looks Objective-C-ish.
I navigated to my ViewController's Save button in my Storyboard
Then I control-click dragged the Save UIButton to Xcode's assistant editor to make the IBOutlet. I get the error "Could not insert new outlet connection: No #implementation found for the class "ClassBVC". How do I avoid this error so I can make the outlet and an action?
I noticed when I click the button with the four squares before "Manual" and the < > buttons, I can go to "Counterparts" and there's a "ClassBVC" file with the same name, but with "(Interface)" next to it. It's not the file that I need though. Even when trying to create an outlet there, I get the same error.
I had same problem. rebuilding works for me.
Try closing Xcode and opening it again.
Creating one #IBOutlet directly in the view controller, and then creating the reference to the component in the Outlet Section in .storyboard worked for me. After that, I was able to drag the components normally to their respective view controller.

Button in swift to connect to a random storyboard (Xcode version 8.2.1

I am new to swift / Xcode and I am trying to create a button in Xcode that can randomly transfer to a different storyboard. I tried following the instructions in this post to do it (I also tried other ideas, but this answer was the most intuitive)
How to Segue Randomly to ViewControllers in Swift?
I've written the IBAction and connected the extra storyboards with different segues to the root view controller but when I start the simulation nothing happens when I click the "go" button. I cannot connect this button with the IBAction by a control drag, so I think that may be the problem.
I've tried creating new projects, connecting the "go" button to the root view controller, and editing the classes (it doesn't let me). What else could be wrong? My code is exactly like the answer to the link I posted. Is there a step missing to that answer?
First make sure you created a custom class for your root view controller. Also make sure you assigned your custom class to the view controller in Interface Builder.
Try dragging from your button to the view controller code pane and let XCode create the IBAction stub for you. Now touching the button should trigger your IBAction.

iOS Storyboard How to access the controls and add event handlers, and binding data to controls added on Storyboard

I used to create the UI thru code. But now I have to use storyboard. I am confused about how to add the event handlers to the controls added on the storyboard and how to bind the data dynamically to the controls added on storyboard. A sample scenario is An UIView is added on the storyboard and two UITableViews and a button are added on top of it. I want to add event handler to the button and bind data to the table views. How do I do this. If I subclass the UIView added on the storyboard will I have access to the controls(button, two table views) added on top of the view or how else I should achieve this ?. Please help !
Adding an event handler to a button is relatively simple. In your UIViewController subclass simply add a method similar to the one below, then in your interface builder select the viewcontroller and on the right side panel click the right most button at the top which looks like an arrow pointing to the right. under received actions drag the circle to the button that you want to perform the action.
-(IBAction)doSomething:(id)sender{
//code for doing what you want your button to do.
}
A separate way you could do it, if you still want to do it programatically is to do the same thing you're used to doing, except in your .h file add IBOutlet UIButton *buttonName; and in the right pane under outlets you'll see your button. which can then be referenced by name within the .m file.

XCode auto code generation

I'm new IOS and XCode. I added some bottuns to my IPhone app and now I want to add to them functionality.
I found this tutorial.
The problem is that when I drag the button to ViewControler.h it bounces back.
Please advice.
Thanks,
Nahum
press Ctrl button + drag the your button to viewController.h then "Insert Outlet and Outlet Collection" will be appeared. After that you remove your finger on mouse you will get Outlet box regarding connection. It has connection, Name, Type properties. I think it will be helpful to you.
You can make UIButtons or any UIControl one of two ways
By Programmatically
By .xib file
I think you are talking about the second method.
In the second method you take UIButtons in class:
#interface className : UIViewController{
IBOutlet UIButton *buttonName;
}
Now you will have to click on the .xib file and then you have to select a file below responder; after that open connection connection inspector. Now you can drag and drop UIButtons on the .xib file and establish a link between displayed UIButton in connection inspector and .xib's UIBUtton.