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

  • Home
  • SEARCH
  • 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 869461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:18:34+00:00 2026-05-15T10:18:34+00:00

Alright, so I’m running into an issue with my code. What I have done

  • 0

Alright, so I’m running into an issue with my code. What I have done is subclassed UIButton so I can give it some more infomormation that pertain to my code. I have been able to create the buttons and they work great. Capiche.

However, one of the things I want my subclass to hold is a reference to a NSMangedObject. I have this code in my header file:

@interface ButtonSubclass : UIButton {
    NSManagedObjectContext *context;
    NSManagedObject *player;
}

@property (nonatomic, retain) NSManagedObject *player;
@property (nonatomic, retain) NSManagedObjectContext *context;

- (id)initWithFrame:(CGRect)frame andTitle:(NSString*)title;
//- (void)setPlayer:(NSManagedObject *)aPlayer;

@end

As you can see, it has a instance variable to the NSMangedobject I want it to hold (as well as the Context). But for the life of me, I can’t get it to hold that NSManagedObject. I run both the @synthesize methods in the Implementation file.

@synthesize context;
@synthesize player;

So I’m not sure what I’m doing wrong. This is how I create my button:

ButtonSubclass *playerButton = [[ButtonSubclass alloc] initWithFrame:frame andTitle:@"20"]; //works

        playerButton.context = self.context; //works
        playerButton.player = [players objectAtIndex:i]; //FAILS

And I have initilaized the players array earlier, where I get the objects. Another weird thing is that when it gets to this spot in the code, the app crashes (woot) and the the console output stops. It doesn’t give me any error, and notification at all that the app has crashed. It just… stops. So I don’t even know what the error is that is crashing the code, besides it has to do with that line up there setting the “player” variable. Thoughts and ideas? I’d love your wisdom!

  • 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-15T10:18:35+00:00Added an answer on May 15, 2026 at 10:18 am

    There is no particular reason why the code you have shouldn’t work. You can assign a NSManageObject instance as an attribute. The simplest explanation for the crash is an out of bounds error for the array. When you don’t get an error from the consoles its usually because the debugger has crashed. That is usually caused by having some kind of recursion that causes the debugger stack to overflow.

    However, putting data logic in a view element like a button is very, very poor practice. This completely breaks the MVC design pattern and will make your code fragile and hard to maintain. UI elements should be “dumb” and only understand how to display any data handed to them, they shouldn’t be involved in actually tracking the data itself. That is the function of the controller and the data model respectively.

    Ideally, the “player” object should be tracked by the data model which the controller then links to the appropriate UI button or other UI element. Putting the tracking of the player in the data model makes the design flexible which makes it easy to expand, reuse and maintain.

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

Sidebar

Related Questions

No related questions found

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.