Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Change picture depending on time of day

Good evening,
I am new to Xcode programming (like so many others) and I am not sure is this has been asked before, but I'm pretty sure that it hasn't. Basically, I'm trying to build an app where a random picture comes up every 30 seconds. On this same page, I want a button that sends an email to someone (wich I have already made), so if there is a way to make a timer where, after 30 seconds it changes to one of 7 images, and on top of that, there is a button that allows you to send an email, I would love to know how to.

Thank you all in advance,
-Jonathan

Sorry for the title, that was my original idea, but then I realized that I liked this one better
Post edited by JonathanBunt on

Replies

  • baja_yubaja_yu Posts: 6,166Super Moderators, Registered Users
    Just use an NSTimer with a 30s interval and set it to repeat. It's basically one line to create the timer. Then in the method that the timer calls change the image (also a few lines).
    As for sending an email, use the MFMailComposeViewController: Loading…
  • JonathanBuntJonathanBunt Posts: 7New Users
    baja_yu;426550 said:
    Just use an NSTimer with a 30s interval and set it to repeat. It's basically one line to create the timer. Then in the method that the timer calls change the image (also a few lines).
    As for sending an email, use the MFMailComposeViewController: Loading…
    Thank you for the advice, I have tried something like this already, but it won't let me use both, the image randomizer and the email at the same time (At least not the way that I have done it). Mainly, how can I put 2 different commands in the .h file (like:

    @interface FourthViewController :UIViewController {

    }
    -(IBAction)email;
    @end


    and then another thing like the randomizer, whenever I try to do it, it gives me one (or many) errors, is there a certain command that I need to use?

    Once again, thanks for your speedy response,
    -Jonathan
  • Duncan CDuncan C Posts: 8,019Tutorial Authors, Registered Users
    JonathanBunt;426553 said:
    Thank you for the advice, I have tried something like this already, but it won't let me use both, the image randomizer and the email at the same time (At least not the way that I have done it). Mainly, how can I put 2 different commands in the .h file (like:

    @interface FourthViewController :UIViewController {

    }
    -(IBAction)email;
    @end


    and then another thing like the randomizer, whenever I try to do it, it gives me one (or many) errors, is there a certain command that I need to use?

    Once again, thanks for your speedy response,
    -Jonathan
    If your view controller has a method called viewWillAppear:, declared like this:

    - (void)viewWillAppear:(BOOL)animated;

    The system will call it before displaying your view controller.

    Similarly, if you have a method viewWillDisappear:, declared like this:

    - (void)viewWillDisappear:(BOOL)animated


    The system will call that when your view controller is about to be closed.

    Add those two methods to your view controller. (Actually, the standard Apple template has those methods, either empty or commented out. Look for them.)

    Add an NSTimer instance variable pictureTimer to your .h file.

    In your viewWillAppear method, use the scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
    method to create a timer and start it running every 30 seconds. Save the timer into your pictureTimer iVar (instance variable).

    Set up the timer to call a method called changePicture:

    The docs on the scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: method explain how to specify the selector of a method to get called each time the timer fires, and what that method should look like.

    Have your changePicture method load a random picture and install it into your image view.

    In your viewWillDisappear: method, call [pictureTimer invalidate]; and the set pictureTimer to nil.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • JonathanBuntJonathanBunt Posts: 7New Users
    Duncan C;426557 said:
    If your view controller has a method called viewWillAppear:, declared like this:

    - (void)viewWillAppear:(BOOL)animated;

    The system will call it before displaying your view controller.

    Similarly, if you have a method viewWillDisappear:, declared like this:

    - (void)viewWillDisappear:(BOOL)animated


    The system will call that when your view controller is about to be closed.

    Add those two methods to your view controller. (Actually, the standard Apple template has those methods, either empty or commented out. Look for them.)

    Add an NSTimer instance variable pictureTimer to your .h file.

    In your viewWillAppear method, use the scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
    method to create a timer and start it running every 30 seconds. Save the timer into your pictureTimer iVar (instance variable).

    Set up the timer to call a method called changePicture:

    The docs on the scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: method explain how to specify the selector of a method to get called each time the timer fires, and what that method should look like.

    Have your changePicture method load a random picture and install it into your image view.

    In your viewWillDisappear: method, call [pictureTimer invalidate]; and the set pictureTimer to nil.
    Thanks for the help, but as I stated before, I am very new to this, what you means makes sense, but I don't know how to add it into the code, the
    - (void)viewWillAppear:(BOOL)animated;


    and the

    - (void)viewWillDisappear:(BOOL)animated


    seem simple enough, what I can't get is the timer, if you could explain a little further I would appreciate it,

    Thanks again
    -Jonathan
  • Duncan CDuncan C Posts: 8,019Tutorial Authors, Registered Users
    JonathanBunt;426942 said:
    Thanks for the help, but as I stated before, I am very new to this, what you means makes sense, but I don't know how to add it into the code, the
    - (void)viewWillAppear:(BOOL)animated;


    and the

    - (void)viewWillDisappear:(BOOL)animated


    seem simple enough, what I can't get is the timer, if you could explain a little further I would appreciate it,

    Thanks again
    -Jonathan

    The method you want to use to create your timer is called scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

    First parameter: The time interval.

    Second parameter. target:: The object that you want to get called each time the timer goes off. You probably want "self", which means the timer will call the object that is setting it up.

    Third parameter. selector:: The selector of the method you want to call. Use something like @selector(updatePicture:)

    That would mean you had a method updatePicture, that looked like this:

    - (void) updatePicture: (NSTimer *) theTimer;


    Fourth parameter. userInfo: An optional object that you ask the timer to pass to you. Use nil

    Fifth parameter. repeats: Pass in TRUE if you want the timer to fire over and over. Pass FALSE if you only want the timer to fire once. In your case, you do want it to repeat, so pass in TRUE.

    The method scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: returns the timer that it creates. As I said in my previous post, save that result into an instance variable called something like pictureTimer, so you can invalidate the timer when your view controller's view is about to disappear.

    Take a stab at writing the call you need to scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:. You'll learn more than if I write it for you.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • JonathanBuntJonathanBunt Posts: 7New Users
    Duncan C;427140 said:
    The method you want to use to create your timer is called scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

    First parameter: The time interval.

    Second parameter. target:: The object that you want to get called each time the timer goes off. You probably want "self", which means the timer will call the object that is setting it up.

    Third parameter. selector:: The selector of the method you want to call. Use something like @selector(updatePicture:)

    That would mean you had a method updatePicture, that looked like this:

    - (void) updatePicture: (NSTimer *) theTimer;


    Fourth parameter. userInfo: An optional object that you ask the timer to pass to you. Use nil

    Fifth parameter. repeats: Pass in TRUE if you want the timer to fire over and over. Pass FALSE if you only want the timer to fire once. In your case, you do want it to repeat, so pass in TRUE.

    The method scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: returns the timer that it creates. As I said in my previous post, save that result into an instance variable called something like pictureTimer, so you can invalidate the timer when your view controller's view is about to disappear.

    Take a stab at writing the call you need to scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:. You'll learn more than if I write it for you.

    This is perfect, thank you so much, and I really appreciate the fact that you chose to explain it thoroughly rather than just give out the code, once again thank you

    -Jonathan

    (by the way, is there like a button to add reputation, or help points to others like on other forums?)
  • Duncan CDuncan C Posts: 8,019Tutorial Authors, Registered Users
    JonathanBunt;427250 said:
    This is perfect, thank you so much, and I really appreciate the fact that you chose to explain it thoroughly rather than just give out the code, once again thank you

    -Jonathan

    (by the way, is there like a button to add reputation, or help points to others like on other forums?)

    As far as I know, all you can do is up-vote this thread.

    You sent me a message in PM asking:
    I had one final question, what you gave me, the code, is it supposed to go in the heard file, or in the .m? sorry, as I had stated I'm fairly new to this.
    By "heard file" do you mean the header file?

    Executable code never goes in the header file. You only put your interfaces, variable declarations, and defines in the header file. Code that actually executes must go in the .m file.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
Sign In or Register to comment.