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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:38:47+00:00 2026-06-15T08:38:47+00:00

I am trying to add UIButton objects to an array, but they fail to

  • 0

I am trying to add UIButton objects to an array, but they fail to do so. Whenever I call [pixels count] or [colors count] it returns 0. I tried using [self.arrayName addObject:myObject] and [arrayName addObject:myObject] but neither seem to work. I’m pretty new to Objective-C so it probably seems dumb on my part, but this has been stumping me for over an hour.

Here is the code for ViewController.h

 #import <UIKit/UIKit.h>

 @interface ViewController : UIViewController {
 NSMutableArray *pixels;
 NSMutableArray *colors;
 }
 @property (nonatomic, retain) NSMutableArray *pixels;
 @property (nonatomic, retain) NSMutableArray *colors;
 @end

And here is the relevant code from ViewController.m

 int x = 30;
 int y = 60;
 for(int i=0; i<10; i++ ) {
      UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,y,20,20)];
      [self.pixels addObject:button];
      x += 20;
      y += 20;
 }

I have the whole project zipped up which can be downloaded here:
http://mdl.fm/pixelated.zip

Thanks in advance to anyone who can help!

  • 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-15T08:38:48+00:00Added an answer on June 15, 2026 at 8:38 am

    Try adding this in before you try to use the arrays:

    NSMutableArray *pixels = [[NSMutableArray alloc] init];
    

    Arrays in Obj-C need to be initialized before being used. Since calling methods on a nil instance just returns zero in Obj-C, it’s easy to do this and not notice until your array isn’t storing what you think it should.

    Edit to add info from comments:

    You can put the initialization into the -ViewDidLoad method, so that they are ready to go once the ViewController itself is ready. Make sure you retain them, so they don’t get automatically garbage-collected.

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

Sidebar

Related Questions

I'm trying to add a UIButton to a UIView, but am having some trouble
I'm trying to add a shadow to a UIButton layer, but for some reason
I'm trying to add a UIButton to a UIView but I'm getting this error
I am trying to add uibutton inside the imageview. But it was not displayed
Im new to asp.net mvc. I'm trying add new model class but it got
Trying to add a blank sample app for a rails tutorial to GitHub, but
Trying to add a class object into a List using reflection, but when invoking
I am trying to add the UIButton to UIScrollView . I have added the
I am trying to add an array of 2 buttons to the right of
. Hello, I am trying to add a UIButton and other items to my

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.