Newbie q: How can I display the result of NSDate?

Discussion in 'iOS Development' started by Bmachine, Apr 8, 2009.

  1. Bmachine

    Bmachine New Member

    Joined:
    Apr 8, 2009
    Messages:
    4
    Likes Received:
    0
    OK, super newbie question here. Apologies in advance.

    I just wrote my first app and I have what should be a very simple question: I have a super simple Hello World app which sends Hello World to the screen via a basic
    - (IBAction)showText {
    mainText.text=@"Hello World";
    }
    in my mainView.m file.

    Now instead of Hello World, I want to display the current time and date. I read in the docs that
    NSDate *date =[NSDate date];
    will retrieve the current date and time.

    So my question is
    - what file do I put that statement in? The mainView.m or the AppDelegate.m?

    - then, do I first need to declare a variable in the .h file for this to work?

    - once that is done, how do I take the result (which is what, exactly?) and turn it into something that I can use in the mainView file to display it? Would it be something like:

    - (IBAction)showText {
    mainText.text=@date;
    }

    Is that the right syntax?

    Sorry if this is real basic but I find that while it is not too hard to find little snippets of code by doing searches (as in thread 131588 for example), it is a lot harder to find the complete sequence of where to put each bit and what little declarations you also need, etc...

    Thank you for any help!

    Bo
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    NSDateFormatter.


    Edit: sigh.
    Code:
    	NSDate *date = [NSDate date];
    	
    	NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]  autorelease];
    	[dateFormatter setDateStyle:NSDateFormatterLongStyle];
    	[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    	NSString *dateString = [dateFormatter stringFromDate:date];
    	
    	
    	[dateFormatter setDateStyle:NSDateFormatterNoStyle];
    	[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    	NSString *timeString = [dateFormatter stringFromDate:date];
    	
    	someLabel.text = [NSString stringWithFormat:@"%@ | %@", dateString, timeString];
  3. Codax

    Codax New Member

    Joined:
    Apr 8, 2009
    Messages:
    1
    Likes Received:
    0
    Well the first thing to do, as someone new to programming in Cocoa, would be to read up on the framework. I would suggest the Cocoa Fundamentals Guide off the Apple developer website.

    All the framework classes, such as NSDate, have references online where you can look up how they work. If you really want to know how to program for the iPhone your time may be better spent by doing some (a lot) of reading.
  4. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    I hear you man. I hear you.
  5. Bmachine

    Bmachine New Member

    Joined:
    Apr 8, 2009
    Messages:
    4
    Likes Received:
    0
    Thank you very much SkylarEC.

    I know it's a bit of a pain to have to answer questions that seem old and uninteresting when you are an experienced coder. I apologize for that. But I only decided to post it after many days of trying to wrap my head around this issue.

    I am a graphic designer by trade and my programming experience comes from "straight line" programming in the movie effects industry using perl, shell scripts or renderman shader writing. So xcode and its myriad of separate files that all seem to perform overlapping functions is a bit confusing.

    Thank you again.

    Bo
    ------------------double post merged------------------

    Thanks again guys, the recommendations worked very well.

    Dare I ask a followup question on the code posted by Skylar? It worked perfectly well. But I noticed that it basically takes a "snapshot" of the time when the app is launched and freezes it until the app quits and relaunches. I want add a little IBAction button to refresh it.

    What would the code for this action look like?

    Thank you very much.

    Bo
  6. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    it look like Today Date | Time
  7. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    I took the code directly out of this app I'm working on:

    Please Register or Log in to view images


    Clearly, I need to reposition the text a couple of pixels, but you get the idea. The output is the date as seen here.



    EDIT: Oh, I didn't read your post, I instead read the follow up and replied, this the picture.

    If you want this to occur in your ib action, just move this code into that action.
    Code:
    - (IBAction)updateDataLabel:(UIButton *)button {
    
    [INDENT]/*  The code I gave you above  */[/INDENT]
    
    }
  8. Bmachine

    Bmachine New Member

    Joined:
    Apr 8, 2009
    Messages:
    4
    Likes Received:
    0
    Excellent! Thank you very much, Skylar!

    Bo
  9. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    Skylar, is that is a gray table view backgrounds?
  10. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    All you have to do to make that is create a subclass of UITableViewCell and set the background color to lightGrayColor.

Share This Page