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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:22:20+00:00 2026-05-27T03:22:20+00:00

Whenever I tap newButton my app crashes. I am using automatic reference counting. Edit:

  • 0

Whenever I tap “newButton” my app crashes. I am using automatic reference counting.

Edit: Just tried this in a different app and it works but does not work in my own.

Here is my code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *fullView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, 320, 480)];
    fullView.backgroundColor = [UIColor blackColor];
    [[self view] addSubview:fullView];

    UIImage* blackButton =[[UIImage imageNamed:@"UIButtonBlack.png"]stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];

    // Create button
    UIButton *newButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];

    // Set button content alignment
    newButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    newButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    // Set button title
    [newButton setTitle:@"Do Something" forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
    // Set button title color
    [newButton setTitleColor:[UIColor colorWithRed:255.0f/255.0 green:255.0f/255.0 blue:255.0f/255.0 alpha:1.0] forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
    // Add the background image
    [newButton setBackgroundImage:blackButton forState:UIControlStateNormal];
    // Add Events
    [newButton addTarget:self action:@selector(showScanner:) forControlEvents:UIControlEventTouchUpInside];
    // in case the parent view draws with a custom color or gradient, use a transparent color
    [newButton setBackgroundColor:[UIColor clearColor]];  
    // Set titleShadowColor this way (apparently, titleLabel.shadowcolor does not work)
    [newButton setTitleShadowColor:[UIColor colorWithRed:0.0f/255.0 green:0.0f/255.0 blue:0.0f/255.0 alpha:.75] forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];

    // Set button titleLabel properties  
    newButton.titleLabel.font = [UIFont fontWithName:@"PTSans-Bold" size:13.0];
    newButton.titleLabel.shadowOffset = CGSizeMake(1, 1);    

    [fullView addSubview:newButton];
}

- (void)showScanner:(id)sender
{
    NSLog(@"Do something…");
}

enter image description here

  • 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-27T03:22:20+00:00Added an answer on May 27, 2026 at 3:22 am

    You’re not keeping a reference to your view controller. You either need to

    • use a standard iOS view controller manager like UITabBarController or UINavigationController to display your views
    • keep a reference (in an instance variable) to this view controller within the class that you opened it from.

    Note that if you were to re-write this code with ARC turned off, you’d have a memory leak, instead of a crash.

    The problem is that all references to the view controller from the view are weak references, meaning that they don’t retain the controller*. So in your loading code ARC releases the view controller after you’ve made it and accessed its view, and it’s gone.

    Within your app you should keep track of all the view controllers, and access their views through them. Something like UINavigationController does this for you.

    *This is because the view controller is considered to have ownership over the view, and if the view controller retained the view and the view retained the view controller, there’d be a retain loop, and neither of them would ever be released.

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

Sidebar

Related Questions

Whenever I try to add this line it crashes my app. Am I not
I have a tap counter and this is the Who Wins code. The app
I am using a seekbar which had maximum of 2. However whenever i tap
Whenever I add a web reference for any Sharepoint web service in VS 2008,
I'm starting with the SDK and I am trying to set an app using
Whenever I tried to search about differences between classes and structs in C# or
Whenever I create an app ID on my provisioning profile, the 10 digit bundle
Whenever I create an app ID on my provisioning profile, the 10 digit bundle
In my application, whenever i tap a textfield the keypad will be visible, i
How do I get the ID of a list element? Whenever I use this.id

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.