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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:53:55+00:00 2026-05-16T11:53:55+00:00

This is driving me nuts… I’ve seen a few posts on similar issues but

  • 0

This is driving me nuts… I’ve seen a few posts on similar issues but can’t seem to make it work, so here it is:

it is a very simple example, I am just creating a custom UIButton in a custom view, then assigning it a custom action to respond to touch events. When I run the app, the button is created, but nothing happens when I try to click it.

Here is the code for my custom class:

#import "MySegmentedControl.h"


@implementation MySegmentedControl
@synthesize button1;

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


    CGFloat a = frame.origin.x+5;
    CGFloat b = frame.origin.y+5;
    CGFloat l = frame.size.width-15;
    CGFloat h = frame.size.height-15;

    CGFloat c = a+l;
    CGFloat d = b;

    CGRect frame2 = CGRectMake(a, b, l, h);


    UIButton *mybutton1 = [UIButton buttonWithType:UIButtonTypeCustom];
    mybutton1.frame = frame2;
    [mybutton1 setTitle:@"TEST" forState:UIControlStateNormal];
    mybutton1.titleLabel.textColor = [UIColor blackColor];
    mybutton1.backgroundColor = [UIColor blueColor];

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

    [self setButton1:mybutton1];

    self.userInteractionEnabled = YES;
    button1.userInteractionEnabled = YES;

    [self addSubview:button1];

    [self bringSubviewToFront:button1];


}

return self;
}

-(void)action {

    [button1 setTitle:@"done" forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor greenColor];
    NSLog(@"Working!");

}

And here is the appDelegate:

#import "MySegmentedControlAppDelegate.h"
#import "MySegmentedControl.h"

@implementation MySegmentedControlAppDelegate

@synthesize window;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    [window makeKeyAndVisible];

    MySegmentedControl *mycontrol = [[MySegmentedControl alloc] initWithFrame:CGRectMake(10,50,135,50)];

    [window addSubview:mycontrol];

    return YES;
}

as you can see from the custom class, from what I’ve read in other posts I’ve tried to add a few things to increase my chances of success:

  • enabling the custom’s view userinteraction
  • bringing the UIButton to front
  • making sure the button’s frame is completely included in the view’s frame
  • …

… yet it still doesn’t work!…

Any help would be greatly appreciated – thanks!

Edit: here is also the header for the custom view:

#import <UIKit/UIKit.h>


@interface MySegmentedControl : UIView {

    UIButton *button1;
}

@property (nonatomic, retain) IBOutlet UIButton *button1;

- (void)action;

@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-16T11:53:56+00:00Added an answer on May 16, 2026 at 11:53 am

    Can you join the header file of your custom view too?

    (Sorry to do that in answer, I don’t just have enough reputation for the moment).

    Edit (and real answer) :

    I’ve build a projet similar to yours and I think I’ve find the problem.

    Your button frame is not good. Look at this screenshot from simulator, custom UIView frame is in red (just add self.backgroundColor=[UIColor redColor]; in your view constructor) :

    alt text

    By using this code :

    CGFloat a = frame.origin.x+5;
    CGFloat b = frame.origin.y+5;
    CGFloat l = frame.size.width-15;
    CGFloat h = frame.size.height-15;
    

    You refer at the frame in windows base coordinates ! So your button is outside of your view and it can’t handle touch event.

    If you want to place your button dynamically, you should use this :

    CGFloat a = self.bounds.origin.x+5;
    CGFloat b = self.bounds.origin.y+5;
    GFloat l = self.bounds.size.width-15;
    CGFloat h = self.bounds.size.height-15;
    

    By using self.bounds you use view base coordinate and it’s working (see screenshots below) !

    alt text alt text

    Hope this helps !

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

Sidebar

Related Questions

This is driving me nuts, I've seen it before but can't replicate it or
This is driving me nuts. I can't seem to get the data template within
This is driving me nuts! A little piece of code that I can't seem
This is driving me nuts. I can't work it out stepping through with Firebug
This is driving me nuts, I'm sure it's rather simple but can't get it
This is driving me nuts. I've been through all the posts on stackoveflow, but
This is driving me nuts. I believe I asked this exact same question, but
This is driving me nuts. I've looked at all the relevant MSDN tutorials but
Alright, this is driving me nuts because my regex is working on Rubular, but
OK, this is driving me nuts. I'm sure it is trivial, but I've been

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.