When creating an eventhandler from a UIButton from the Storyboard, Swift adds #IBAction before func. When adding an event programmatically to a UIButton to a func, Swift gives a error and says I need to add #objc in front of my method, but when I add #IBAction, it compiles as well.
Are there an difference between the 2 and which should I use when adding an event to my UIButton programmatically?
The IBAction prefix already exposes the method to the Objective-C runtime, so there is no need to prefix it additionally with #objc.
Since IB stands for Interface Builder, it's unnecessary to use it when you create a button programmatically. All it does (besides exposing the method to Objective-C) is make Interface Builder list the function as an action in the Connections inspector.
Related
im kinda new to swift, and I don't understand what is the difference between the Interface Builder Outlet/Actions and programatically constructing a view. If I have IBOutlet why I don't need to still add them as subviews of the self.view and why do I don't need to instantiate them? As well, to not create two questions, why in the case of a creating a custom UiView I have to load the xib file with Bundle.loadNib?
Thanks,
You cannot initialize a property that is not nil by nature at instantiation time.
Interface Builder outlet, which always initializes after its owner. In this specific case — assuming it's properly configured in Interface Builder — you've guaranteed that the outlet is non-nil before you use it. That is why we can use as below:
#IBOutlet private var searchBar: UISearchBar!
Also, you already added subviews as well.
SearchViewController - ContentView - searchBar
That means ContentView or self.view is already superview of searchBar so you don`t need to add as a subview.
IBAction, IBOutlet, is responsible for connecting with objects on the Storyboard.
IBOutlet is a variable for accessing values, and IBAction can define an action on the event.
With Storyboard, prototypes can be created quickly and are easy to implement. However, if you work with multiple people, it can cause conflicts, so be careful.
I'd like to create a basic view controller class BasicController with two subclasses SubclassController and AnotherSUbClassController. BasicController controls view that have a button inside them, so there should be a
weak var buttonThing: UIButton?
property in it. In the actual implementation I want to use the subclasses in various parts of the interface: I want to draw them in storyboard, put buttons in that drawings, and connect the buttons to the inherited buttonThing property via an outlet. But wait, the buttonThing property was announced in the superclass and doesn't even appear in the code of the subclasses. So how do I connect the buttons to that outlet?
You can ctrl-drag the UIButton from your storyboard to your BasicController for each subclass you have laid out in your storyboard. You can then use the buttonThing in each subclass as usual.
Remember to link your subviewcontrollers to their respective viewcontrollers in the storyboard.
What is the better approach if we are creating a UIButton in the .xib file using the Interface Builder, is it better to use IBAction method to define its Action Method and the required Control Event, or should we use
[UIButton addTarget:action: forControlEvents:]
method to define the Action Method?
Which approach is better and why?
Yes. You can use any way.
Both of these methods begin with your existing eventreporter program. You’ll add a simple
UIButton to it using Interface Builder. Place the button a top the label at the bottom of your page and use the attributes tag to label it Reset. With it in place and defined, it’s ready to be linked into your program by one of two different ways.
Using addTarget:action:forControlEvents: with a button
On the one hand, you may wish to add actions to your button programmatically. This could be the case if you created your button from within Xcode or if you created yourbutton in Interface Builder but want to change its behavior during runtime.Your first step is bringing your button into Xcode. If you created your button inInterface Builder, as we suggested earlier, you need to create an IBOutlet for the but-ton, which should be old hat by now. If you didn’t create your button in InterfaceBuilder, you can do so in Xcode. This probably means using the factory class method buttonWithType:, which lets you create either a rounded rectangle button or one of afew special buttons, like the info button. By either means, you should now have a but-ton object available in Xcode.
Using an IBAction with a button
The other way you can link up actions to methods is to do
everything
inside InterfaceBuilder. This is the preferred choice if you’ve created your object in Interface Builder(as we’ve suggested) and you’re not planning to change its behavior at runtime.When you use this procedure, you don’t need to make your button into an
IBOutlet. It’s effectively invisible from Xcode, which is fine, because all you careabout is what happens when the button is pushed. You also don’t use the somewhat complex addTarget:action:forControlEvents: method that we just ran through; instead, you connect things via intuitive Interface Builder means.
It depends upon user which one to use.
Use xib to make easier and lots of code can be avoid to write.
Although any of the two can be used. The only reason I see it should be IBAction is because you have used xib. In this way you know that everything you have created is from xib. But once again, both are fine.
Just starting out with an iPhone application using xcode 4.2.
I understand that it is good practice to use a subclass of UIViewController for each view in my application, and I am able to write some basic code in these to test buttons etc on the associated view.
Now I want to perform some action on a text field in one of these views, let's say I will use the Value Changed event to log the textfield's contents on each keystroke.
Should I be creating some kind of UITextField controller subclass? Or do I deal with this kind of thing in the existing ViewController subclass that houses the textfield?
If the latter, how do I refer to the textfield in the view controller subclass, and make the connections?
You'll probably want your UIViewController to implement UITextFieldDelegate. Connections can be made via the Interface Builder and outlets or just setting the delegate on the text field and using the reference you get from the delegate callbacks.
You would use the existing ViewController subclass that houses the textfield. A good rule of thumb is one ViewController per screen of views (not including UINavigationController, Modal screens, uisplitviewcontroller, and popovers).
To refer to it, you would make a property in the .h of your custom UIViewController:
#property (nonatomic,weak) IBOutlet UITextField * myTextField;
Note the IBOutlet keyword. This will allow you to connect it in InterfaceBuilder (or your storyboard). To learn how to connect that I would recommend you watch a video about IBOutlets since its more of a visual thing.
I have a UIViewController, which is used in 2 different classes within same xib. I have created a uibutton in it and associated the action method in both the classes which are trying to use it.Both classes A and B have different outlets to that UIVIewController.
when i try to press the UIButton in the viewcontroller, both functions in Class (A) and Class (B) get called (since the event is on touch down) but the method names in each class is different.
Can I somehow check which class called the uiviewcontroller's button touch event?
Set a tag(different values) for Button from each class and in your button function first check the tag and execute respective action.
you can check with isKindOfClass if you have 2 classes