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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:31:13+00:00 2026-05-31T15:31:13+00:00

I’m adding two subviews, which are stored by properties, to my view. When adding

  • 0

I’m adding two subviews, which are stored by properties, to my view. When adding the subviews to my view the subViews are seemingly deallocated after my setup method is called. The end result is that the views never get displayed. Now, if I change my properties to strong as opposed to weak I retain a reference to the views and they now show on screen. So what’s going on here? Why is the addSubview: and insertSubview: not retaining the subviews? See the code below:

btw, I’m using iOS5 with ARC (hence the strong and weak stuff)

#import "NoteView.h"
@interface NoteView() <UITextViewDelegate>
@property (weak, nonatomic) HorizontalLineView *horizontalLineView;  // custom subclass of UIView that all it does is draw horizontal lines
@property (weak, nonatomic) UITextView *textView;
@end

@implementation NoteView
@synthesize horizontalLineView = _horizontalLineView;
@synthesize textView = _textView;

#define LEFT_MARGIN 20
- (void)setup
{
    // Create the subviews and set the frames
    self.horizontalLineView = [[HorizontalLineView alloc] initWithFrame:self.frame];
    CGRect textViewFrame = CGRectMake(LEFT_MARGIN, 0, self.frame.size.width, self.frame.size.height);
    self.textView = [[UITextView alloc] initWithFrame:textViewFrame];


    // some addition setup stuff that I didn't include in this question...

    // Finally, add the subviews to the view
    [self addSubview:self.textView];
    [self insertSubview:self.horizontalLineView atIndex:0];
}

- (void)awakeFromNib
{
    [super awakeFromNib];

    [self setup];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setup];
    }
    return self;
}
  • 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-31T15:31:14+00:00Added an answer on May 31, 2026 at 3:31 pm

    Here’s a line of your code:

    self.horizontalLineView = [[HorizontalLineView alloc] initWithFrame:self.frame];
    

    Recall that the horizontalLineView property is weak. Let’s walk through what really happens in that line, with the extra code that ARC generates. First, you send the alloc and initWithFrame: methods, getting back a strong reference:

    id temp = [[HorizontalLineView alloc] initWithFrame:self.frame];
    

    At this point, the HorizontalLineView object has a retain count of 1. Next, because you used dot-syntax to set the horizontalLineView property, the compiler generates code to send the setHorizontalLineView: method to self, passing temp as the parameter. Since the HorizontalLineView property is declared weak, the setter method does this:

    objc_storeWeak(&self->_horizontalLineView, temp);
    

    That sets self->_horizontalLineView equal to temp, and puts &self->_horizontalLineView on the object’s list of weak references. But it does not increment the retain count of the HorizontalLineView object.

    Finally, because the temp variable is no longer needed, the compiler generates this:

    [temp release];
    

    That lowers the HorizontalLineView object’s retain count to zero, so it deallocates the object. During deallocation, it walks down the list of weak references, and sets each one to nil. So self->_horizontalLineView becomes nil.

    The way to fix this is to make the temp variable explicit, so that you can extend its lifetime until after you have added the HorizontalLineView object to its superview, which retains it:

    HorizontalLineView *hlv = [[HorizontalLineView alloc] initWithFrame:self.frame];
    self.horizontalLineView = hlv;
    // ...
    [self insertSubview:hlv atIndex:0];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

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.