Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I wrote a custom button which is basically a NSView subclass. The button reacts on mouseDown() and should run a selector/action.
I don't know how to add a target and action variable to the subclass (similar to user interface elements like NSButton). I tried
var target : AnyObject?
var action: Selector?
Also how do I run the selector with the target in my subclass?
The "target/action" pair is archaic and should be avoided. But if you really wanted to use it, have a look at the NSApplication sendAction(_:to:from:) method.
Or if your custom button extends NSControl, you can use its target and action properties and its sendAction(_:to:) method.
A more modern approach would be to provide a closure property for your button class and then call that closure instead of using a target/action pair. Using a closure is safer, cleaner, and probably makes client code of your button class easier to write.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to be able to reference other functions inside my script, and then call them. Something like when assigning an on-click event for the button, so I can just drag and drop it in the Inspector. Thanks
You seem to mean a UnityEvent
public UnityEvent someName;
and later on at some point
someName.Invoke();
Otherwise just go via your type like e.g.
public YourOtherScript example;
and then
example.YourMethod();
you might mean calling a method/function from another class?
Then you could try referencing to that class by doing :
private yourclassName yourClassReferenceName;
Public void methodname()
{
yourClassReferenceName.ThefunctionFromTheOtherClass;
}
After you can drag and drop this class on the button and call the methodname(your method).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am calling a function in a different class that simply removes a lottie animation. I do this by calling...
RedeemController().removeAnimationFromSuperview()
Which enters the function (as proven by a print statement). However the animation does not disappear from the view.
When I call the same function from within the class that the animation is defined, the animation is removed from the view as expected. Here is the function. Very simple.
fileprivate func removeAnimationFromSuperview(){
animationViewDraw.removeFromSuperview()
print("Entered")
}
I would expect this animation to disappear. Thanks for your help!!!!
You are creating new instance of RedeemController that has it's own properties, instead you have to use the instance that you currently have
myControllerInstance.removeAnimationFromSuperview()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have an application with multiple forms. One of the forms frmHistory can be accessed from two different forms, frmClient, frmChild by clicking on cmdHISTORY. FrmHistory has a button called cmdBACK on it. What I'm trying to do is, code the cmdBACK button so when frmHistory is accessed from frmClient, upon clicking cmdBACK it would go back to frmClient (same thing from frmChild). How would I go aobut doing it?
This may not be the best answer but it is the way I know how.
I would create a hidden control on the frmHistory, i.e. lblParent. I then assign that frmHistory.lblParent = "frmChild" or frmHistory.lblParent= "frmClient" from frmChild or frmClient. In the frmHistory you check for lblParent to know which form calls it.
I did a similar thing to what KD did.
I created a global variable called g_whichForm. Upon clicking on the HISTORY button in frmParent i made it g_whichForm="Parent" and g_whichForm ="Child" for child. So when I clicked back i check if g_whichForm = parent or child then go back to the correct form.
Thanks!
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am new to OOP and Swift. I have some code like this:
class ViewController: UIViewController {
var topMenuScrollView: UIScrollView!
topMenu_doSomething1()
topMenu_doSomething2()
topMenu_doSomething3()
...
}
topMenu is a scrollView I have to do many things with. I found I have to write a lot of code for this top menu in this view controller file. Therefore, should I create a new class (a new swift file) specifically for the top menu handling?
However, I only have one top menu. Is it correct to write a class for it? (because I won't have multiple instances)
What's the right way to arrange this?
The purpose of object-oriented design is as much code-reuse as it is code-comprehensibility. In your case, it sounds like your menu has a lot of detailed behavior which is specific to that menu. Well, imagine at some point you want to change or rewrite the menu; it will certainly be easier if that logic is clearly isolated.
It sounds like this is a good case for another class, but it's entirely up to you.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Do you think I should create a ViewModel class for each View class I have or I can use a ModelView class for several View class ?
for example :
-ModelView
-ItemViewModel
-View
-ListItemView
-AddItemView
In this way, I will have to instanciate twice "ItemViewModel" and I will to add in it some method usefull for only one view and other method usefull for the other one. That don't sound really great, but I'm not sure.
It is perfectly sensible to have one ViewModel for couple of Views that present slightly different aspects of the same Model.
A more typical approach would be to implement BaseViewModel that contains Properties and Commands common to all the 'aspects' and all the differences reflected by the derived ViewModels, ending up with one ViewModel per one View relation.