asp.net vs. xcode button click - xcode

I just started learning objective c and iphone development. Coming from an asp.net and web development, I am so lost with xcode.
In asp.net, you create a button, then you can simply click on it and it takes you right to the button event. Vs. Xcode, you create a button, then you will have to create an outlet in the header file, property, IBaction. Then in the implementation (.m) file, you create the click event method for that button. After that, you have to create an outlet reference of that button touch to that event by draging...
Just out of curiosity, is there any shortcuts or easier way to do this like asp.net? Without having to create outlet, property, etc. If there are any ways to eliminate some steps would be helpful.

If you want to perform action only click button you don't need to declare UIButton IBOutlet.
Just simply drag UIButton object on your view then declare & implement IBAction method in your class. Then make connection between action and button.
There is not shortcut in xcode for this if you want to interact with interface objects in your controller class you need to declare them as property and implement using #synthesize

Related

Linking actions to buttons on a modal/popover segue (swift 2 Xcode 7)

I'm sure I'm asking a simple question but I've only just started coding... so take pity on me!
I'm trying to figure out how to connect buttons (actions) to the viewController.swift from a modal/popover segue (I think the solution is the class but whenever I change it I get an error).
Eg.
In the storyboard, on the viewController interface, I have a button for sharing files. When I click it, a popover segue appears with two buttons on it, one for Fb one for Twitter, but I can't connect any actions from them to the viewController.swift
In another project I made a Google+ login and connected it to a modal segue but I couldn't make that work either.
Is segue the wrong thing to use?
Thanks for your help!
Marie
Your question would be easier to answer if you'd provide the actual error you're getting when you try to change the class.
It sounds like you're trying to connect the popover's buttons to actions in the presenting view controller (that is, the view controller that presented the popover), rather than the view controller of the popover itself. If that's the case, then that's the problem. You can only connect your buttons directly to actions available in the current scene.
This means you need a custom class for the presenting view controller (the one with the button that segues to the popover controller) and one for your popover. Set each scene view controller's classes to the appropriate custom classes you created (which must be a subclass of NSViewController or one of its subclasses or you won't be able to set the class name in IB) and you should be able to drag connections.

Accessing menu items from view controller in a storyboard, OS X, swift

I am building an OS X app in Xcode using storyboards. I have found that working with menu items is difficult and non-intuitive. The menu items are in a different scene (the application) than the view controller, and can not be connected to the view controller by the usual control-drag methods to create an outlet/action. I need to access menu items from the main view controller. I have figured out how to connect actions via first responder chain. However, I also would like to enable/disable menu items from the view controller, and I see no way to create an outlet to be able to do this.
I am new to OS X programming. I feel like I am missing something very basic here. I have watched several WWDC videos related to storyboards, and have not seen a good example showing the proper way to work with menus. Any help would be most appreciated.
There are several ways to work with menus. The traditional Cocoa way is to add an action method to First Responder proxy in IB.
Then flesh out the same action method in the NSResponder subclass that you want. Declare it in the header too.
This will then receive the action message via the Responder Chain.
Read up on the Responder Chain.

In Xcode, how do I create an outlet for a button that is inside a container?

I'm a total newbie with xcode and swift, trying to wrap my head around ios programming.
I'm designing a storyboard for my app. The storyboard uses containers to keep track of the controls. In one of the containers resides a button. I want to create an outlet for it to add some code when it is clicked.
If the button would be on the base viewport of the storyboard, I would control-drag a blue line from the button to the source window with my UIViewController subclass file, and it would assist me in generating the code. But for some reason when the button is in a container, this just doesn't work.
When following the documentation, it says to open the assistant editor when the button is selected and it should open the relevant file. So it open an objective-c file, but when I try to control-drag into it, it informs me that I do not have write permissions. Also I feel like I should be doing it in a subclass instead.
I have searched online a lot and tried everything I can think about, but nothing has worked so far. How does this work? Can I do it programmatically or so perhaps? I hope someone can straighten out this question mark...
A container view is intended to represent an area that will host a view from a different view controller that becomes a child of the view controller that owns the container. Usually, you would create a second view controller, link your container view to it using an "embed" segue, and then put your buttons and such in the second controller's view. The code behind those would then go into the second controller.
If your purpose is simply to have superviews to control layout within a single view controller, use a UIView rather than a container and the problem goes away. That's what the Editor->Embed In->View menu item is for.

Where to put MainMenu.xib selector?

I have a new project I am working on in Xcode 5.1 and I have been using Interface Builder to create and bind most of my layouts. I customized the default MainMenu.xib to contain the menu items I want but I cannot figure out how to bind the selectors for each menu item.
The project has a MainWindow.xib and a MainWindowController that is used by my app delegate and the controller is where I would expect the menu selectors to go, but I don't know how to do that.
The drag-and-drop connections panel will not work with the controller.
The MainMenu.xib's owner is the NSApplication, which doesn't seem right for the selector.
How do (and where should) I handle the MainMenu.xib actions?
Note: my question is very similar to this one but since I have done everything in IB and the accepted answer is in obj-c, the answer is of no use to me.
In my own project, I have a MainMenu.xib and a MainWindow.xib file.
MainMenu.xib is the default starting xib file when one isn't using a storyboard for an app. That's why NSApplication lives in there, it needs to be instantiated.
My MainWindow.xib is owned by my own MainWindowController implementation.
For the various menu items in MainMenu.xib, I am pointing those items at my AppDelegate (which is the true file owner for MainMenu.xib), and with those IBActions I'm firing off methods in other objects, notifications to other objects, or instantiating objects to get whatever task done that's associated with that menu item.
If you have additional classes & objects instantiated within MainMenu.xib, you can add IBActions in those classes and connect the menu items directly to them.

Cocoa custom view for NSMenuItem

I am developing a small app that display the active mounts in a menu in the NSStatusBar. So far it looks like this:
I want to add an eject button to the right of each menu item (like the left bar in the Finder). I know that I have to create a custom view and set it with the setView: method. The problem is that I am very new to Cocoa and right now I don't know how to dive into the custom view topic. Actually, I programmed a lot but never worked with interfaces so far :). Does anyone of you have a good tutorial for adding a custom view to a NSMenuItem?
I think this app will be very handy because you can hide the mounted icons from your desktop. The problem is that you always have to go back to the finder to unmount a volume...
Thanks for any help or tips you have!
It doesn't have to be a custom view. It can just be a standard NSView that acts as a container for standard controls.
The tricky part for a newbie is making the view reusable. You'll want your own NSViewController subclass with a corresponding view nib/xib (set up with your name label and eject button). For each menu item, you'll instantiate a new NSViewController with the XIB ( -initWithNibName:bundle: ) and set its represented object to your mount point. Your view controller subclass will have all it needs to respond to the eject button click, set the label to its represented object's path, etc.

Resources