Casting sender to IBAction as id or specific class - objective-c

I see two approaches to handling sender objects in IBAction statements. The first looks like this:
-(IBAction)buttonPressed:(id)sender{
UIButton*someButton=(UIButton*)sender;
//do something with someButton.tag or whatever
}
Another seems easier:
-(IBAction)buttonPressed:(UIButton*)sender{
//do something with sender.tag or whatever
}
I generally opt for version 2. Any particular reason to prefer one style over the other, if you know that only a button will be sending to this method?
I can see where version 1 is good if anything can be a sender, like a button, or switch or slider, etc. But if you are looking for UIButton properties like tag it won't make much difference if your sender is not a UIButton. So version 2 seems a lot more straightforward.
Just thought I'd see if I'm missing an obvious reason to prefer version 1.

I see no problem using the second version. I usually use the second version, only using the first version if the sender may be more than one type of object. Then, if the method needs to know what type of object, the method can query the sender before casting the sender to a particular type.
Even more frequently I find no need to access the sender, so I just use:
- (IBAction)buttonPressed {
// Do something.
}

Related

Disable control callbacks when setting values in code?

I've noticed that when a control such as an AutoSuggestBox has a callback, the callback is executed both when a user interacts with the control and when my code changes the associated value.
For example, if I set the TextChanged property on an AutoSuggestBox, the function is called even when my code sets the Text property to an initial value.
This is causing problems in my application in the form of both bugs and unnecessary function calls. You may be wondering how the code came to be in this state -- the answer is, I don't know. The project was handed off to me from another developer and I've been tasked to fix a number of bugs.
Although I can individually hunt down all the places in the code where this happens and temporarily remove the callback, I'm wondering if there is an easier way, for example a property I can set on the control that says, "don't call the callbacks when it is the code making a change rather than the UI".
For the AutoSuggestBox.TextChanged event, the attribute of the trigger reason is provided in AutoSuggestBoxTextChangedEventArgs, and you can judge based on this
private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
if (args.Reason == AutoSuggestionBoxTextChangeReason.ProgrammaticChange)
{
return;
}
//other code
}
For other controls, you need to deal with them according to your situation.
Best regards.

Identifying objects in IBM RFT

While executing my script in RFT, my script got failed due to the slight position change of a button. (This button's position slightly changes according to the option selected for previous combo box due to the label appearing near the button)
As there are 2 positions for this button in window, one of my script fails while other passes.
Please suggest how to identify this same object in 2 different places in RFT?
If you're alright with not using pre-mapped values and instead work with objects directly in code (which I've personally found to be extremely useful... it's allowed me to do great and wondrous things with RFT :), the following ought to work fine:
private void clickObject(String uniqueIdentifier) {
// Find object
RootTestObject root = RootTestObject.getRootTestObject();
TestObject[] matchingObjs = root.find(atProperty(".id", uniqueIdentifier));
if (matchingObjs.length > 0) {
// Click the object
((GuiTestObject) matchingObjs[0]).click();
}
// Clean-up
unregister(matchingObjs);
}
Feel free to replace ".id" with whatever property is best suited for the situation... since I work primarily with a web application, the ".id" property has worked splendidly for me.
Because the method finds the object anew each time, it'll grab the object's position wherever it's at at the time the method's called. The clean-up will also prevent any weird, horrible, and otherwise unfortunate UnregisteredObjectExceptions from cropping up.
Without looking at your pages I cannot be sure, but I think the buttons are actually two different buttons. Maybe they are generated by javascript, or they are just un-hidden after the option you select in the combobox.
If they are two different buttons (record them both and look at the recognition properties) you can either replace some properties with a regular expression or check wich button is visible/exists and then click it:
if (btn_button1.exists()) {
btn_button1.click();
} else if (btn_button2.exists()) {
btn_button1.click();
}
Here's a more complete tutorial on Object Recognition.
You can increase the tolerance of Rational Performance Tester AssureScript in the properties tab or you could set the description but hide the value. You can also make a custom code that updates the object map to prepare for this change in a java IF structure

Does every action method need a sender object?

Can I just omit the parameter completely? I cannot seem to find a use for it within my IBAction method.
Yes, you can omit it if you don't want it:
-(IBAction)action{
// some stuff
}
Although it can come in handy in a lot of situations
Source : Apple Doc

How can I use the FilterFunction passed to Gdk.Window.AddFilter?

I need to intercept several events before they are delivered to the widget's standard handlers, so I've done this already:
//Inside the definition of my custom widget
protected override void OnRealized()
{
base.OnRealized();
this.GdkWindow.AddFilter(PreFilterMessage);
...
}
So, later I define the PreFilterMessage method:
public Gdk.FilterReturn PreFilterMessage(IntPtr xEvent, Gdk.Event evnt)
{
Console.WriteLine(evnt.Type);
...
}
But the thing is that when I test it, whatever message gets to the window (KeyEvent, ButtonEvent, etc.) it always prints "Nothing", so I'm only getting empty events every time. Somewhere I read that the real information gets through the xEvent parameter, but that's just an IntPtr, so I don't know how to get the information I need (event type, pointer coordinates, etc.) from it.
Can anyone tell me how to do this? Thanks in advance.
Per the docs on the gtk.org website, the GdkEvent received in the filter func is unpopulated. The purpose of this AddFilter mechanism is to allow the user to intercept X events before the gdk event processing starts up. We do not bind any of the X data structures in Gtk#, so you would need to manually marshal that data from the IntPtr using System.Runtime.InteropServices Marshal.
So, unless that sounds familiar as far as what you are trying to accomplish, you may want to consider other alternatives.

Prevent child class methods from firing?

I am sure this question has been asked before, but I searched and couldn't find it so I apologize in advance for duplicating content here on SO.
That being said: In Objective-C, in an overridden method you can call the parent class method using something like [super methodName]
but how do prevent the rest of the code from executing in the child class from the parent? It could because it's Friday, but I stared at my monitor for a few minutes and couldn't get past it in my head.
Example (in child class):
- (void)methodName
{
[super methodName];
//Everything below this line shouldn't execute if tell it not to from the parent
NSString *aString = #"This should never be called.";
}
Help me out! I know there's a simple solution, but my brain just isn't picking it up today...
You could maybe have another function returning True/False which you use to decide if you wanna proceed with the remaining code in the child. This 'control' function can depend on a variable set in the parent class
Having said that, it sounds like an interesting requirement to me. Maybe you need to take another look at your class design and hierarchy.
you could test the object type and determine if you do or don't want to run additional code for example:
if (![myObject isKindOfClass:[MyChildObject class]]
{
//run only superclass code
}
As I write this, I can't help but wonder if you don't have some issue with how you setup your classes though. If your design is spot on, you shouldn't have to go through these kinds of contortions.