| OS X - Development and Darwin Discussion and questions about development for Mac OS X. |
| Post Reply | New Thread | Subscribe |
|
|
Thread Tools |
![]() Member Since: Mar 04, 2010
Posts: 37
![]() |
Hey.
I'm new to xCode but learning fast due to already having somewhat of a background in C++. The whole square bracket/message thing was a little disconcerting at first, but I got used to it. Anyways, on to my questions. There's two of them. One: I'm writing a little practice app for Mac osx that's supposed to allow you to set a timer and then show a reminder message when the time runs out: ![]() The slider is used to select # of minutes to set, the level indicator drains as time runs out, and the label that says "1" changes to show number of minutes selected on the slider (not time remaining). Right now I'm using a repeating NSTimer to tick seconds down. However, when the computer goes to sleep, the NSTimer obviously stops repeating until the computer awakens. I figure I need an NSDate set to x amount of seconds in the future (using dateWithTimeIntervalSinceNow) but being that the user can press start and stop whenever they want, however many times they want, they're going to be creating many NSDate's. When creating a new NSDate, do I just release the old one by calling [endTime release] and then calling the above method again to create the next timer? (assuming of course I already have an NSDate object named endTime) Two: I original wanted my slider on the above program to go from 1-240, and multiply the amount chosen by 0.25 thus allowing them to choose from 1-60 minutes in 15 second increments. But as mentioned, I also have a label on which I'm displaying the value of the slider. I got the takeIntValueFrom thing down (by that I mean I control+clicked and dragged from the slider to the textbox, and chose that method), but if the slider is 1-240, the label can also show 1 to 240. How can I get it to multiply the slider value by 0.25 and show THAT on the label in real time (thus still only showing 1-60? |
| QUOTE Thanks | |
![]() Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
![]() ![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
|
I did a similar little project a few years for the same reason, for practice.
Mine takes manual input of the time interval of interest, so I didn't play with the slider technique.From how I understand your description in question 1, I think you are on the right track. Give it a try. For number 2, things get a little complicated to explain. I think this is what you want to know... After you have done takeIntValueFrom in your action you have to apply your math. From the looks of my code, I converted the input of days, hours, minutes and seconds down to seconds. So do what it takes to convert a number such as 59.75 (239 * .25) minutes into seconds and use the value to calculate your endTime. Also use this value to set the textbox using setFloatValue:, I think. If you want the textbox to have pretty formatting, you'll need to split the minutes and seconds out as separate numbers I think and setup an appropriate looking string. Although there maybe some formatting options I'm not aware of. Someone at the Lake Forest CA Cocoaheads meeting last night was asking about this kind of app too. CameraTime - Time lapse photography for novice and advanced users. When asking questions, post the version of your software. You'll receive better answers. Please post your results to the thread as it is good feedback.
|
| QUOTE Thanks | |
![]() Member Since: Mar 04, 2010
Posts: 37
![]() |
Quote:
Actually though (for number two), my problem is more about the fact that I don't know where to put the code for the math. Right now all I've got is a "connection" (I don't know the "official" term) that causes the slider to send takeIntValueFrom to the label. I did that by just control-dragging, in interface builder, from the slider to the label and then selecting the method from the popup menu. I do want as you said to change it so that it's formatted nicely with two variables placed neatly into a string, but to do that I suspect I need to overwrite the label's takeIntValueFrom method somehow. Unfortunately, I have no idea what to write or where to write it in order to do that. =/ Last edited by SoulRed12; 04-15-2010 at 10:58 PM. Reason: Made post clearer =p |
|||||
| QUOTE Thanks | ||||||
![]() Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
![]() ![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
|
In your controller object you want to add an outlet of NSSlider type and an action for your slider to send to. You then control drag from the slider to your controller instance and connect to the action. Then you control drag from the controller to the slider and connect to your outlet. I think I have that the right way around.
Now in the action code, you do your math and update the text field. That will be called when the slider moves and you'll get the value from the slider via the takeIntValueFrom: message. You should have an outlet for the text box too and connected via IB. Do you have some books? How are you learning this stuff? CameraTime - Time lapse photography for novice and advanced users. When asking questions, post the version of your software. You'll receive better answers. Please post your results to the thread as it is good feedback.
|
| QUOTE Thanks | |
![]() Member Since: Mar 04, 2010
Posts: 37
![]() |
Quote:
I've been learning it using some video podcasts, and then looking up things in the documentation when I hit a roadblock. I watched a few podcasts with the basics in them and then I decided to start writing my first project to see what I could do. Last edited by SoulRed12; 04-16-2010 at 03:04 PM. |
|
| QUOTE Thanks | ||
![]() Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
![]() ![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
|
The Stanford University iPhone course could be helpful too. It is available via iTunes. Sure, you'd have to watch out to not get confused with iPhone stuff and be open to finding the alternatives, such as NSView instead of UIView, but it is a well constructed course.
To add complication to your app, you could make the text box an interactive text field to allow the user to type in the time. CameraTime - Time lapse photography for novice and advanced users. When asking questions, post the version of your software. You'll receive better answers. Please post your results to the thread as it is good feedback.
|
| QUOTE Thanks | |
![]() Member Since: Mar 04, 2010
Posts: 37
![]() |
Quote:
But actually, I have a problem somewhat related to the first question now... I was trying to change the timing system to use the NSDate, and now I'm having a bit of an issue with this method. I kept the NSTimer so I could update the level indicator every 1/8 second, but for some reason when the timer executes the countDown method, the app freezes. When viewing the console, it brings up something called "gdb" and one time it also said that the "program received signal: EXC_BAD_ACCESS". I have no idea what either of those things are. Here's the method that's causing the problem (you'll see below why I know this): Code:
-(void)countDown
{
float barVal;
// Set barVal to a float which is the % of time completed
barVal = (float)[endTime timeIntervalSinceNow] / (float)startSeconds;
[timeBar setFloatValue:ceil(barVal*240)];
// When it's past endTime, call the timeOver method
if ([endTime timeIntervalSinceNow]<=0) [self timeOver];
}
Code:
// note to readers of this post: mainTimer variable created earlier mainTimer = [NSTimer scheduledTimerWithTimeInterval:0.125 target:self selector:@selector(countDown) userInfo:nil repeats:YES]; |
|
| QUOTE Thanks | ||
![]() Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
![]() ![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
|
The first thing that I noticed is that your selector signature is wrong. It should have a colon in it. So this part
Code:
selector:@selector(countDown) Code:
selector:@selector(countDown:) Code:
-(void)countDown:(NSTimer*)theTimer; Code:
NSLog(@"endTime: %@", endTime); NSLog(@"endTime: %@", [endTime description]); Also confirm via an NSLog that startSeconds is not zero. By the way, have you considered what happens when you pass your NSLevelIndicatorCell a negative value? I didn't see any mention of negative numbers mentioned in the documentation. As for gdb. That is the GNU Debugger. You use debuggers while the program is running. They allow you to set break points, inspect and reset variables, step through code and some other things. See this video for some debugging tips. EXC_BAD_ACCESS means you have done something that caused your running program to try to access memory it isn't allowed to access. When you created endTime, was it retained? If the NSLog()s I mention above crash, that is a likely scenario. CameraTime - Time lapse photography for novice and advanced users. When asking questions, post the version of your software. You'll receive better answers. Please post your results to the thread as it is good feedback.
|
| QUOTE Thanks | |
![]() Member Since: Mar 04, 2010
Posts: 37
![]() |
Quote:
Quote:
Code:
#import <Foundation/Foundation.h>
@interface timerController : NSObject {
IBOutlet NSTextField *numOfMinutes;
IBOutlet NSLevelIndicator *timeBar;
IBOutlet NSSlider *timeSlider;
IBOutlet NSButton *toggleButton;
IBOutlet NSTextField *messageToShow;
NSTimer *mainTimer;
NSDate *endTime;
float startSeconds;
bool buttonState;
}
-(IBAction)toggleTimer:(id)sender;
-(IBAction)updateTextField:(id)sender;
-(void)countDown;
-(void)timeOver;
@end
Quote:
Quote:
Thanks for all your time and help with this, I seriously appreciate it. |
||||
| QUOTE Thanks | |||||
![]() Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
![]() ![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
|
The fact that you get a crash at those NSLog()s suggests your endTime ivar isn't set, or you are not retaining it after setting it.
When is it crashing; upon opening the application, or when hitting the 'Start' button? If it crashes at startup, then it sounds like you are initiating your timer when you haven't set things up yet. Specifically endTime. Your interface suggests that you are not initiating your timer because you haven't designated the obvious methods; awakeFromNib or init. If it crashes when hitting the start button, have you used the slider yet to assign a value to endTime? What is the code that sets up endTime look like? You need to look into Objective-C memory management. On the iPhone OS, you are responsible for it. Here is a link that covers some of it. There are several good tutorials at Cocoadevcentral. The general rule for when you get a retained object back from classes is weather you have used the alloc, copy, or new class methods to create your object. If you have not, then you have an autoreleased object that you need to apply the retain method to if you want to keep it beyond the method you are in, even when that object is declared in your interface. Objects that are autoreleased get released at the end of the event loop. I'll leave it up to you to read about this. It is critical that you understand this and is to long to describe here. CameraTime - Time lapse photography for novice and advanced users. When asking questions, post the version of your software. You'll receive better answers. Please post your results to the thread as it is good feedback.
|
| QUOTE Thanks | |
![]() Member Since: Mar 04, 2010
Posts: 37
![]() |
I see. I knew I was missing something, I had no idea there was even a retain method at all. Haha. That would be the problem then, because I am using autoreleased objects.
Thanks for clearing this up, I've obviously got a lot to learn. Time to hit the books (or internet resources XD) I guess. Thanks again. |
| QUOTE Thanks | |
| Post Reply | New Thread | Subscribe |
| Thread Tools | |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|
|||||||
Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Couple of questions | mrcinsc | iOS and Apps | 2 | 09-03-2008 12:12 AM |
| A couple questions about the hardware | Spr33 | Apple Desktops | 5 | 08-23-2008 09:27 PM |
| New to Mac, couple of questions. | punjabi | Switcher Hangout | 4 | 08-16-2008 12:01 PM |
| Bonjour! Just a couple of questions | Hunter_Predd | Switcher Hangout | 3 | 02-10-2007 02:38 PM |
| Um, a couple questions. Ok, alot. | catlikethief | Switcher Hangout | 18 | 08-16-2003 02:46 AM |
All times are GMT -4. The time now is 05:38 PM.
Powered by vBulletin