How can i create `IBOutlet` or `IBAction` in custom class
Look at Apple's Cocoa tutorial documentation that comes with Xcode 4, this is rather fundamental to the Xcode/Cocoa/interface designer model!
In outline:
@interface MyCustomClass
- (IBAction) myButtonClickAction:(id)sender;
@end
@implementation MyCustomClass
- (IBAction) myButtonClickAction:(id)sender
{
NSLog(@"My button has been clicked");
}
@end
Now in the interface designer (just open the .xib file in Xcode to get the designer) you need to:
- Add an instance of
MyCustomClass
- select Object from the Object Library and drag it onto your Objects (usually on lhs of canvas) or the design canvas (it will just go to Objects and not create a graphic widget on the canvas). Now select the added Object and in the Inspector (usually on rhs of canvas) set the class toMyCustomClass
. Now when your application starts up an instance ofMyCustomClass
will be created. - Select your
NSButton
on the design canvas, select the Connections tab in the Inspector. Click and drag from selector under Sent Actions to yourMyCustomClass
under Objects. On releasing you'll get a menu ofIBAction
s to select from, pickmyButtonClickAction
. - You'll probably want to add an
IBOutlet
to your application delegate to link to the custom object instance that has been created, if you don't you won't have a direct way of accessing it. The process to do this follows the same pattern as for theIBAction
above.
That is it, in words (pictures help), and very briefly.
Now go read those tutorials!
Creating outlets and actions on object from Interface Builder
The class actions and outlets panes have been moved to the library window in newer versions of Interface Builder
Click on the Classes tab, then search for your class.
Click on your class, then use the drop-down menu to select outlets or actions.
Use a generic class as a custom view in Interface Builder
Interface Builder "talks" to your code through the ObjC runtime. As such, IB can can access only features of your code that are representable in the ObjC runtime. ObjC doesn't do generics, so there's not a way for IB to tell the runtime which specialization of your generic class to use. (And a Swift generic class can't be instantiated without a specialization, so you get an error trying to instantiate a MyButton
instead of a MyButton<WhateverConcreteType>
.)
(You can recognize the ObjC runtime at work in IB when you break other things: Attempting to use a pure Swift class with outlets/actions in a nib/storyboard gives runtime errors about ObjC introspection. Leaving an outlet connected whose corresponding code declaration has changed or gone away gives runtime errors about KVC. Et cetera.)
To ignore the runtime issues and put it in a slightly different way... let's go back to what IB needs to know. Remember that the nib loading system is what instantiates anything in a storyboard at runtime. So even if the parts of your class that take a generic type parameter aren't @IBInspectable
, the nib still needs to know what specialization of your generic class to load. So, for IB to let you use generic classes for views, IB would have to have a place for you to identify which specialization of your class the nib should use. It doesn't — but that'd make a great feature request.
In the meantime, if it's still helpful for your MyButton
class to involve some generic storage, perhaps you could look into having MyButton
reference another class that includes generic storage. (It'd have to do so in such a way that the type of said storage isn't known at compile time, though — otherwise the type parameters of that other class would have to propagate back to MyButton
.)
How to define Outlets and Actions in the Classes Pane in Interface Builder in Xcode 4?
This does appear to be removed from Xcode 4, but there isn't much need for it anymore since Xcode 4 integrates the editor directly with IB. The feature never really worked that well in Xcode 3 IMO anyway.
In Xcode 4, display the header for your object by selecting your object and View>Editors>Assistant. Now control-drag from the object you want to connect into the header. This will let you automatically create an outlet or action and bind it all at once.
See the Xcode 4 Transition Guide for more information.
How do I attach an action to a button using Interface Builder?
Setting up an Action
- Right-Click on your control instance
- Drag to your target and let go.
Setting up an Outlet
- Right-Click on your object instance
- Drag to your control instance and let go.
Inspecting Actions/Outlets/Bindings
- Right-Click on your object instance
Connect IBAction to custom class
change PressableView
parent class as UIControl
class then you can connect for actions and handle it.
UIControl
is subclass of UIView
class only. so you will have all the properties of UIView
as well.
class PressableView: UIControl {
@IBAction func uicontrolEventAction(_ sender: Any) {
}
}
Related Topics
Dictionary Becomes Nil When Trying to Pass Back Cllocation Object to iOS App Extension
Swift Video to Document Directory
Tvos Button Inside Navigationlink Is Not Working
User Specific Avatar in Jsqmessagesviewcontroller with Sdwebimage
Error Domain=Nscocoaerrordomain Code=3840 "Invalid Value Around Character 0
Is There a Simple Way to Test If You Match One of a Set of Enumerations
Swift Method Parameters in Documentation
Error Domain=Nsosstatuserrordomain Code=-12780 \"(Null)\"
Getting Reference to a Dictionary Value
Swiftui Remove Transition of Fullscreen Cover
What Is The Reason to Store Subscription into a Subscriptions Set
Swift: Guard Let and Where - The Priority
Secidentity + Force Cast Violation: Force Casts Should Be Avoided. (Force_Cast)
Swift: Trunc a Floating Number to Show It in a Label