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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:41:31+00:00 2026-05-28T03:41:31+00:00

As the title describes I’m looking to add custom properties for every UIViewController and

  • 0

As the title describes I’m looking to add custom properties for every UIViewController and every subclass (like UITableViewController). My first thought was to create a category, but then I realized one cannot add ivars in them, so I can’t synthesize the properties. If I simply subclass UIViewController and add stuff there, It wouldn’t affect other subclasses.

Basically my purpose with this is to add a custom toolbar to my ´UINavigationController´ subclass (No, I can’t use the default for various reasons). It needs to ask each viewController if it wants a toolbar, and – in that case – for an array of toolbarItems. It’s kind of like how the UIToolbar works I believe?

How would you do it?

  • 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-28T03:41:32+00:00Added an answer on May 28, 2026 at 3:41 am

    Adding properties (with backing storage) is possible using Objective-C’s associated object API. I even have a little macro I wrote for the purpose. But before the macro, here’s what it would look like “unrolled”:

    #import <objc/runtime.h>
    
    @interface UIViewController (SOAdditions)
    @property (atomic, readwrite, copy) NSString* myProperty;
    @end
    
    static void * const kMyPropertyAssociatedStorageKey = (void*)&kMyPropertyAssociatedStorageKey; 
    
    @implementation UIViewController (SOAdditions)
    
    - (void)setMyProperty:(NSString *)myProperty
    {
        objc_setAssociatedObject(self, kMyPropertyAssociatedStorageKey, myProperty, OBJC_ASSOCIATION_COPY);
    }
    
    - (NSString*)myProperty
    {
         return objc_getAssociatedObject(self, kMyPropertyAssociatedStorageKey);
    }
    
    @end
    

    You can find the documentation for associated storage here. You should be aware that there is a performance (speed and memory) penalty for using associated objects. They’re a neat trick, but you might want to ask yourself if there’s a better way to do whatever it is you’re trying to do. (The neatest part of the trick, if you ask me, is that the runtime will handle the -releases on -dealloc for you, according to the policy you specify; See the docs for more info.)

    Now here’s the macro:

    #ifndef ASSOCIATED_STORAGE_PROPERTY_IMP
    #define THREE_WAY_PASTER_INNER(a, b, c) a ## b ## c
    #define THREE_WAY_PASTER(x,y,z) THREE_WAY_PASTER_INNER(x,y,z)
    
    #define ASSOCIATED_STORAGE_PROPERTY_IMP(type, setter, getter, policy) \
    static void * const THREE_WAY_PASTER(__ASSOCIATED_STORAGE_KEY_, getter, __LINE__) = (void*)&THREE_WAY_PASTER(__ASSOCIATED_STORAGE_KEY_, getter,__LINE__); \
    \
    - (type)getter { return objc_getAssociatedObject(self, THREE_WAY_PASTER(__ASSOCIATED_STORAGE_KEY_, getter,__LINE__) ); } \
    \
    - (void)setter: (type)value { objc_setAssociatedObject(self, THREE_WAY_PASTER(__ASSOCIATED_STORAGE_KEY_, getter,__LINE__) , value, policy); } \
    
    #endif
    

    If you pop that in your header file, then the above example can be reduced to this:

    #import <objc/runtime.h>
    
    @interface UIViewController (SOAdditions)
    
    @property (atomic, readwrite, copy) NSString* myProperty;
    
    @end
    
    @implementation UIViewController (SOAdditions)
    
    ASSOCIATED_STORAGE_PROPERTY_IMP(NSString*, setMyProperty, myProperty, OBJC_ASSOCIATION_COPY)
    
    @end
    

    It should go without saying that the policy (i.e. retain/copy/assign, atomic/nonatomic) you declare in the @property declaration needs to match the policy that you use when using the macro (and/or calling the underlying API, if you’re not using the macro) otherwise, you’ll end up leaking memory (or crashing.)

    Also, I’ll say it again for emphasis. This trick isn’t “free” so please be sure to measure performance and make sure that whatever benefits you’re getting by using this are worth it.

    EDIT: I’m going to backtrack a bit on my emphatic and dire warnings about the performance penalties of associated object storage. There is a penalty, but a quick investigation tells me that properties implemented this way are not all that much worse than their @synthesized and ivar-backed equivalents. The test is a bit contrived, but with 10,000,000 objects, each with 5 associative-storage-backed iVars, I’m seeing ~30% slower performance setting and getting. That’s really not all that dire, IMHO. I was expecting much worse. The time spent doing the memory management (retains, copies) associated with the set operations is dwarfing the overhead from the associative lookups.

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

Sidebar

Related Questions

as the title describes i would like to create an image upload with ajax
First of all, I'm not sure if the title accurately describes what I'm referring
The following test case describes my problem. CREATE TABLE the_table (id INT, Title CHAR(10));
As title describes, I have 3 classes: #include <iostream> using namespace std; class first
As the title describes - I am looking for some good OpenGL ES resources.
As the title describes I am looking for a way to query for the
As the title describes, what are the different doctypes available and what do they
As the title describes, I'm trying to group up a grid of 3x3 radio
As the title describes. I need to see if the machine accessing my page
Not sure the title fully describes the problem/question I'm trying to ask, sorry. One

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.