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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:16:43+00:00 2026-06-16T13:16:43+00:00

I am new to Objective-C and could use some help with a problem that

  • 0

I am new to Objective-C and could use some help with a problem that has left me stumped. I call it the problem of the disappearing variable. I will attempt to describe it below.

I’m attempting to create my own tab bar controller and view as I’ve concluded that UITabBarController and UITabBar don’t allow me to do what I need to do (I need to change the appearance of the tab bar quite drastically).

I’ve subclassed UIViewController and UIView to create my classes SYTabBarController and SYTabBar, respectively.

In SYTabBar, I’ve written a method to programmatically add UIButtons to my view:

- (UIButton*) addButtonWithFrame:(CGRect)frame action:(SEL)action target:(id)target imageToken:(NSString*)imageToken
{
    UIButton *b = [[UIButton alloc]initWithFrame:frame];
    [b addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    UIImage
    *unselectedImage = [UIImage imageNamed:APPEND_STRING(@"TBI+Unselected+", @"imageToken")],
    *selectedImage   = [UIImage imageNamed:APPEND_STRING(@"TBI+Selected+", @"imageToken")],
    *selectedBgImage = [UIImage imageNamed:@"TBI+SelectedBG"];

    [b setImage:unselectedImage forState:UIControlStateNormal];
    [b setImage:selectedImage forState:UIControlStateHighlighted];
    [b setImage:selectedImage forState:UIControlStateSelected];
    [b setBackgroundImage:selectedBgImage forState:UIControlStateHighlighted];
    [b setBackgroundImage:selectedBgImage forState:UIControlStateSelected];
    [b setShowsTouchWhenHighlighted:NO];
    [b setAdjustsImageWhenHighlighted:NO];
    [b setAdjustsImageWhenDisabled:NO];
    [self addSubview:b];

    return b;
}

I invoke this method in SYTabBarController to create five buttons:

    UIButton *button1 = [self.tabBar addButtonWithFrame:CGRectMake(0, 0, 64, 45)
                                                 action:@selector(changeView:)
                                                 target:self
                                             imageToken:@"Popular"];

    UIButton *button2 = [self.tabBar addButtonWithFrame:CGRectMake(64, 0, 64, 45)
                                                 action:@selector(changeView:)
                                                 target:self
                                             imageToken:@"Feed"];

    UIButton *button3 = [self.tabBar addButtonWithFrame:CGRectMake(64*2, 0, 64, 45)
                                                 action:@selector(changeView:)
                                                 target:self
                                             imageToken:@"Capture"];

    UIButton *button4 = [self.tabBar addButtonWithFrame:CGRectMake(64*3, 0, 64, 45)
                                                 action:@selector(changeView:)
                                                 target:self
                                             imageToken:@"Likes"];

    UIButton *button5 = [self.tabBar addButtonWithFrame:CGRectMake(64*4, 0, 64, 45)
                                                 action:@selector(changeView:)
                                                 target:self
                                             imageToken:@"Profile"];

Then, still in SYTabBarController, I try to take these five buttons and add them as keys in a dictionary which is stored as a property (see code below). The dictionary contains UIViewController instances keyed to the buttons. The idea is that when one of the five buttons is pressed, it will call the selector changeView:, which in turn will retrieve the respective UIViewController instance from the dictionary and display its view.

    self.viewControllersKeyedToButtons = [NSDictionary dictionaryWithObjectsAndKeys:

                                          rootViewController,
                                          button1,

                                          [[SYViewController_Feed alloc] init],
                                          button2,

                                          [[SYViewController_Capture alloc] init],
                                          button3,

                                          [[SYViewController_Likes alloc] init],
                                          button4,

                                          [[SYViewController_Profile alloc] init],
                                          button5,

                                          nil];

My code crashes right when I initialize the dictionary. I get this error message:

2012-12-27 02:54:28.498 Smuvy[17931:c07] -[UIButton copyWithZone:]: unrecognized selector sent to instance 0x717fc10
2012-12-27 02:54:28.500 Smuvy[17931:c07] * Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[UIButton copyWithZone:]: unrecognized selector sent to instance 0x717fc10’
*
First throw call stack:
(0x174c012 0x1223e7e 0x17d74bd 0x173bbbc 0x173b94e 0x17ce714 0x1712b55 0x172d5a8 0x65d6 0x2c6e 0x1687b7 0x168da7 0x169fab 0x17b315 0x17c24b 0x16dcf8 0x3676df9 0x3676ad0 0x16c1bf5 0x16c1962 0x16f2bb6 0x16f1f44 0x16f1e1b 0x1697da 0x16b65c 0x2aad 0x29d5)
libc++abi.dylib: terminate called throwing an exception
(lldb)

Now here’s the mystery. When I track the variable button5 in my debugger, I notice that right before I try to instantiate the dictionary, it disappears! No wonder I get an error message.

As I am new to Objective-C (and to C) I don’t know much about memory management and I imagine I’ve done something wrong with my variables. I’ve run Instruments and interestingly enough, I see all UIButton instances alive and well. It seems, thus, that the button is there, it’s just the pointer that has somehow gone kaput.

Any help you can provide 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-06-16T13:16:44+00:00Added an answer on June 16, 2026 at 1:16 pm

    When you create an NSDictionary, it makes copies of the keys that you provide, by calling -copy, which calls -copyWithZone:. These methods are part of the NSCopying protocol.

    Unfortunately, UIButton does not conform to that protocol. It isn’t copyable. When NSDictionary tries to send the -copy message to it, an exception is raised, as you have discovered.

    Alternatives:

    • Use an NSMapTable instead.

    • Wrap the keys in an NSValue, using +[NSValue valueWithNonretainedObject:] or +[NSValue valueWithPointer:]. (If you’re using ARC, NSValue will not ensure that your object is retained, so you will need to make a strong reference to the object some other way.)

    • Use some other method to determine a key that the view controller and the UIButton can share. (For instance, have them both refer to the same string.)

    • Don’t use a map at all. Perhaps keep your view controllers in an array, and use the tag on the UIButton to store the index of the corresponding view controller in the array.

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

Sidebar

Related Questions

I am relatively new to Objective C and need some array help. I have
I could use some help here, I can't figure out why the populate method
I was hoping you could help answer an Objective-C based question for me that
Note: I'm relatively new to Objective-C and am coming from Java and PHP. Could
I use sharekit with mail/twitter/facebook and I am really new to objective-c. sharekit works
I'm fairly new to Objective-C, and it would be really helpful if someone could
I am newcomer in Objective C. I have an object, that creates new thread
I'm new to Objective-C and XCode, but I was happy to see that XCode
Well, I decided that it's time to learn a new skill, programming objective-c. My
Situation : C#, .NET 3.5, WinForms Objective : Form1 has a Button that can

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.