Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 925475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:31:33+00:00 2026-05-15T19:31:33+00:00

I’m not quite sure how UIButton works. For example, if I have a timer

  • 0

I’m not quite sure how UIButton works. For example, if I have a timer that goes off when I hit the start button, how do I make it so that if the button gets pressed again, it doesn’t start another timer. Cause then my timer is going twice as fast. It seems like I’d want to invalidate my original timer if the button gets pressed twice and start a new timer, but I don’t know how you can tell if the button gets pressed twice.

Also, is there a way to change the label on the UIButton once its pressed once, and then have it revert back when it gets pressed again? Like, Play/Pause? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:31:34+00:00Added an answer on May 15, 2026 at 7:31 pm

    Encapsulation and state management are very useful for situations like this. Depending on how much you wish to encapsulate, you might consider having a new object to be modified by the UIButton (you mention Play/Pause so I’m imagining some kind of media player?)

    (You might consider an enumeration of states in this example to make it more extendable, but for simplicity I’ll use a BOOL)

    @interface MediaManager : NSObject
    {
    
        BOOL isPlaying;  // whether
        NSTimer *playTimer; // timer used exclusively by the media manager
        //... other vars relating to type of media/selected track etc   anything related
    }
    
    -(void) togglePlay;
    
    
    @property (nonatomic, synthesize) NSTimer *playTimer;
    @propety (nonatomic, assign) BOOL isPlaying;
    
    @end
    
    
    @implementation MediaManager
    
    @synthesize playTier, isPlaying;
    
    
    - (void)togglePlay 
    {
        if (!self.isPlaying)  
        {
            if (self.playTimer == nil) // if it hasn't already been assigned
            {
                self.playTimer = [NSTimer ...]; // create and schedule the timer
                self.isPlaying = YES;
            }
        }
        else 
        {
            if (self.playTimer != nil)
            {
                // get the timer and invalidate it
                self.isPlaying = NO;
            }
        }
    
    }
    
    @end
    

    This is not the best though out example, but by encapsulating the state within a seperate object, your view UIButton can rely solely on the model state for presenting itself (assuming it holds a reference to an initialized Media Manager:

    -(IBAction) buttonPressed: (id) sender
    {
        [self.mediaManager togglePlay]; // play from whatever state it is in 
    
        // update the button text to reflect state
        if (self.mediaManager isPlaying)
        {
            self.mediaButton.textLabel.text = @"Pause";
        }
        else
        {
            self.mediaButton.textLabel.text = @"Play";
        }
    
    }
    

    This way you don’t have to include the state handling each time you want to use this functionality, and the button always reflects the true state of the object it is acting on.

    You might go a step further and encapsulate the state strings in the object also, which could be queried in a similar manner.

    (Apologies for poorly written code, I’ve not had a coffee yet)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.