Iphone UIButton not working in nested UIViews - iphone

This is so damn simple im sure! Im missing something and im exhausted from trying to fix it. hopefully someone can help.
The Button in CharacterView.m works but the button nested down in CharacterMale.m does not. I'm not using IB everything is done progmatically.
What would cause one button to work and other not?
/////////////////////////////////////////////////////////////////////////////////
CharacterController.m
/////////////////////////////////////////////////////////////////////////////////
#import "CharacterController.h"
#import "CharacterView.h"
#implementation CharacterController
- (id)init {
NSLog(#"CharacterController init");
self = [ super init ];
if (self != nil) {
}
return self;
}
- (void)loadView {
[ super loadView ];
characterView = [ [ CharacterView alloc ] init];
self.view = characterView;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[super dealloc];
}
#end
/////////////////////////////////////////////////////////////////////////////////
CharacterView.m
/////////////////////////////////////////////////////////////////////////////////
#import "CharacterView.h"
#import "CharacterMale.h"
#implementation CharacterView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
characterMale = [ [ CharacterMale alloc ] init];
[self addSubview: characterMale];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 200, 200, 100);
[button setImage:[UIImage imageNamed:#"btnCharSelect.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(ApplyImage:) forControlEvents:UIControlEventTouchUpInside];
[ self addSubview: button ];
}
return self;
}
- (void)drawRect:(CGRect)rect {
}
-(void)ApplyImage:(id)sender
{
NSLog(#"CharacterView button works");
}
- (void)dealloc {
[super dealloc];
}
#end
/////////////////////////////////////////////////////////////////////////////////
CharacterMale.m
/////////////////////////////////////////////////////////////////////////////////
#import "CharacterMale.h"
#import "CharacterController.h"
#implementation CharacterMale
- (id)init {
self = [ super init];
if (self != nil) {
UIImage *image = [UIImage imageNamed:#"charMale.png"];
imageView = [[ UIImageView alloc] initWithImage:image];
[image release];
[ self addSubview: imageView ];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200, 100);
[button setImage:[UIImage imageNamed:#"btnCharSelect.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(ApplyImage:) forControlEvents:UIControlEventTouchUpInside];
[ self addSubview: button ];
}
return self;
}
-(void)ApplyImage:(id)sender
{
NSLog(#"CharacterMal button works");
}
- (void)dealloc {
[super dealloc];
}
#end

FINALLY!!!!!!
I had to init all the views with initWithFrame and pass in valid frame rects. Init should be used with controllers and initWithFrame passing rects for UIViews!!!!
characterView = [ [ CharacterView alloc ] initWithFrame:CGRectMake(0, 0, 480, 320)];
then
characterMale = [ [ CharacterMale alloc ] initWithFrame:frame];

Another thing to consider is that UIButton subviews that have been added a UIImageView don't work at all. You need to create a UIView that you add both the image view and the buttons to.
This is probably because interaction is turned off by default on image views, but I've not checked this.

Does the new view accept user interaction? In other words, is userInteractionEnabled enabled on the view "Characters"?

I had a similar issue when tried to add the button during the initialization of an UIView with a frame of CGRectZero:
#implementation SomeView
...
- (id)initWithTitleImage:(UIImage *) newTitleImage {
// BROKEN: frame with CGRectZero.
self = [super initWithFrame:CGRectZero];
if (self) {
UIButton *someButton = ...;
[self addSubview someButton];
}
}
Once I changed the frame to a proper rectangle, the button worked:
#implementation SomeView
...
- (id)initWithTitleImage:(UIImage *) newTitleImage {
// WORKING: frame with proper CGRect.
self = [super initWithFrame:CGRectMake(0, 0, 480, 320)];
if (self) {
UIButton *someButton = ...;
[self addSubview someButton];
}
}

I have had luck with a selector for a UIButton created in drawRect by using UIControlEventTouchDown instead of the popular UIControlEventTouchUpInside.

For me the issue was caused because, the origin of the button frame was bigger then the parent frame.

Related

Button hidden behind my BaseViewController in iPhone

I am facing weird problem. I have a baseViewController which extends with UIViewController.
in My BaseView controller i have a custom HeaderBar (UIImageView) on top of it.
I then made a new ViewController and extends that new ViewController with baseViewController. The headerBar in BaseViewController is appearing on my UIViewController. But now if I add any subview in my NEWViewController over that headerBar then it does not come over that whereas it goes behind the header bar.
Can anybody suggest what to do.
EDITED
Here is my code.
BASEViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
CGFloat xOffset = 0;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
xOffset = 224;
}
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
headerBarView = [self addHeaderBar];
//[self.view addSubview:headerBarView];
[self.view insertSubview:headerBarView belowSubview:self.view];
}
return self;
}
-(UIView *)addHeaderBar{
UIView *header_bar = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 50.0)];
//HeaderBar Image
UIImageView *headerBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0.0, 320, 50.0)];
[headerBar setImage:[UIImage imageNamed:#"top_bar"]];
[header_bar addSubview:headerBar];
return header_bar;
}
NEWViewController Extended With BaseViewController
//Back Button
back = [UI getActionBarButtonWithName:[NSString stringWithFormat:#"Back"] andCGRect:CGRectMake(7.0, 100.0, 55.0, 31.0) andEdgeInset:UIEdgeInsetsMake(0.0, 9.0, 2.0, 0.0) withBackGround:[UIImage imageNamed:#"back_button"]];
[self.view bringSubviewToFront:back];
+(UIButton *)getActionBarButtonWithName:(NSString *)name andCGRect:(CGRect)rect andEdgeInset:(UIEdgeInsets)edgeInset withBackGround:(UIImage *)background{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = rect;
[button setBackgroundImage:background forState:UIControlStateNormal];
[button setTitleEdgeInsets:edgeInset];
[button setTitle:name forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:13.0]];
[button setUserInteractionEnabled:YES];
return button;
}
if you are having many views, you can also use any one of these -
[self.view insertSubview:<#(UIView *)#> aboveSubview:<#(UIView *)#>]
[self.view insertSubview:<#(UIView *)#> atIndex:<#(NSInteger)#>]
[self.view insertSubview:<#(UIView *)#> belowSubview:<#(UIView *)#>]
hope it will help you.
Try using
[self.view bringSubviewToFront:button];
New UIVIew override your base viewcontroller,you can set frame of new view and then add subview.
Ex. UIView* tempView=[UIView alloc]initWithFrame:CGRectMake(0,44,320,420)];
[self.view addSubView:temView];

How can i load a different view on same method in iphone

First Class
#implementation WatchViewController
- (void)viewDidLoad
{
[super viewDidLoad];UIButton *watch1 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
watch1.frame = CGRectMake(5.0, 10.0, 140.0, 170.0);
[watch1 setBackgroundImage:[UIImage imageNamed:#"Watch1.png"] forState: UIControlStateNormal];
[watch1 addTarget:self action:#selector(WatchesPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[scr addSubview:watch1];
UIButton *watch2 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
watch2.frame = CGRectMake(170.0, 10.0, 140.0, 170.0);
[watch2 setBackgroundImage:[UIImage imageNamed:#"watch2.png"] forState: UIControlStateNormal];
[watch2 addTarget:self action:#selector(WatchesPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[scr addSubview:watch2];
}
Method:
- (IBAction)WatchesPreviewButtonPressed:(id)sender {
WatchPreviewViewController *watchesPreviewView = [[WatchPreviewViewController alloc] initWithNibName:#"WatchPreviewViewController" bundle:nil];
[self.navigationController pushViewController:watchesPreviewView animated:YES];
[watchesPreviewView release];
}
Second Class:
#implementation WatchPreviewViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 46, 320, 384)];
[self.view addSubview:scr];
NSArray* ds =[NSArray arrayWithObjects:
[NSArray arrayWithObjects:[self getPath:#"1a"],[self getPath:#"1b"],[self getPath:#"1c"],[self getPath:#"1d"],nil],
[NSArray arrayWithObjects:[self getPath:#"2a"],[self getPath:#"2b"],[self getPath:#"2c"],[self getPath:#"2d"],nil],nil];
SSView* sv =[SSView createWithFrame:CGRectMake(0, 0, 320, 380) ds:ds];
if(??????????????????) //what condition is required for watch1?
{
sv.curIndexPath =[NSIndexPath indexPathForRow:0 inSection:0];
[scr addSubview:sv];
}
else if(?????????????????) //what condition is required watch2?
{
sv.curIndexPath =[NSIndexPath indexPathForRow:1 inSection:0];
[scr addSubview:sv];
}
In class one i have two images of watch and i want load next page view on the click of watch. for this i am using method WatchesPreviewButtonPressed.
In second Class i am creating the page for loading on button click.
in second class i have a scroll view inside the view. and i have array for images.
i want display different image on next watch click event.
any one please me, i am new in iphone development.
Set the tag property on each button when you create them in (void)viewDidLoad
:
watch1.tag = 1 //new code
[scr addSubview:watch1]; //existing code
watch2.tag = 2 //new code
[scr addSubview:watch2]; //existing code
In your WatchPreviewViewController.h, make a property in the #interface section:
#property (assign) int watchType;
Then in - (IBAction)WatchesPreviewButtonPressed:(id)sender set the property according to which button is pressed:
watchesPreviewView.watchType = sender.tag
(you might have to typecast sender: (UIView*)sender.tag, I am not testing this live)
Now your if(??????????????????) test is if (self.watchType == 1)
Create an enum like style in WatchPreviewViewController and create your own init method.
typedef enum WatchPreviewViewControllerStyleType {
WatchPreviewViewControllerStyleType1 = 0,
WatchPreviewViewControllerStyleType2 = 1
}WatchPreviewViewControllerStyleType;
#interface WatchPreviewViewController : UIViewController
{
WatchPreviewViewControllerStyleType style;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andStyle:(WatchPreviewViewControllerStyleType)_style;
#implementation
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andStyle:(WatchPreviewViewControllerStyleType)_style
{
self.style=_style;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if(self.style==WatchPreviewViewControllerStyleType1)
{
}
else if(self.style==WatchPreviewViewControllerStyleType2)
{
}
}
in this custom init set the ivar style sent while creating the controller. and then in viewDidload check for the style type and add the required views for that style.
and in WatchViewController
#implementation WatchViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *watch1 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
watch1.tag=123;
watch1.frame = CGRectMake(5.0, 10.0, 140.0, 170.0);
[watch1 setBackgroundImage:[UIImage imageNamed:#"Watch1.png"] forState: UIControlStateNormal];
[watch1 addTarget:self action:#selector(WatchesPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[scr addSubview:watch1];
UIButton *watch2 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
watch1.tag=456;
watch2.frame = CGRectMake(170.0, 10.0, 140.0, 170.0);
[watch2 setBackgroundImage:[UIImage imageNamed:#"watch2.png"] forState: UIControlStateNormal];
[watch2 addTarget:self action:#selector(WatchesPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[scr addSubview:watch2];
}
- (IBAction)WatchesPreviewButtonPressed:(id)sender {
if(sender.tag==123)
{
WatchPreviewViewController *watchesPreviewView = [[WatchPreviewViewController alloc] initWithNibName:#"WatchPreviewViewController" bundle:nil andStyle:WatchPreviewViewControllerStyleType1];
[self.navigationController pushViewController:watchesPreviewView animated:YES];
[watchesPreviewView release];
}
else
{
WatchPreviewViewController *watchesPreviewView = [[WatchPreviewViewController alloc] initWithNibName:#"WatchPreviewViewController" bundle:nil andStyle:WatchPreviewViewControllerStyleType2] ;
[self.navigationController pushViewController:watchesPreviewView animated:YES];
[watchesPreviewView release];
}
}

Make image appear after button push

I am trying to change the background color and make an image appear on the screen when a button is pushed. I can get the color to change, but I can't get a trace of the image to appear. I don't know how to place the image on the screen either. I am using iOS 5 and storyboard, but I have not added the UIImageView to the storyboard, as I want it to appear.
My ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
- (IBAction)changeButton:(id)sender;
#end
My ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)colorchange
{
self.view.backgroundColor = [UIColor greenColor];
}
-(void)imageShow
{
UIImage *image = [UIImage imageNamed:#"logo-tag-centered.pmg"];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[imgView setImage:image];
}
- (IBAction)changeButton:(id)sender
{
[self colorchange];
[self imageShow];
}
#end
Any help would be greatly appreciated! Thanks!
You have to add the image view to your view controller's view.
- (void)showImage
{
UIImage *image = [UIImage imageNamed:#"logo-tag-centered.png"];
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
imgView.center = CGPointMake(self.view.bounds.size.width / 2.0f, self.view.bounds.size.height / 2.0f);
[self.view addSubview:imgView];
}
You haven't added the image view anywhere to the main view. You could do this:
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:#"logo-tag-centered.pmg"];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[imgView setImage:image];
[imgView setHidden:YES];
[self.view addSubview:imgView];
}
-(void)imageShow
{
[imgView setHidden:NO];
}
- (IBAction)changeButton:(id)sender
{
[self colorchange];
[self imageShow];
}

create uibutton subclass

I tried to subclass UIButton to include an activity indicator, but when i use initWithFrame:(since i'm subclassing uibutton i'm not using buttonWithType:) the button doesn't display. Also how would i set the button type in this case?:
my view controller:
ActivityIndicatorButton *button = [[ActivityIndicatorButton alloc] initWithFrame:CGRectMake(10, 10, 300, 44)];
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Older Posts..." forState: UIControlStateNormal];
[cell addSubview:button];
[button release];
my activityindicatorbutton class:
#import <Foundation/Foundation.h>
#interface ActivityIndicatorButton : UIButton {
UIActivityIndicatorView *_activityView;
}
-(void)startAnimating;
-(void)stopAnimating;
#end
#implementation ActivityIndicatorButton
- (id)initWithFrame:(CGRect)frame {
if (self=[super initWithFrame:frame]) {
_activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_activityView.frame = CGRectOffset(_activityView.frame, 60.0f, 10.0f);
[self addSubview: _activityView];
}
return self;
}
-(void) dealloc{
[super dealloc];
[_activityView release];
_activityView = nil;
}
-(void)startAnimating {
[_activityView startAnimating];
}
-(void)stopAnimating {
[_activityView stopAnimating];
}
#end
Favour composition over inheritance.
Create a UIView which contains the components you need and add them to your view.
I ran into a similar situation, and agree with Jeff that you don't really need to subclass UIButton. I solved this by subclassing UIControl, and then overriding layoutSubviews to do all of the configuration of the views I wanted on my "button". It's a much more simple implementation that subclassing UIButton since there does seem to be some hidden mojo going on under the hood. My implementation looked like this:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.opaque = YES;
self.imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
[self addSubview:self.imageView];
self.textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self addSubview:self.textLabel];
}
return self;
}
And layoutSubviews looked like this:
- (void)layoutSubviews {
[super layoutSubviews];
// Get the size of the button
CGRect bounds = self.bounds;
// Configure the subviews of the "button"
...
}
I have created a custom class, preferring composition over inheritance and it works perfect. My custom class has a button and it knows it's MCContact object. Also it draws a proper button and calculates frames automatically using MCContact object, that is passed.
Header file sample:
#import <UIKit/UIKit.h>
#protocol MCContactViewDelegate;
#interface MCContactView : UIView
{
}
#property (nonatomic, strong) MCContact *mcContact;
#property (nonatomic, weak) id <MCContactViewDelegate> delegate;
- (id)initWithContact:(MCContact*)mcContact delegate:(id <MCContactViewDelegate>)delegate;
#end
#protocol MCContactViewDelegate <NSObject>
- (void)contactViewButtonClicked:(MCContactView*)contactView;
#end
Implementation file:
#import "MCContactView.h"
#interface MCContactView()
{
UIButton *_button;
}
#end
#implementation MCContactView
- (id)initWithContact:(MCContact*)mcContact delegate:(id <MCContactViewDelegate>)delegate
{
self = [super initWithFrame:CGRectZero];
if (self) {
GetTheme();
_mcContact = mcContact;
_delegate = delegate;
_button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *normalBackgroundImage = [[UIImage imageNamed:#"tokenNormal.png"] stretchableImageWithLeftCapWidth:12.5 topCapHeight:12.5];
[_button setBackgroundImage:normalBackgroundImage forState:UIControlStateNormal];
UIImage *highlightedBackgroundImage = [[UIImage imageNamed:#"tokenHighlighted.png"] stretchableImageWithLeftCapWidth:12.5 topCapHeight:12.5];
[_button setBackgroundImage:highlightedBackgroundImage forState:UIControlStateHighlighted];
_button.titleLabel.font = [theme contactButtonFont];
[_button setTitleColor:[theme contactButtonTextColor] forState:UIControlStateNormal];
[_button setTitleEdgeInsets:UIEdgeInsetsMake(4, 6, 4, 6)];
NSString *tokenString = ([allTrim(mcContact.name) length]>0) ? mcContact.name : mcContact.eMail;
[_button setTitle:tokenString forState:UIControlStateNormal];
[_button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
CGSize size = [tokenString sizeWithFont:[theme contactButtonFont]];
size.width += 20;
if (size.width > 200) {
size.width = 200;
}
size.height = normalBackgroundImage.size.height;
[_button setFrame:CGRectMake(0, 0, size.width, size.height)];
self.frame = _button.frame;
[self addSubview:_button];
}
return self;
}
- (void)buttonClicked:(id)sender
{
[self.delegate contactViewButtonClicked:self];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
You have a pretty obvious problem that concerns your dealloc method: [super dealloc]; must be called AT THE END of your implementation, or else the line after that will try to access a memory space (the ivar space) that has been already deallocated, so it's going to crash.
For the other problem, I'm not sure it's a good idea to put an activity monitor as the subview of a button in general...
You don’t really want to subclass UIButton. It’s a class cluster, so individual instances will be something like UIRoundRectButton or some other private Apple class. What are you trying to do that requires a subclass?

Iphone UIButton not working

This is so damn simple im sure! Im missing something and im exhausted from trying to fix it. hopefully someone can help.
The Button in CharacterView.m works but the button nested down in CharacterMale.m does not. I'm not using IB everything is done progmatically.
CharacterView.m is being used as a container
/////////////////////////////////////////////////////////////////////////////////
CharacterController.m
/////////////////////////////////////////////////////////////////////////////////
#import "CharacterController.h"
#import "CharacterView.h"
#implementation CharacterController
- (id)init {
NSLog(#"CharacterController init");
self = [ super init ];
if (self != nil) {
}
return self;
}
- (void)loadView {
[ super loadView ];
characterView = [ [ CharacterView alloc ] init];
self.view = characterView;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[characterView release];
[super dealloc];
}
#end
/////////////////////////////////////////////////////////////////////////////////
CharacterView.m
/////////////////////////////////////////////////////////////////////////////////
#import "CharacterView.h"
#import "CharacterMale.h"
#implementation CharacterView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
characterMale = [ [ CharacterMale alloc ] init];
[self addSubview: characterMale];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 200, 200, 100);
[button setImage:[UIImage imageNamed:#"btnCharSelect.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(ApplyImage:) forControlEvents:UIControlEventTouchUpInside];
[ self addSubview: button ];
}
return self;
}
- (void)drawRect:(CGRect)rect {
}
-(void)ApplyImage:(id)sender
{
NSLog(#"CharacterView button works");
}
- (void)dealloc {
[characterMale release];
[super dealloc];
}
#end
/////////////////////////////////////////////////////////////////////////////////
CharacterMale.m
/////////////////////////////////////////////////////////////////////////////////
#import "CharacterMale.h"
#import "CharacterController.h"
#implementation CharacterMale
- (id)init {
self = [ super init];
if (self != nil) {
UIImage *image = [UIImage imageNamed:#"charMale.png"];
imageView = [[ UIImageView alloc] initWithImage:image];
[image release];
[ self addSubview: imageView ];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200, 100);
[button setImage:[UIImage imageNamed:#"btnCharSelect.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(ApplyImage:) forControlEvents:UIControlEventTouchUpInside];
[ self addSubview: button ];
}
return self;
}
-(void)ApplyImage:(id)sender
{
NSLog(#"CharacterMal button works");
}
- (void)dealloc {
[super dealloc];
}
#end
FINALLY!!!!!!
I had to init all the views with initWithFrame and pass in valid frame rects. Init should be used with controllers and initWithFrame passing rects for UIViews!!!!
characterView = [ [ CharacterView alloc ] initWithFrame:CGRectMake(0, 0, 480, 320)];
then
characterMale = [ [ CharacterMale alloc ] initWithFrame:frame];
I'm new to this too, but have you tried: UIControlEventValueChanged instead of UIControlEventTouchUpInside ?
How big is the charMale.png image? What is the z-Order for the image and button in CharacterMale? The image may be sitting on top of the button that you are creating preventing it from being touched.
Hopefully this is the problem... I've not seen anything else yet...
Please check your button frame.
You have set (0,0,200,100);