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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:27:14+00:00 2026-05-27T01:27:14+00:00

How do I go about adding actions to buttons that are created in a

  • 0

How do I go about adding actions to buttons that are created in a UIView subclass?

Here is my code. This is my first time writing my views purely programmatically in a UIView subclass. I have create the “directions” button as follows:

directionsButton = [self buttonWithTitle:@"Directions" target:self selector:@selector(getDirections:) frame:directionsFrame image:buttonImage insertImage:directionsIcon];

Now how can I add the “getDirections” selector in my view controller? The following UIView subclass is added as a subview of BookViewController

#import "BookView.h"

#define GRAY_BOTTOM 44
#define PADDING 5
#define THUMB_WIDTH 50
#define THUMB_HEIGHT 43
#define DIRECTIONS_BUTTON_WIDTH 94
#define BUTTON_HEIGHT 34
#define BUTTON_VERTICAL_INSET 2

static NSArray* grayBottomButtons = nil;

@interface BookView (PrivateMethods)

- (void)setupSubviewsWithContentFrame:(CGRect)frameRect;
- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame image:(UIImage*)image insertImage:(UIImage*)insertImage;

@end

@implementation BookView

// @synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        [self setupSubviewsWithContentFrame:frame];

        self.backgroundColor = [UIColor clearColor]; // Make sure clear Background
        self.opaque = NO;

    }
    return self;
}

- (void)setupSubviewsWithContentFrame:(CGRect)frameRect {

    // Setup views

    // Create frame for white background
    CGRect frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    WhiteRoundedView *whiteBg = [[WhiteRoundedView alloc] initWithFrame:frame];

    // Create frame for gray bottom view
    CGRect gFrame = CGRectMake(0, (self.frame.size.height - GRAY_BOTTOM), self.frame.size.width, GRAY_BOTTOM);
    GrayBottomView *grayBg = [[GrayBottomView alloc] initWithFrame:gFrame];

    // Create salon thumbnail view
    UIImageView *salonThumb = [[UIImageView alloc] initWithFrame:CGRectMake(PADDING, PADDING, THUMB_WIDTH, THUMB_HEIGHT)];
    // Set salonThumb image
    salonThumb.image = [UIImage imageNamed:@"SalonThumb.png"];

    // Set salonName UILabel
    UILabel *salonName = [[UILabel alloc] initWithFrame:CGRectMake((PADDING*2 + THUMB_WIDTH), PADDING, 200, 15)];
    // Set text
    salonName.text = @"Salon Aria";
    // Set font
    salonName.font = [UIFont fontWithName:@"PTSans-Bold" size:15.0];
    // Set Alignment
    salonName.textAlignment = UITextAlignmentLeft;
    // Set font color
    salonName.textColor = [UIColor colorWithRed:0.0f/255.0 green:0.0f/255.0 blue:0.0f/255.0 alpha:1.0];


    // Set salonAddressLineOne UILabel (Line One)
    UILabel *salonAddressLineOne = [[UILabel alloc] initWithFrame:CGRectMake((PADDING*2 + THUMB_WIDTH), (PADDING + 15), 140, 13)];
    // Set text
    salonAddressLineOne.text = @"6325 Main St, Suite 140";
    // Set font
    salonAddressLineOne.font = [UIFont fontWithName:@"PTSans-Regular" size:13.0];
    // Set Alignment
    salonAddressLineOne.textAlignment = UITextAlignmentLeft;
    // Set font color
    salonAddressLineOne.textColor = [UIColor colorWithRed:40.0f/255.0 green:40.0f/255.0 blue:40.0f/255.0 alpha:1.0];

    // Set salonAddressLineTwo UILabel (Line Two)
    UILabel *salonAddressLineTwo = [[UILabel alloc] initWithFrame:CGRectMake((PADDING*2 + THUMB_WIDTH), (PADDING + 28), 140, 13)];
    // Set text
    salonAddressLineTwo.text = @"Woodridge, IL 60517";
    // Set font
    salonAddressLineTwo.font = [UIFont fontWithName:@"PTSans-Regular" size:13.0];
    // Set font color
    salonAddressLineTwo.textColor = [UIColor colorWithRed:40.0f/255.0 green:40.0f/255.0 blue:40.0f/255.0 alpha:1.0];
    // Set Alignment
    salonAddressLineTwo.textAlignment = UITextAlignmentLeft;

    // Set the background image with stretchable type for all buttons on this view
    UIImage *buttonImage = [[UIImage imageNamed:@"UIButton.png"] stretchableImageWithLeftCapWidth:2 topCapHeight:2];

    // Set the insert image (directions icon)
    UIImage *directionsIcon = [[UIImage imageNamed:@"DirectionsIcon.png"] stretchableImageWithLeftCapWidth:2 topCapHeight:2];
    // Set the directions button frame
    CGRect directionsFrame = CGRectMake(self.frame.size.width - PADDING - DIRECTIONS_BUTTON_WIDTH, PADDING, DIRECTIONS_BUTTON_WIDTH, BUTTON_HEIGHT);
    // Add the button
    directionsButton = [self buttonWithTitle:@"Directions" target:self selector:@selector(getDirections:) frame:directionsFrame image:buttonImage insertImage:directionsIcon];

    // Create the salonMap with frame
    salonMap = [[MKMapView alloc] initWithFrame:
                CGRectMake(PADDING, (PADDING*2 + THUMB_HEIGHT), (self.frame.size.width - PADDING*2), (self.frame.size.height - THUMB_HEIGHT - PADDING*3 - GRAY_BOTTOM))];

    // Add view in proper order and location

    [self addSubview:whiteBg];

    [whiteBg addSubview:grayBg];

    [whiteBg addSubview:salonThumb];

    [whiteBg addSubview:salonName];

    [whiteBg addSubview:salonAddressLineOne];

    [whiteBg addSubview:salonAddressLineTwo];

    [whiteBg addSubview:directionsButton];

    [whiteBg addSubview:salonMap];

    [self setNeedsDisplay];

}

- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame image:(UIImage*)image insertImage:(UIImage*)insertImage
{
    // Create button
    UIButton *button = [[UIButton alloc] initWithFrame:frame];

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

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

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

    // If there is an insertImage icon
    if(insertImage != nil) 
    {
        // padding between image and text
        int seperate = 6; 
        // Image and title frame (used to center text/image)
        int width = (button.titleLabel.frame.size.width + insertImage.size.width + seperate);
        int buttonWidth = button.frame.size.width;
        int xPadding = (buttonWidth - width)/2;
        int yPadding = (button.frame.size.height - insertImage.size.height)/2;

        // Create an image view for the image (standard icon is 12x13)
        UIImageView *insertImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPadding, yPadding, insertImage.size.width, insertImage.size.height)];
        // add the insertImage to the view
        [insertImageView setImage:insertImage];
        // add the insertImageView to the button
        [button addSubview:insertImageView];
        // Add offset with title
        button.titleEdgeInsets = UIEdgeInsetsMake(BUTTON_VERTICAL_INSET, insertImage.size.width + seperate, 0, 0);
    }
    else 
    {
        // No image so no horizontal offset but PTSans font needs a 2 vertical offset
        button.titleEdgeInsets = UIEdgeInsetsMake(BUTTON_VERTICAL_INSET, 0, 0, 0);
    }
    return button;
}


@end
  • 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-27T01:27:15+00:00Added an answer on May 27, 2026 at 1:27 am

    From the view controller, for each button in the view do something like:

    [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
    

    Alternatively you can set up the connection within Interface Builder by ctrl-dragging from the button to the header file of the file owner (the view controller). This will ask you to define a method name, and link it all up for you.

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

Sidebar

Related Questions

How would I go about adding enctype=multipart/form-data to a form that is generated by
How would I go about adding the Spent Time as a column to be
Reading this: http://guides.rubyonrails.org/routing.html#adding-more-restful-actions What does it mean to add a 'member route'? or do
I understand that are similar question on StackOverflow about this problem, but none of
How would one go about adding a submenu item to the windows explorer context
I'm thinking about adding some kind of copy protection to one of my tools.
How would I go about adding a dynamic .......... to a restaurant menu in
How would someone go about adding check boxes to only the children of a
I see a couple of previously answered questions about adding an item to an
I'm trying to generate POJO's from mapping xml files. I read something about adding

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.