Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 19, 2005, 04:37 PM   #1
gamestriker
macrumors regular
 
Join Date: Dec 2004
Tables and Timers and Steppers, oh my!

I have several questions on how to do certain things in Cocoa:

1) How does NSStepper work exactly? Can I simply connect it to a method in a custom controller, or do I have to set it up a special way? How would that method get the increment from NSStepper? My aim is to have an NSStepper fire a custom method that will do incrementing and decrementing for the values I want it to, and then I can update the UI myself.

2) How can I change the text in a button when its pressed? Do I just call a method to change the text of the button?

3) I want set up a table so that when I click on a cell, it fires a method that will change the text in a text field to the desired text. How would I do this?

4) How do I use an NSTimer? I want to have an NSTimer countdown every second, and at every second, fire a method to update the values and UI. I also want to know how I can start or pause the NSTimer with the push of an NSButton.

5) Is there a way to set values (text) in table cells from IB or do I have to do it through code? If through code only, how would I do that?

6) How can I change what shows up on the "About" panel when you click on About NewApplication?

I appreciate the help.
__________________
PowerBook 12" Rev D - PowerPC G4 1.5GHz, 1.25GB RAM, 80GB HDD, APX/BT, SuperDrive, running Mac OS X Tiger 10.4.8
gamestriker is offline   0 Reply With Quote
Old Dec 19, 2005, 05:16 PM   #2
HexMonkey
Administrator
 
HexMonkey's Avatar
 
Join Date: Feb 2004
Location: New Zealand
Quote:
Originally Posted by gamestriker
1) How does NSStepper work exactly? Can I simply connect it to a method in a custom controller, or do I have to set it up a special way? How would that method get the increment from NSStepper? My aim is to have an NSStepper fire a custom method that will do incrementing and decrementing for the values I want it to, and then I can update the UI myself.
In Interface Builder, set up the NSStepper attributes (min value, max value, increment amount etc). These can also be set programmatically at runtime if necessary. When the user clicks the up or down arrows, the value of the NSStepper changes automatically according to its attributes. Set up an IBAction for the control, and then you can get its new value there:

Code:
- (IBAction)stepStepper:(id)sender
{
	int stepperValue = [sender intValue]; //Get the new value

	//Do other things (eg update other controls if necessary)
}
Quote:
Originally Posted by gamestricker
2) How can I change the text in a button when its pressed? Do I just call a method to change the text of the button?
In the button's action method just call [sender setTitle:newTitle];

Quote:
Originally Posted by gamestricker
3) I want set up a table so that when I click on a cell, it fires a method that will change the text in a text field to the desired text. How would I do this?
In Interface Builder, set one of your classes as the table's delegate (it's an attribute of the table view). Then in the class, implement the -tableViewSelectionDidChange: method, eg:

Code:
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
	int selectedRow = [[aNotification object] selectedRow];
	...
	[myTextField setStringValue:someString];
}
Quote:
Originally Posted by gamestricker
4) How do I use an NSTimer? I want to have an NSTimer countdown every second, and at every second, fire a method to update the values and UI. I also want to know how I can start or pause the NSTimer with the push of an NSButton.
Start the timer as follows (note you'll need to store the timer as a variable so that you can stop it later, unless you stop it from within the timer's selector):

Code:
NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateInterface:) userInfo:nil repeats:YES];
Then your timer method would be something like this:

Code:
- (void) updateInterface:(NSTimer*)theTimer
{
	//Update interface here
}
An NSTimer can't be paused, so to pause and restart you'd have to stop a timer then create a new one. To stop a timer:

Code:
[myTimer invalidate]
Quote:
Originally Posted by gamestricker
5) Is there a way to set values (text) in table cells from IB or do I have to do it through code? If through code only, how would I do that?
Through code, you'll need to implement the NSTableView data source methods. Set one of your classes as the table's data source in Interface Builder, then in that class implement (at minimum) the following methods:

Code:
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex //Return the cell contents from this method
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
Alternatively, you could use Cocoa bindings if you're not supporting Jaguar users.

Quote:
Originally Posted by gamestricker
6) How can I change what shows up on the "About" panel when you click on About NewApplication?
Set the appropriate values in the Info.plist and InfoPlist.strings files in your project and it will update automatically. If you want to display more information, add a Credits.rtf or Credits.html file to your project, and it will be added to your about box as well.

Edit to add: You might find AppKiDo a useful application. It shows documentation for each class in a nicer way than Xcode's documentation.
HexMonkey is offline   0 Reply With Quote
Old Dec 19, 2005, 05:17 PM   #3
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
0) Have you read the documentation and looked at the sample apps?

1) Read the documentation!

2) Have you tried calling setStringValue:?

3) Look at the delegate methods of NSTableView. You can get informed whenever the selection changes. Note that in an NSTableView you cannot select a single cell, only a row or column. You probably really want an NSMatrix.

4) Read the documentation. It's really quite simpe.

5) You have to do it in code. Either by a datasource or via Bindings.

6) Put a file called Credits.rtf (or Credits.html) in Resources. If you mean the Copyright string look at the Info.plist entries.
robbieduncan is offline   0 Reply With Quote
Old Dec 19, 2005, 10:21 PM   #4
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by gamestriker
2) How can I change the text in a button when its pressed? Do I just call a method to change the text of the button?.
You can set the buttons Alt. Title attribute in IB, then when the button is pressed it will change to that text. However, this only works when you have the button behavior set as Toggle, which the default button does not allow, so you'd have to change button styles.

For the default button style, you'd have to do it manually using setStringValue:. It would be nice if bindings could better communicate directly between two UI objects. For example, provide a text field right in the IB bindings palette where your object would take on that static value when a bound control has NSOnState (or whatever). You can do this with bindings by going through an abject with bound properties and methods, but I'd like a simpler, more direct way as an option.
HiRez is offline   0 Reply With Quote
Old Dec 20, 2005, 03:51 AM   #5
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: Hitchin, Herts, UK
Quote:
Originally Posted by gamestriker
I
4) How do I use an NSTimer? I want to have an NSTimer countdown every second, and at every second, fire a method to update the values and UI. I also want to know how I can start or pause the NSTimer with the push of an NSButton.
Have you considered using notifications to trigger updates to your UI?
caveman_uk is offline   0 Reply With Quote
Old Dec 21, 2005, 12:04 PM   #6
gamestriker
Thread Starter
macrumors regular
 
Join Date: Dec 2004
Thanks HexMonkey, you helped quite a bit.

Quote:
Originally Posted by robbieduncan
0) Have you read the documentation and looked at the sample apps?

1) Read the documentation!
Some of the documentation did not make sense to me or I did not understand. The Apple Documentation is good, but not always clear.

Quote:
Originally Posted by HiRez
You can set the buttons Alt. Title attribute in IB, then when the button is pressed it will change to that text. However, this only works when you have the button behavior set as Toggle, which the default button does not allow, so you'd have to change button styles.
Thank you, that is a much nicer way of doing that. Is there a way to sychronize that with a menu item? I have a menu item for that button as well and I want it to change text just like the actual button. At the moment, I'm doing this through code (in the action that is called when the button is pressed).

Quote:
Originally Posted by caveman_uk
Have you considered using notifications to trigger updates to your UI?
Elaborate please. I'm still not yet familiar with a lot of stuff in Objective-C and Cocoa.
__________________
PowerBook 12" Rev D - PowerPC G4 1.5GHz, 1.25GB RAM, 80GB HDD, APX/BT, SuperDrive, running Mac OS X Tiger 10.4.8
gamestriker is offline   0 Reply With Quote
Old Dec 22, 2005, 04:24 AM   #7
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: Hitchin, Herts, UK
Notifications are a way of communicating between objects without actually doing a method call. Basically objects that are interested in something happening register as observers of a notification. When the thing happens the object that makes it happen posts a notification and then all the objects that registered as observers get told. In your case you might have code that updates the UI and another object that handles your data model. The UI code registers as an observer of a notification that the data model would post if there were any changes.

The Apple documentation is here
caveman_uk is offline   0 Reply With Quote
Old Dec 22, 2005, 01:28 PM   #8
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by gamestriker
Is there a way to sychronize that with a menu item? I have a menu item for that button as well and I want it to change text just like the actual button. At the moment, I'm doing this through code (in the action that is called when the button is pressed).
I'm not sure I'm understanding exactly what you're asking here...you're saying you have both a menu item and a button which both control the same variable (they are always synchronized), and can have one of two states with different text for each state? In that case you might want to try Cocoa bindings to keep the two synchronized, with an object controller (or bind to a controller you wrote). Cocoa bindings are powerful, but far too difficult to explain here, search for "Cocoa bindings" in the documentation using a full-text search to get you started. There's also a pretty good intro here. Be careful of changing the text of menu items as well, that can be bad interface design if misused and confusing for the user. Sometimes you can indicate "on" or "off" status for a menu item by checking it. Another strategy is to have two items visible all the time, but only keep the currently-applicable one enabled.
HiRez is offline   0 Reply With Quote
Old Dec 24, 2005, 09:39 PM   #9
gamestriker
Thread Starter
macrumors regular
 
Join Date: Dec 2004
Rather than start a separate thread, I'll just ask here.

Does anyone know if and what is the equivalent of making a "public static final CONSTANT_HERE" variable in Objective-C/Cocoa?
__________________
PowerBook 12" Rev D - PowerPC G4 1.5GHz, 1.25GB RAM, 80GB HDD, APX/BT, SuperDrive, running Mac OS X Tiger 10.4.8
gamestriker is offline   0 Reply With Quote
Old Dec 24, 2005, 10:00 PM   #10
gamestriker
Thread Starter
macrumors regular
 
Join Date: Dec 2004
Quote:
Originally Posted by HexMonkey
Through code, you'll need to implement the NSTableView data source methods. Set one of your classes as the table's data source in Interface Builder, then in that class implement (at minimum) the following methods:

Code:
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex //Return the cell contents from this method
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
Alternatively, you could use Cocoa bindings if you're not supporting Jaguar users.
I implemented the 2 methods (with a little help from the Apple Documentation) and set the class as the data source, but the text still won't show up.

I'd appreciate the help.
__________________
PowerBook 12" Rev D - PowerPC G4 1.5GHz, 1.25GB RAM, 80GB HDD, APX/BT, SuperDrive, running Mac OS X Tiger 10.4.8
gamestriker is offline   0 Reply With Quote
Old Dec 24, 2005, 10:57 PM   #11
HexMonkey
Administrator
 
HexMonkey's Avatar
 
Join Date: Feb 2004
Location: New Zealand
Quote:
Originally Posted by gamestriker
I implemented the 2 methods (with a little help from the Apple Documentation) and set the class as the data source, but the text still won't show up.
Do the methods get called? To find out, add breakpoints to the methods then debug (and see if they are triggered), or use functions such as NSBeep or NSLog inside them (which have immediately noticeable effects).

Double check the connections in Interface Builder, it's the most common place for things to go wrong. If that doesn't fix it, post your code for those methods here.
HexMonkey is offline   0 Reply With Quote
Old Dec 25, 2005, 12:08 AM   #12
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
Quote:
Originally Posted by gamestriker
Rather than start a separate thread, I'll just ask here.

Does anyone know if and what is the equivalent of making a "public static final CONSTANT_HERE" variable in Objective-C/Cocoa?
#define CONSTANT_HERE constant

i.e. #define PI 3.1415926535
Catfish_Man is offline   0 Reply With Quote
Old Dec 25, 2005, 01:36 PM   #13
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by Catfish_Man
#define CONSTANT_HERE constant

i.e. #define PI 3.1415926535
Just a quick tip, you do NOT put a semicolon after #defines (known along with other "#" markers as preprocessor directives because during compilation these are evaluated first before the rest of the code). Normally you put these at the very top of your .h (header) file, just underneath your#import/#include statements (also preprocessor directives). Alternatively, when using Xcode, you can put #define statements in your "prefix" header file, which will automatically make them usable to all the classes in your project.
HiRez is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Why are some 12 steppers worse off in sobriety? 63dot Politics, Religion, Social Issues 18 Jan 3, 2014 09:45 PM
All iPads: 1st Timers, how are you liking your iPad? Blakjack iPad 4 Nov 17, 2013 07:57 AM
Timers in Lockscreen Extcee iOS 7 14 Jun 17, 2013 08:56 PM

Forum Jump

All times are GMT -5. The time now is 06:31 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC