PDA

View Full Version : Multiple IBAction's?




Fritzables
Jun 25, 2011, 12:20 AM
Quick question.....

Can a single control have multiple IBActions ??

Pete



jiminaus
Jun 25, 2011, 01:39 AM
No. At least not at the same time. What are you trying to achieve?

Fritzables
Jun 25, 2011, 01:48 AM
G'Day Jim,

How's it go'n down there??

OK.... I have a TextField that I will need to evaluate every key entry as the end user types in a value - is this just a normal IBAction ??

Pete
Brisbane

jiminaus
Jun 25, 2011, 02:15 AM
G'Day Jim,

How's it go'n down there??

OK.... I have a TextField that I will need to evaluate every key entry as the end user types in a value - is this just a normal IBAction ??

Pete
Brisbane

G'day Pete,

I think you're mistaken about which way's up. :p

An action is not what you want if you want to react to each change.

If you want to react whenever the text field is change (be it by pressing a key, cutting/pasting text, etc.) then what you want to do is set up a delegate to the text field.

Connect the delegate outlet to whatever the nearest window/view controller. Then in the controller implement the following method.


- (void)controlTextDidChange:(NSNotification *)aNotification
{
NSTextField* textField = [aNotification object];
NSString* newValue = [textField stringValue];

NSLog(@"%s: %@", __PRETTY_FUNCTION__, newValue);
// TODO: React to changed text field
}


Jim

Fritzables
Jun 25, 2011, 02:22 AM
Thanks once again Jim for the guidance on this one. Much appreciated.

Go the Maroons :D


Pete

Fritzables
Jun 25, 2011, 11:06 PM
Jim,

Take a look at this link:

http://pagesofinterest.net/blog/2010/11/subclassing-nstextfield-to-allow-only-numbers/

Seeing that the NSTextField I want to use needs to handle a numeric number - would this example be the best way out for me??

Pete

jiminaus
Jun 25, 2011, 11:44 PM
Jim,

Take a look at this link:

http://pagesofinterest.net/blog/2010/11/subclassing-nstextfield-to-allow-only-numbers/

Seeing that the NSTextField I want to use needs to handle a numeric number - would this example be the best way out for me??

Pete

Hi Pete,

It seems to work.

I wouldn't use intValue/setIntValue. I would instead use integerValue/setIntegerValue so 64-bit numbers can be entered in 64-bit builds.

I also wouldn't call the delegate explicitly. The blog posters code is wrong because it doesn't check that the delegate actually responds to controlTextDidChange:. Because NSTextField's textDidChange: sends controlTextDidChange: to the delegate if appropriate, I would just let that code still do its job by call super.

So I would code the textDidChange:.

-(void)textDidChange:(NSNotification *)aNotification
{
[self setIntegerValue:[self integerValue]];
[super textDidChange:aNotification];
}


Another way would be to do Windows-style filtering of keyDown events. But then you'd also need to handle other ways of getting text into the text field, such as copy and paste.

Like the blog poster, I don't like the UX of trapping a user into a text field. Personally, I don't try to do input validation at the control level. I do it at the form level when the form is "submitted".

Jim

Fritzables
Jun 26, 2011, 01:23 AM
Afternoon Jim,

Settled with your code - works great - all I will need to do is convert the newValue to a float.

One thing I am failing to understand and hope you may steer me in the right direction.

In your code there is no reference to the TextField pointer I am using yet your code responds to my user input - how does this happen if I have made no reference in you code?

Pete

jiminaus
Jun 26, 2011, 01:36 AM
Afternoon Jim,

Settled with your code - works great - all I will need to do is convert the newValue to a float.

One thing I am failing to understand and hope you may steer me in the right direction.

In your code there is no reference to the TextField pointer I am using yet your code responds to my user input - how does this happen if I have made no reference in you code?

Pete

NSTextField inherits from NSControl. NSControl has getter and setters for the control's value as different types. See Setting the Control's Value (http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSControl_Class/Reference/Reference.html#//apple_ref/doc/uid/20000073-SW8).

Assuming you're talking about textDidChange:, self is the pointer to the NSTextField-subclass. "self" is equivalent to "this" in other languages such as C# and Java.

If you're talking about controlTextDidChange:, the NSTextField(-subclass) can be retrieved by [aNotification object].

Fritzables
Jun 26, 2011, 01:58 AM
So, what you're saying is, as NSTextField inherits the NSText object which has the textDidChange delegate which will post a notification when an end user taps a key.

Then the method you wrote will handle all messages associated with the Notification?

What would happen if the interface has a number of TextFields within, I guess the coder then will need to reference using 'id' ?

Thanks again Jim, sorry to bother you.

Pete

jiminaus
Jun 26, 2011, 04:06 AM
So, what you're saying is, as NSTextField inherits the NSText object which has the textDidChange delegate which will post a notification when an end user taps a key.

NSTextField doesn't inherit from NSText. It does have the same delegate methods though.


Then the method you wrote will handle all messages associated with the Notification?

What would happen if the interface has a number of TextFields within, I guess the coder then will need to reference using 'id' ?


I'm still not sure which method you're actually referring? The textDidChange: method in the NSTextField subclass, or the controlTextDidChange: method in delegate.