How to trigger a binded action from a button? - swift

I have a subclass of NSButton, but I don't want to binded action to be triggered when user will make mouse down/up on the button.
How can I trigger that from another function?

Just change the function declaration to something like this:
#IBAction doSomething() {
// ...
}
You can both connect it to your NSButton instance, as well as call it from your code with doSomething().

Related

How do I offer a "sent action" from a custom class?

I'm developing a custom class in Swift based on NSObject. It's a statusMenu icon/menu helper. When I receive an event for the icon being clicked in my custom class, I want to pass this on in the same way an NSButton allows to create an IBAction to respond to the user clicking the button.
How do I do this?
Code:
I'm registering a selector in my class to listen to clicks:
statusItem.action = #selector(statusBarIconClicked)
The selector receiving this:
#objc func statusBarIconClicked(sender: AnyObject) {
print("clicked clicked!!")
// pass sent action on through a new sent action... how?
}
I want this to be linkable to the user in the same way a button can lead to this:
#IBAction func myClassSaysMenuWasClicked(_ sender: Any) {
// Reacting to that
}
Googled for a good while and found: nothing.
I take it that you're asking about this sort of thing, displayed in the Connections inspector (this is iOS, not macOS, but it's the same idea):
The question would then be: when the user selects an instance of my class in the nib editor in Xcode, I'd like those Sent Events to appear in the Connections inspector so that the user can hook up one of them and use the target-action architecture.
You can do this only if your class is a Control subclass. Thus, for example, in iOS, a custom UIControl subclass displays those sent events in Interface Builder.
If you can't do that, then the programmer won't be able to configure your target-action in Interface Builder. You can certainly implement a target–action architecture, but the programmer will have to set the target and action in code. (You could do half a job of it by making the target an outlet, of course.)
I worked around the comment above and googled further. I found the solution being to change from NSObject to NSController in this line:
class StatusMenuController: NSControl, NSMenuDelegate {
And run this command when I want to trigger the sent action:
if let theAction = self.action { NSApp.sendAction(theAction, to: self.target, from: self) }
The if-command of course checking so that an action is actually set before trying to use it.
I found no ways during my research to add any further sent actions. The way to go here seems to be delegates.

Performing two actions with one button at the same time

Im creating an application where I want to perform two actions at the same time with one button like it is said in the title but I don't have any idea to do that.
Here is a part of my code:
//Function Test button: animate the button while playing the song
#IBAction func testButton(sender: AnyObject) {
self.ButtonAudioPlayer.play()
animView.startCanvasAnimation()
}
Thank you for your answers !
You can call the other action from within the first action. For example, I want a button to perform its own action plus the action of another button:
#IBAction func buttonOneAction(sender:AnyObject){
self.doButtonOneThing()
self.buttonTwoAction(sender)
}
#IBAction func buttonTwoAction(sender:AnyObject){
self.doButtonTwoThing()
}
When button one is pressed it will do something then it calls button two's action, preforming both. #IBActions are just functions like any other. #IBAction is strictly used by interface builder.

Xcode 7.2.1 Making a Reset Button using a Button with Swift

I am trying to make a reset button for my app that will reset the UI to the original state. I have made a UIButton and linked it to the ViewController, but I have no idea where to go from here. I tried using the following code:
#IBAction func resetToOriginalState(sender: UIButton) {
self.resetToOriginalState (sender: UIButton)
}
It gave me the following error:
Editor placeholder in source file
Sorry if there may be an obvious answer, but I am very new to Swift and Xcode.
Is there any other way to create a reset button?
The error:
Editor placeholder in source file
Is because you are calling a function with the UIButton Class name instead of the actual button.
#IBAction func resetToOriginalState(sender: UIButton {
// this line is wrong, you shouldn't have UIButton in here
self.resetToOriginalState (sender: UIButton)
// the line should read
self.resetToOriginalState (sender: sender)
}
This way, you are passing the actual button into the function that was passed to resetToOriginalState
Seems you have too IBAction for the same button, check how many time you have #IBAction func resetToOriginalState(sender: UIButton) in your code and remove the references from the references Interface list to clean it, should there be only one :
It depends what is in the scene and what do you need to reload. As far is I know you can't really segue a ViewController to itself, but here are few options:
Try to add loadView() when the button is pressed
Duplicate the view controller, and segue between the two. (might be risky and create more work)
Reset your variables to their initial state when the button is pressed
You should give us more detail because this is implementation specific.
Nevertheless, it's not very clean, but depending on the architecture of your code, you might be able to generate a new instance of your view controller, destroy the current one, and present the new one.

Simulate pressing a button in swift

I have the following method
#IBAction func clearDisplay(sender: UIButton)
and I want to overload this method so that I can clear the display if some conditions are satisfied in my code. So
func clearDisplay()
My idea is to simply simulate pressing the clearDisplay button but am not sure how to do that. Does this sound like a good idea? How would I simulate pressing the button?
As Matt says, just call the IBAction method directly. You should probably change the object type of sender to AnyObject, and then use
clearDisplay(self)
Which will pass in the view controller as the sender.
In your clearDisplay method, if you have code that does something with the button, check it's type first to make sure it really is a button.

How can I determine whether my UIButton's event is Touch Down?

How can I determine whether my button's event is Touch Down?
I want to do a function like this:
if(users click on touchdown event)
{
NSLog(#"a");
}
else if(users click on touchupinside event)
{
NSLog(#"b");
}
Eather you set two different IBAction methods in the InterfaceBuilder or you set two different targets via:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
in your code while creating the button.
You "find out" by letting the button tell you when the event happens.
Add a method (or methods) like this:
- (IBAction)myButtonClick:(id)sender;
In Interface Builder, attach the method(s) to the events you're interested in.
You create a separate method for each type of event if you want different behavior for a TouchDown as opposed to TouchUpInside.
you attach each unique event to its own IBAction