Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Using NSStepper

Using NSStepper
Thread Tools
Wes
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 25, 2004, 04:59 PM
 
I have an NSTextField and an NSStepper set up in Interface Builder, and then connected using the takeValue actions. But how do I set it up so that every-time a number is typed into the NSTextField, the value of NSStepper is changed so that when the user "steps" the NSTextField doesn't revert back to the old value of whatever the NSStepper had previously?
     
itai195
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status: Offline
Reply With Quote
May 25, 2004, 05:19 PM
 
I've never used an NSStepper, but I'd guess that you'd want to use the - setIntValue: method in its superclass (NSControl). Not sure if that works, but worth a shot.
     
Wes  (op)
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 25, 2004, 06:55 PM
 
It looks like something along those lines should work... can you give me any more details though? I'm still having trouble.
     
itai195
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status: Offline
Reply With Quote
May 25, 2004, 07:08 PM
 
Sure. What you probably want to do is use a delegate for the NSTextField and also connect the delegate to the NSStepper via an outlet (you'll probably use a controller object for all of this). Implement the textDidChange: method in the delegate. That method should call setIntValue: on the NSStepper, passing whatever the current value of the NSTextField is. I can't try this out to check if it works right now, but I can try later tonight if you haven't had time to try it by then.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 25, 2004, 07:36 PM
 
I second that solution. What you need is to update the stepper's value every time the text field is edited, but NSTextField will only send its action after you finish editing. So you'll need to use controlTextDidChange:.

Incidentally, I still find it strange Apple didn't include a simple NSSteppingTextField control.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Wes  (op)
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 25, 2004, 07:58 PM
 
The whole "delegate" thing still confuses me. So if any of you have time I would appreciate a simple run threw of them as applied to my situation

Thanks for the help.
     
itai195
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status: Offline
Reply With Quote
May 25, 2004, 08:20 PM
 
Have you read this document? It describes some Cocoa design patterns, including delegates. There is also this introduction and you might be able to find some other info at cocoadev.com. If you're confused about how to set a delegate up, you can do it in Interface Builder by control-dragging a connection between your delegate in the instances pane and the NSTextField in your interface.

The delegate in your situation receives a notification from the NSTextField when its value changes and responds to that notification by changing the value of the NSStepper. The delegate is essentially an event handler in this scenario.
     
Wes  (op)
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 25, 2004, 11:12 PM
 
I understand those documents (albeit vaguely), however I still don't know what is meant by...
I second that solution. What you need is to update the stepper's value every time the text field is edited, but NSTextField will only send its action after you finish editing. So you'll need to use controlTextDidChange:.
Do I make an action "controlTextDidChange" from my TextField to the controller class that updates the value of the stepper? Will it automatically understand what I'm talking about?


( Last edited by Wes; May 25, 2004 at 11:21 PM. )
     
itai195
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status: Offline
Reply With Quote
May 26, 2004, 01:27 AM
 
Originally posted by Wes:
I understand those documents (albeit vaguely), however I still don't know what is meant by...


Do I make an action "controlTextDidChange" from my TextField to the controller class that updates the value of the stepper? Will it automatically understand what I'm talking about?


You need to connect the controller instance to the text field's delegate outlet in IB, and then implement that method as described above. The method will automatically be called when the value in the text field changes, and the body of the code (which you must implement) will update the value of the NSStepper.
     
Wes  (op)
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 26, 2004, 03:56 AM
 
I got it working with notifications in a fairly round-about way...

But if you could spare some more time for me I have another question. I want to perform a function on window loading. I have made my controller class the delegate of my window and added windowDidLoad, but that doesn't seem to want to work... any ideas?

Edit-

One more question, I tried calling ((NSTextField)field).setPlaceholderString("String" ) and it gave me a "cannot resolve symbol symbol : method setPlaceholderString (java.lang.String)". I was under the impression that NSTextField implemented all the methods of NSTextFieldCell (which contains setPlaceholderString()). ?
( Last edited by Wes; May 26, 2004 at 04:49 AM. )
     
bewebste
Senior User
Join Date: Mar 2000
Location: Ithaca, NY
Status: Offline
Reply With Quote
May 26, 2004, 10:34 AM
 
Using the text field's delegate is one way to do it, but I usually just use the action of the text field instead. The main difference is that the delegate will get called each time the user presses a key in the text field, whereas the action only gets called when the user finished editing (i.e. presses return or tab to exit the field).

Here's the way I usually handle a text field/stepper combo:

1. Create two outlets and hook them up to the text field and stepper. Let's say the outlets are called myTextField and myStepper.
2. Add an action to your controller object (we'll call it changeMyValue:) and then hook up both the stepper and the text field to that same action.
3. The action method then looks like this:
Code:
- (IBAction)changeMyValue:(id)sender { int theValue = [sender intValue]; [myStepper setIntValue:theValue]; [myTextField setIntValue:theValue]; }
Since the sender variable will hold whichever control it was that the user changed, you get the new value from the correct control, and then just put that value back into both controls, ensuring that they stay synchronized.

As for the windowDidLoad method, that is a method specific to the NSWindowController class, so it will only get called if you're loading the window through one of them. Otherwise, you should implement awakeFromNib to implement stuff that should happen after the nib file has been fully loaded.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 02:31 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,