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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:29:18+00:00 2026-05-13T13:29:18+00:00

Lets say i have a little bit of code I would like to repeat

  • 0

Lets say i have a little bit of code I would like to repeat a number of times. How should I best include this in my iPhone app to only have to write this once?

Its a typical TableView Controller App.

//Set Icon
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(30,25,20,20)];
        imgView.image = [UIImage imageNamed:@"ico-date.png"];
        [self.view addSubview:imgView];

Regards

  • 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-13T13:29:19+00:00Added an answer on May 13, 2026 at 1:29 pm

    Your options:

    1) Create a static C function to do it

    static UIImageView* myfunc(CGFloat x, CGFloat y, CGFloat w, CGFloat h,
      NSString* name, NSString* type, UIView* parent) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:
          CGRectMake(x,y,w,h)]; 
        imgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
          pathForResource:name ofType:type]];
        [self.view addSubView:imgView];
        [imgView release];
        return imgView;
    }
    

    2) Create a C macro

    #define CREATE_ICON_VIEW(parent,x,y,w,h,name) \
      ...
    

    3) Create an Objective-C static method

    // in @interface section
    + (UIImageView*)addIconWithRect:(CGRect)rect name:(NSString*)name
      type:(NSString*)type toView:(UIView*)view;
    
    // in @implementation section
    + (UIImageView*)addIconWithRect:(CGRect)rect name:(NSString*)name
      type:(NSString*)type toView:(UIView*)view {
      UIImageView *imgView = [[UIImageView alloc] initWithFrame:rect];
      imgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
        pathForResource:name ofType:type]];
      [self.view addSubView:imgView];     }
      [imgView release];
      return imgView;
    }
    
    // somewhere in app code
    [MyClass addIconWithRect:CGMakeRect(0,0,32,32) name:@"ico-date"
      type:@"png" toView:parentView];
    

    4) Create an Objective-C category on UIImage or UIImageView

    5) Create a method on the view that is to have a UIImageView added

    - (void)addIconWithRect:(CGRect)rect name:(NSString*)name type:(NSString*)type {
      UIImageView *imgView = [[UIImageView alloc] initWithFrame:rect];
      imgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
        pathForResource:name ofType:type]];
      [self.view addSubView:imgView];     }
      [imgView release];
      return imgView;
    }
    

    6) Create a Helper class

    Like option (3) but put the static methods in a separate class that’s just for utility methods for repeated sections of code eg call the helper class “UIUtils”.

    7) Use a C inline function

    static inline UIImageView* myfunc(CGFloat x, CGFloat y, CGFloat w, CGFloat h,
      NSString* name, NSString* type, UIView* parent) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:
          CGRectMake(x,y,w,h)]; 
        imgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
          pathForResource:name ofType:type]];
        [self.view addSubview:imgView]; 
        [imgView release];
        return imgView;
    }
    

    8) Use a loop to execute the same code repeatedly

    9) Use an ordinary non-static Objective-C method

    Personally I would go for none of these for your particular example and just write it out long-hand, unless it is repeated more than ten times in a file in which case I might go for (3). If its used in a lot of files I might go for (6).

    Edit: Expanded descriptions for (3) and (6) and note about when I use (6).

    Edit: Added options 8 & 9. Fixed memory leaks and some mistakes.

    Edit: Updated code to use imageWithContentsOfFile instead of imageNamed.

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

Sidebar

Related Questions

Lets say I have an array like this: string [] Filelist = ... I
I want to do THIS , just a little bit more complicated: Lets say,
So lets say I have a table that looks something like this: ItemName ProductType
Lets say I have a set of model classes like this: public class Person
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Lets say we have something like: <div class=row> <div class=box> <a class=more href=#more/> </div>
I'm having a little confusion when distinguishing this in an as3 file lets say
Lets say I have a String: Go to this page: http://mysite.com/?page=1 , and I
Lets say you have 3 or 4 site features like photos, videos, wall posts,
i'm a little bit confused with memory management in view controllers. Lets say i

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.