Set NSDatePicker to System Time

Discussion in 'Mac Programming' started by stadidas, Jul 28, 2006.

  1. macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #1
    At Uni last year I wrote a program in pure Java to help manage my money. Recently I've decided to re-write this program using Cocoa and Objective-C.
    I've worked out most of the back-end of it, but there's one thing I need to sort out first.
    Here is the prototype interface I've been working on:
    [​IMG]

    I would like to be able to set the top NSDatePicker to the current system date. I would then use the value from the user-definable NSDatePicker at the bottom to calculate how long is left in the current term.
    If anyone knows how to set an NSDatePicker to the current system time I would be grateful if you let me know!
     
  2. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #2
    Just looking at the docs have you tried

    [datePickerObject setDateValue:[NSDate date]];
     
  3. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #3
    I shall give that a try when I get down to coding it.
    By the way, Beer Alchemy looks awesome! One of my mates at Uni is buying a MacBook Pro soon, and he brews beer, so he'll be buying a license, he looked impressed by your app.
     
  4. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #4
    I've made a test project to try and get this NSDatePicker setting to work.
    I have an NSDatePicker called currentDate defined in my header file. I have written a method called setDate which I call from the + (void)Initialize function. The setDate function looks like this:
    Code:
    - (void)setDate
    {
    	[currentDate setDateValue:[NSDate date]];
    }
    
    I call it from the Initialize function by doing [self setDate]; but I get a warning and it doesn't seem to work. I'm new to this Objective C lark and am probably doing something very stupid. Anyone know what I'm doing wrong?
     
  5. macrumors 6502

    Joined:
    Jul 25, 2006
    #5
    Perhaps if you told us what the warning you're getting is, we could help you easier...

    Also, it looks like you have your class's +initialize method called +Initialize, which isn't the same thing, and it's probably never getting called since you're not overriding the correct method.
     
  6. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #6
    Ta very much!:D
    Is there any reason why you're using that? I'd do it in -init. Do you know the difference between the - and + methods. (Just asking)?
     
  7. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #7
    Good point...I didn't spot that.
     
  8. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #8
    I am using the initialize function, shouldn't have capitalised it.
    The - methods are instance methods, and + are class methods I think. Still not sure whether they're supposed to be called functions or methods (I tend to think of them as methods as I come from a Java background).

    I've now put this code in:
    Code:
    - (void) init
    {
    	[self setDate];
    }
    
    I get an warning saying: 'Converter' may not respond to '-setDate'. When I try and run the app the XCode run log says:
    "[Session started at 2006-07-28 22:53:29 +0100.]
    2006-07-28 22:53:30.330 NSDatePickerTest[1309] *** -[NSCFArray addObject:]: attempt to insert nil"
     
  9. macrumors 6502

    Joined:
    Jul 25, 2006
    #9
    Looks like you've got two problems that may or may not be connected.

    First, the compiler can't find a -setDate method for your Converter class. Did you declare one in a .h file?

    Second, you think you're adding something to an array, but you're not (you're trying to add nil instead of a valid pointer). I would break on -[NSException raise] in the debugger to see why that pointer is nil.
     
  10. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #10
    +initialize is almost certainly a bad time to be doing stuff like that. Try registering as NSApp's delegate and doing it in -applicationDidFinishLaunching.
     
  11. macrumors newbie

    Joined:
    Nov 6, 2008
    #11
    I think, you have following 3 issues: I had those previousely
    1. in your header file, you need to declare the setDate method like
    -(void) setDate;
    this will help for the warmming at compile time

    2. in the init function, you need to return the object
    please note, you don't set time here, the datepicker obj is not ready yet
    -(id)init
    {
    return self;
    }

    3. add another method in your .m file and run your setDate here
    -awakeFromNib
    {
    [self setDate];
    }

    try above three steps. :)
     

Share This Page