I have a scenario where i have 5 buttons which call the same method when clicked. These buttons are clicked in various conditions, but now i want to know how we determine that which particular button has been clicked, from the called method.
For example, i have been calling chocolate() method when i click the buttons, eclairs, dailrymilk, cadbury, snickers and kitkat. Now i will click anyof these buttons from the UI and i want to know which one is clicked. this event has to be handled in the chocolate() method only.
Please suggest me how can i implement this. I am using Adobe Flex 3
If you are not using the addEventListeners but are setting the click property in your buttons you could do something like that:
<s:Button id="snickers"
click="{chocolate('snickers')}"
label="snickers"/>
<s:Button id="kitkat"
click="{chocolate('kitkat')}"
label="kitkat"/>
private function chocolate(type:String):void
{
trace("button", type, "was clicked");
if(type == "snickers")
{
// do stuff
}
else if(type == "kitkat")
{
// do something else
}
}
if you are working with event listeners you could determine the buttons from their ids, for example:
<s:Button id="snickers"
label="snickers"/>
<s:Button id="kitkat"
label="kitkat"/>
// add your event listeners somewhere like in onCreationComplete
snickers.addEventListener(MouseEvent.CLICK, chocolate);
kitkat.addEventListener(MouseEvent.CLICK, chocolate);
private function chocolate(e:MouseEvent):void
{
// e.target is the component that has dispatched the event (a button in this case)
var type:String = e.target.id;
trace("button", type, "was clicked");
if(type == "snickers")
{
// do stuff
}
else if(type == "kitkat")
{
// do something else
}
}
Related
I have a large Dijit-based form with many Dijits in collapsible TitlePanes.
When the form validates, any invalid items hidden inside closed TitlePanes (obviously) cannot be seen. So it appears as though the form is just dead and won't submit, though, unbeknownst to the user, there's actually an error hidden in a closed TitlePane which is preventing the form processing.
What's the solution here? Is there an easy way to simply open all TitlePanes containing Dijits that are in an error state?
If validation is done by following, it will work:-
function validateForm() {
var myform = dijit.byId("myform");
myform.connectChildren();
var isValid = myform.validate();
var errorFields = dojo.query(".dijitError");
errorFields.forEach(fieldnode){
var titlePane = getParentTitlePane(fieldnode);
//write a method getParentTitlePane to find the pane to which this field belongs
if(titlePane) {
titlePane.set('open',true);
}
}
return isValid;
}
function getParentTitlePane(fieldnode) {
var titlePane;
//dijitTitlePane is the class of TitlePane widget
while(fieldnode && fieldnode.className!="dijitTitlePane") {
fieldnode= fieldnode.parentNode;
}
if(fieldnode) {
mynode = dijit.getEnclosingWidget(fieldnode);
}
return titlePane;
}
Lets say if the following is the HTML and we call the above validateForm on submit of form.
<form id="myform" data-dojo-type="dijit/form/Form" onSubmit="validateForm();">
......
</form>
Here's what I ended up doing (I'm not great with Javascript, so this might sucked, but it works -- suggestions for improvement are appreciated):
function openTitlePanes(form) {
// Iterate through the child widgets of the form
dijit.registry.findWidgets(document.getElementById(form.id)).forEach(function(item) {
// Is this a title pane?
if (item.baseClass == 'dijitTitlePane') {
// Iterate the children of this title pane
dijit.registry.findWidgets(document.getElementById(item.id)).forEach(function(child) {
// Does this child have a validator, and -- if so -- is it valid?
if (!(typeof child.isValid === 'undefined') && !child.isValid()) {
// It's not valid, make sure the title pane is open
item.set('open', true);
}
});
}
});
}
I haev a Document Based Application, and In the document view, I have a NSSearchField. In the search field, I have enabled the menu, which I get to show up, and I have associated actions with the menu item. One of the menu items is called "Match Case". I want to be able to put (and remove) a check next this menu item. When I attempt to do so, the menu does not show the check.
-(IBAction)searchMenuMatchCase:(id)sender {
NSMenuItem *smi = [searchMenu itemWithTitle:#"Match Case"];
if (searchCaseSensitive) {
searchCaseSensitive = false;
[[searchMenu itemWithTitle:#"Match Case"] setState:NSOffState];
} else {
searchCaseSensitive = true;
[[searchMenu itemWithTitle:#"Match Case"] setState:NSOnState];
}
[searchMenu update];
NSLog(#"SM State %ld",[smi state]);
}
The code gets executed, and I get a log message showing the state going from 0 to 1 to 0 to 1. But there is never a check next to the menu item. When I look at the menu item object while debugging, I do see the "state" value set to 0 and 1 before I toggle it.
Any suggestions as to what I am missing?
To get around this issue, I had to use the NSMenuDelegate and use the method validateMenuItem. This way the validateMenuItem method was called before the menu was drawn, and I could make sure the state was set correctly before the menu was drawn. I believe what was happening was that the menu item I was getting in the original code was not the actual menu that was being drawn, but just a template.
/**
This is called when the user selectes the "Match Case" menu item found in the Search field
*/
-(IBAction)searchMenuMatchCase:(id)sender {
if (searchCaseSensitive) {
searchCaseSensitive = false;
} else {
searchCaseSensitive = true;
}
}
/**
This is called Each time a menu is shown.
*/
-(BOOL) validateMenuItem:(NSMenuItem *)menuItem {
if ([[menuItem title] compare:#"Match Case"] == 0) {
if (searchCaseSensitive) {
[menuItem setState:NSOnState];
} else {
[menuItem setState:NSOffState];
}
}
return YES;
}
Double clicking fast on a button in Sencha Touch 2 when having a navigation view will on some Android device push the new view twice on the view stack, how to solve it? Doesnt happen on iPhone
If you're having problems with the single click, then wrap the event function in a delayed task... for instance:
tap: function(button, e, eOpts) {
if(this.delayedTask == null){
this.delayedTask = Ext.create('Ext.util.DelayedTask', function() {
this.myFunctionToCall();
this.delayedTask = null;
}, this);
this.delayedTask.delay(500);
}
}
So in the example above, if another tap is registered and this.delayedTask has yet to be null, then it will not create the new delayed task which ultimately calls whatever function you need after 500 miliseconds... hope this makes sense, it's also a way to create double tap events on buttons...
This issue is a bit old but I was facing the same issue in the app I'm working on in my company. This is especially frustrating when buttons are bound to an Ajax call.
I took back Jeff Wooden's solution to override every button in my app :
Ext.define('MyApp.override.CustomButton', {
override : 'Ext.Button',
doTap: function(me, e) {
if(this.delayedTask == null){
this.callOverridden(arguments);
this.delayedTask = Ext.create('Ext.util.DelayedTask', function() {
this.delayedTask = null;
}, this);
this.delayedTask.delay(500);
} else {
console.log("Preventing double tap");
}
}
});
How does it works?
Each button action will trigger a delayedTask which will intercept button action for 500 ms, therefore preventing doubletap
This will work for 'tap' and 'handler:', which both pass through 'doTap' method
this is linked to the current button so it won't reverberate on other buttons
To use it simply add it in your app.js 'requires' block
Ext.application({
requires: [
...
'MyApp.override.CustomButton',
...
],
Helpfull Sources :
https://www.sencha.com/forum/showthread.php?173374-Ext-override()-on-Ext-components-in-ExtJS-4-x
Best practice for overriding classes / properties in ExtJS?
this post
ItemClick event is of gfx.control.ScrollingList.
This ScrollingList MovieClip has two child buttons.
I'd like to handle these children's click event.
private function configUI() {
super.configUI();
// MyList : ScrollingList;
MyList.addEventListener( "itemClick", this, "OnListItemClicked" );
}
private function OnListItemClicked( e : Object ) {
// how???
// e.renderer?
}
e.renderer is of type "MyListItemRenderer extends ListItemRenderer".
if you're using CLIK AS3 the event should actually be of type
scaleform.clik.events.ListEvent
It includes the 'itemData' for the clicked list item (the data you gave the list which corresponds to the entry the user clicked on) and 'index' the index that was clicked. Also, by default the list marks as 'selected' the item that was clicked, so you could just ask the list:
private function OnListItemClicked( e : ListEvent )
{
var myDataObj:Object = e.itemData;
var clickedIdx:int = e.index;
// or
clickedIdx = MyList.selectedIndex;
}
If you're using the AS2 CLIK I imagine it is similar, but I've not used that version so can't say for sure.
I am beginner of iPhone. I have create five button. button click event perform go to other class in that how to call button click event method..?
it is in ViewController class
(IBAction)onclickbutton1
{
}
(IBAction)onclickbutton2
{
}
and this onclickbutton1 and onclickbutton2 method how to call in other class in if condition
so, that condition of onclickbutton1 is true perform onclickbutton event
Just add a test within the event handling methods:
- (IBAction)onclickbutton1
{
if (conditionIsTrue)
[OtherClass doThing1];
else
[OtherClass doThing2];
}
- (IBAction)onclickbutton2
{
if (conditionIsTrue)
[OtherClass doThing1];
else
[OtherClass doThing2];
}