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 7488909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:06:30+00:00 2026-05-29T15:06:30+00:00

I am currently making an app that will have multiple timers, which are basically

  • 0

I am currently making an app that will have multiple timers, which are basically all the same.

I want to create a custom class that uses all of the code for the timers as well as the layout/animations, so I can have 5 identical timers that operate independently of each other.

I created the layout using IB (xcode 4.2) and all the code for the timers is currently just in the viewcontroller class.

I am having difficulty wrapping my brain around how to encapsulate everything into a custom class and then add it to the viewcontroller, any help would be much appreciated.

  • 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-29T15:06:31+00:00Added an answer on May 29, 2026 at 3:06 pm

    Well to answer conceptually, your timer should likely be a subclass of UIView instead of NSObject.

    To instantiate an instance of your timer in IB simply drag out a UIView drop it on your view controller’s view, and set it’s class to your timer’s class name.

    enter image description here

    Remember to #import your timer class in your view controller.

    Edit: for IB design (for code instantiation see revision history)

    I’m not very familiar at all with storyboard, but I do know that you can construct your interface in IB using a .xib file which is nearly identical to using the storyboard version; You should even be able to copy & paste your views as a whole from your existing interface to the .xib file.

    To test this out I created a new empty .xib named “MyCustomTimerView.xib”. Then I added a view, and to that added a label and two buttons. Like So:

    enter image description here

    I created a new objective-C class subclassing UIView named “MyCustomTimer”. In my .xib I set my File’s Owner class to be MyCustomTimer. Now I’m free to connect actions and outlets just like any other view/controller. The resulting .h file looks like this:

    @interface MyCustomTimer : UIView
    @property (strong, nonatomic) IBOutlet UILabel *displayLabel;
    @property (strong, nonatomic) IBOutlet UIButton *startButton;
    @property (strong, nonatomic) IBOutlet UIButton *stopButton;
    - (IBAction)startButtonPush:(id)sender;
    - (IBAction)stopButtonPush:(id)sender;
    @end
    

    The only hurdle left to jump is getting this .xib on my UIView subclass. Using a .xib dramatically cuts down the setup required. And since you’re using storyboards to load the timers we know -(id)initWithCoder: is the only initializer that will be called. So here is what the implementation file looks like:

    #import "MyCustomTimer.h"
    @implementation MyCustomTimer
    @synthesize displayLabel;
    @synthesize startButton;
    @synthesize stopButton;
    -(id)initWithCoder:(NSCoder *)aDecoder{
        if ((self = [super initWithCoder:aDecoder])){
            [self addSubview:
             [[[NSBundle mainBundle] loadNibNamed:@"MyCustomTimerView" 
                                            owner:self 
                                          options:nil] objectAtIndex:0]];
        }
        return self;
    }
    - (IBAction)startButtonPush:(id)sender {
        self.displayLabel.backgroundColor = [UIColor greenColor];
    }
    - (IBAction)stopButtonPush:(id)sender {
        self.displayLabel.backgroundColor = [UIColor redColor];
    }
    @end
    

    The method named loadNibNamed:owner:options: does exactly what it sounds like it does. It loads the Nib and sets the “File’s Owner” property to self. We extract the first object in the array and that is the root view of the Nib. We add the view as a subview and Voila it’s on screen.

    Obviously this just changes the label’s background color when the buttons are pushed, but this example should get you well on your way.

    Notes based on comments:

    It is worth noting that if you are getting infinite recursion problems you probably missed the subtle trick of this solution. It’s not doing what you think it’s doing. The view that is put in the storyboard is not seen, but instead loads another view as a subview. That view it loads is the view which is defined in the nib. The “file’s owner” in the nib is that unseen view. The cool part is that this unseen view is still an Objective-C class which may be used as a view controller of sorts for the view which it brings in from the nib. For example the IBAction methods in the MyCustomTimer class are something you would expect more in a view controller than in a view.

    As a side note, some may argue that this breaks MVC and I agree somewhat. From my point of view it’s more closely related to a custom UITableViewCell, which also sometimes has to be part controller.

    It is also worth noting that this answer was to provide a very specific solution; create one nib that can be instantiated multiple times on the same view as laid out on a storyboard. For example, you could easily imagine six of these timers all on an iPad screen at one time. If you only need to specify a view for a view controller that is to be used multiple times across your application then the solution provided by jyavenard to this question is almost certainly a better solution for you.

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

Sidebar

Related Questions

I'm currently making a whimsical iPhone app that will allow you to change your
I am making my first app which will use push notifications. I have decided
I'm currently making an iPhone app that has PDF viewing a crucial part of
I'm currently making some system that will gather statistical reports from different sites, for
I am currently making an app which works with images. I need to implement
I am currently making an app and website. Is there a system around that
My iPhone app currently uses core data. I want to create an online database
I am making an app that will make several HttpWebRequest objects and downloading my
I am making a blog app, and in that I want to provide the
I'm currently making an iphone web app based on Google App Engine (python). I

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.