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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:10:40+00:00 2026-06-01T15:10:40+00:00

I’m trying to decide on the best way to customize an initializer for UIView

  • 0

I’m trying to decide on the best way to customize an initializer for UIView so a value can be passed into the initializer and it is immediately stored as an iVar in the UIView subclass, as this value is required for the drawing in drawRect.

Currently I am using the default initWithFrame and the class that creates this UIView then sets its iVar to a value after calling initWithFrame, and it is in fact working; somehow the drawRect is getting this iVar’s value even though it is set after the UIView is initialized.

So I need either one of two things:

1) Confidence to know this is okay: that I can rely on drawRect somehow delaying the drawing for a bit so iVars are set by another object after UIView is initialized. In which case, just when in the UIView initializing does drawRect get called?

Or,

2) I need to pass value during initialization: is this the best way to create a custom initializer that takes more than (CGRect)frame as an argument for initWithFrame. Could it be as simple as doing this:

 myNewView=[[MyCustomView alloc] initWithFrame:myRect andMore:myValue];

And then in MyCustomView.m:

 - (id)initWithFrame:(CGRect)frame andMore:(int)someValue
  {
     self = [super initWithFrame:frame];
     if (self) {
       myIvar=someValue;
       ... //continue as normal
     }
  }

Would this code be a good approach? Then I would comfortably know that the value is there before drawRect is called.

  • 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-01T15:10:41+00:00Added an answer on June 1, 2026 at 3:10 pm

    1.) You can rely on drawRect:to be called after any init. In fact, drawRect: will be called when the view drawing and compositing is done, which usually happens as a result of a Core Animation commit (done at the end of the runloop if you modify any CALayer or UIView (which are always backed by a CALayer)).

    2.) This approach is better from an architectural point of view. If you consider this variable to be an integral part to the functionality of your class (and the class can’t work properly without it), it should definitely be an init parameter. you might also consider throwing an NSInternalInconsistencyException (or any other exception) inside other init-Methods and/or mark them as deprecated (init-Methods of the superclass will still work fine when called with super initWithFrame:).

    • 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
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.