how do I add code for a button on a viewcontroller? - xcode

How do I add code for a button on a ViewController? For example, in VisualStudio, I can simply double-click on a button and it will take me to an auto-genned clicked handler for the button in the codebehind. How do I go about doing something similar to this in xcode?

Steps to generate button code.
1. Create button using drag and drop from the list of components.
Now click on this button(shown in second image )
It will automatically directs you in your code file.
But if some problems occurs to direct you jn your code file.
Follow this steps.
1. Click on your viewController where you added button. (In viewcontroller scene)
2. Click on the identity inspector check the class name.
3.Check same class name is there in the second part.
If its same than now you can simple using control + three finger and drag it to your .swift file it will open it like this.
Now select action in connection to get button click action. And remains outlet to get the button property.
But if you are not getting the same file select here and find your file name in this hierarchy. After getting file do same process again.

Just do what I say it's simple.
1.Open File Navigator and Select your View's viewController.h file.
2.Now left click on your button and click+control drag to viewController.h (make sure you drag between #interface ---- #end).
3.Give name to your method and make sure You select Action in Connection option.
enter image description here
4.Click connect and you will have your method declaration on viewcontroller.h .
5.Now switch to viewController.m and there you'll find your method at the end and there you will write all the defination of your method.
enter image description here

Related

How to connect a button's Sent Action in Xcode for a custom Automator Action project

I'm in the Interface Builder of Xcode, creating an Cocoa-Applescript custom Automator Action. I have a Button and a ComboBox menu in the interface. I'd like to fill the content values of the ComboBox's menu when the user clicks the Button.
I've created the
on buttonSentAction_(sender)
-- set popupMenuContentValues of my parameters() to aList as list
my popupMenu's addItemsWithObjectValues_(aList)
end buttonSentAction_
handler in the applescript file, but when I ctrl drag from the button to the File's Owner, the File's Owner does not highlight for me to drop the connection. What I'm expecting is for it to drop and give me the option of choosing the buttonSentAction_ handler to receive the sent action. If I right-click on File's Owner, the received action handler I've created in the Applescript controller file does not show up. (Note, I'm still unsure about the correct line to populate the ComboBox Menu inside that handler, too.)
I can see in the "FM to Named Text Boxes" sample Automator Action project at macosxautomation.com has a button in the IB where you can see in the Bindings Inspector that the button's sent action is in fact connected to the File's Owner, and that the applescript file has that matching handler. Also, I the controller for File's Owner is by default set to the applescript file.
I'm missing something specific about hooking up sent actions in an Automator Action Project, obviously. Any help?
Update: I got it to work.
The key was that you have to create an Outlet for the object before you can then bind to a Sent Action Handler.
I deleted the button and started over, with a new naming scheme. This time, the File's Owner received the drag, and everything connected to the sent action and it works as expected. I did things exactly as I had before, so it is a mystery why the first action handler could not receive the Sent Action in IB.
Sample Code below:
on searchTypeMatrixWasClicked_(sender)
-- called with the matrix sent action
set theIndex to (actionTypeIndex of my parameters()) as integer
if theIndex is 0 then
-- do stuff, etc.
else if theIndex is 1 …
end if
end
Maybe this will help you.
I know that it does not work by control + dragging to File's Owner for some reason, but you can connect to the File's Owner by going to the bindings inspector.
This is not exactly what you are doing, because you want to send information to a handler, but I think the trick is to use the Parameters Object. You have to create a key to hold the value, and access it with AppleScript using get |keyname| of my parameters() as integer, (or string,list) for example. see The Structure of the on run Command Handler.
Below I provide an example of how I connected a property to a popup button.
See pictures:
I haven't worked with Automator projects to connect actions to custom objects in Interface Builder you either:
Set your existing object as the owner in instantiateWithOwner:topLevelObjects:. Then set the class accordingly in the Nib file.
Instantiate a new custom object by dragging a NSObject to your Nib, it will show up below the "Parameters" object in your screenshot. Then set it's class to any object that you want IB to instantiate. You should also retain this object with an Outlet.

Link a button on a site to specific view

I have created two views in my main storyboard ->
Now I added a button on the first one which should link to the other one (so when I press it it should lead to the other view). I have id't the one I want to link to with SceneViewController.
I copied the following code (and defined the method in the header) ->
- (IBAction)button:(id)sender {
SceneViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"SceneViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
But nothing happens when I press the button. Anybody a clue on what I'm doing wrong?
Thanks, cheers!
It's not enough to just copy the IBAction code. You have to link the button in the storyboard to the IBAction method you created in your header file.
Open your storyboard, click on the Assistant Editor icon at top right of screen (little icon of a waistcoat and bow tie). Make sure the right pane has your header file selected, if not you can chane to it in the drop down at the top.
Then zoom in on your button, right click on it and drag to the IBAction line in your header file. This should create the connection for you.

Standard Back Button in XCode (XIB)

I can't get the standard back button of iOS into a navigationBar because I can't find it in the Object Library, so can I do it with code or something else?
I just want the normal, standard, blue back button - you know which I mean.
To "automatically" have a back button you need first have a UINavigationController. Then you need to take a different UIViewController and add it as the root view controller in UINavigationController's init method:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:someOtherViewController];
Be sure to also set a title for someOtherViewController, usually in it's viewDidLoad or initializer. I'll tell you why this is important in a second:
self.title = #"Some other VC";
Then take a second UIViewController and push it onto your navigation controller:
[navigationController pushViewController:anotherViewController animated:YES];
You now have two UIViewControllers on your navigation stack: someOtherViewController and anotherViewController.
Your view will now have a back button with "Some other VC" in it. This is the title of the view controller that was just moved out of view:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
http://simplecode.me/2011/09/04/an-introduction-to-uinavigationcontroller/
I would also suggest reading up on how UINavigationControllers work and searching this site a bit more for customizing the back button. There are plenty of threads about it.
You can't add the back button yourself. The back button is part of the Navigation controller. If you embed a Navigation controller into your view(s), the back button will appear and be populated by the name of the previous view.
If you're using storyboards select your view controller, then in top menu choose "editor" -> "embed in" -> "navigation controller".
Edit: Here is an exmaple.
I'm running Xcode 7.2. This was driving me crazy, but I figured it out. Here are all the pieces you need to make the Back button appear (make a test project to prove it):
1) You have to have a Navigation Controller and it has to be set to be the initial view controller. So add the Navigation Controller, you will import two tables. Click on the Navigation Controller and on the properties list, check the box that reads "Is Initial View Controller". You will now see and arrow pointing to this view.
2) In our case we want a ViewController and not the included / connected TableViewController, so delete the TableViewController (RootController) and add a new ViewController.
3) Connect the Navigation Controller to the new ViewController by clicking on the top bar of the Navigation controller and orange circle with the arrow pointing left. Hold the Control button on your keyboard down and click and drag from the orange circle to the ViewController and let go. When given the list of options on how to connect the two views, select 'root view controller'.
Done! Now you the functioning navigation bar and you automatically get the back arrow on all segues added. Test this. Add another ViewController and connect to it with a button on the existing ViewController. Use the Control-click-drag approach from the button to the newest ViewController. Select the 'show' option for the new segue you created.
Run it. You'll see the back option has automatically appeared when you click the button and moved to the newest ViewController.
This is all provided by the Navigation Controller, but only when you make another controller the RootController. Happy navigating!

Can't ctrl+drag into ViewController.h from second Viewcontroller

I am building an iOS app for fun and here is where I run into trouble. I can insert an outlet and action in the ViewController.h files directly from my first View Controller through the ctrl+drag method; however, when I try ctrl+drag on the second ViewController it will not allow me.
Ctrl+drag on first ViewController
Ctrl+drag on second ViewController
You have the wrong document open in the assistant editor. It should be ViewController.h, but you are displaying UIViewController.h.
Check you have correctly set your second view controller to your custom class ViewController using the Identity Inspector (third of the right hand utility panels) then make sure it's header file is the document you are displaying on the right.
update
From your comments, you are having difficulty setting the second view controller to a custom class.
Here is how you select it in the storyboard. Note that you are selecting the View Controller, not it's topmost View
Copy and paste the class in the field "Class." You then need to press ENTER to take effect. Example

2 Action Connections in IB to one Button

I have an xcode app that I am building using IB for mac. THe app launches with windowA already opened. There is a button on WindowA that when pressed I would like to open WindowB and Consequently close WindowA.
I have found in the tutorials that there can only be 1 sent action connection per object, so all I can do at this point is have windowA close, or call WindowB to open on top.
Can anyone tell me how I can accomplish both using the same button?
Well, actually, you can call a single method. But within this method, you may accomplish several things. For example close a window and open another.
It's not necessary to create a complete class - which would correspond to a .m file. You just add another method
- (IBAction) doIt:(id) sender
to an existing class file. Connect this to your button in IB. Therefore, you set "File's Owner" to the class where your IBAction is (or, better, put the IBAction method in "File's Owner" class file).
In interface builder, select menu item File>Reload all class files
Now, right click "File's Owner" in interface builder. You should see your action there. Drag the round circle on the right of the popup to your button. Now, each time your button is clicked, the method should be called.

Resources