acces view from a wrapper class - iphone

In Unity's callback method I've added a UIButton
int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight, int* openglesVersion) {
CGRect rect = [[UIScreen mainScreen] bounds];
// Create a full-screen window
_window = [[UIWindow alloc] initWithFrame:rect];
EAGLView* view = [[EAGLView alloc] initWithFrame:rect];
if (wrapperObj == nil)
{
wrapperObj = [[WrapperClass alloc] init];
}
UIImage *buttonImage = [UIImage imageNamed:#"imageButton.png"];
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(view.bounds.origin.x+35, view.bounds.size.height-72, 36, 39)];
[backButton setImage:buttonImage forState:UIControlStateNormal];
[backButton addTarget:wrapperObj action:#selector(goToPreviousScreen:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:backButton];
}
How do I acces the view of my class (MyViewController) where Unity3D has been instantiated? I'd like change the view
#implementation MyViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
QCARUnityPlayer::getInstance().QCARInit([orientation UTF8String]);
[self startUnity];
}
return self;
}
#end

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];
}
}

TapGestureRecognizer does not get called

Hi in the main WiewController in the viewDidLoad i set up a
UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(processTap)];
and then i a for loop i create UIViews and add them to a scroll view, that is then added to the main view.
UIView *newsContainer = [[UIView alloc] initWithFrame:CGRectMake(160 * countnews, 30, 156, 126)];
newsContainer.tag = countnews;
newsContainer.userInteractionEnabled = YES;
[newsContainer addGestureRecognizer:recognizer];
[tempScroll addSubview:newsContainer];
then i have a function
- (void)processTap:(UITapGestureRecognizer *)recognizer {
UIView *view = recognizer.view;
NSLog(#"HELLO, %d", view.tag);
}
Which never gets called, any suggestions? your help would be greatly appreciated. Thanks in advance.
here is the entire .m
#import "iPadMainViewController.h"
#import "GradeintView.h"
#import <QuartzCore/CALayer.h>
#import "Category.h"
#import "News.h"
#import "LazyImageView.h"
#import "TouchView.h"
#interface iPadMainViewController ()
#end
#implementation iPadMainViewController
#synthesize detailsView = _detailsView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(processTap:)];
[recognizer setDelegate:self];
GradeintView *MainTitle = [[GradeintView alloc] initWithFrame:CGRectMake(0, 0, 1024, 50)];
GradeintView *MainSubTitle = [[GradeintView alloc] initWithFrame:CGRectMake(0, 50, 1024, 30)];
NSMutableArray *categoriesCollection = [[Category alloc] allCategoriesFromFeedAtUrl:#"http://geonews.ge/xml/category.php"];
UIScrollView *categories = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 512, 768)];
_detailsView = [[UIWebView alloc] initWithFrame:CGRectMake(500, 0, 512, 768)];
[_detailsView addGestureRecognizer:recognizer];
[categories setScrollEnabled:TRUE];
[categories setContentSize:CGSizeMake(500, categoriesCollection.count * 156)];
MainTitle.layer.masksToBounds = NO;
MainTitle.layer.shadowOffset = CGSizeMake(3, 3);
MainTitle.layer.shadowRadius = 5;
MainTitle.layer.shadowOpacity = 0.3;
[categories setBackgroundColor:[UIColor redColor]];
int counter = 0;
for (Category *cat in categoriesCollection)
{
UIView *categoryTitle = [[UIView alloc] initWithFrame:CGRectMake(0, 166 * counter
, 500, 20)];
UILabel *categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 20)];
[categoryLabel setBackgroundColor:[UIColor clearColor]];
NSMutableArray *allCurrentNews = [[News alloc] allNewsFromCategory:cat.CategoryId];
categoryLabel.text = cat.Name;
categoryLabel.textColor = [UIColor whiteColor];
[categoryTitle addSubview:categoryLabel];
UIColor *myblack = [[UIColor alloc] initWithRed:0.14 green:0.14 blue:0.14 alpha:1];
UIColor *ligheterBlac = [[UIColor alloc] initWithRed:0.227 green:0.22 blue:0.22 alpha:1];
[categoryTitle setBackgroundColor:myblack];
UIScrollView *tempScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 166 * counter, 500, 166)];
UIColor *tempcolor = ligheterBlac;
tempScroll.layer.borderColor = [UIColor colorWithRed:0.34 green:0.34 blue:0.34 alpha:1].CGColor;
tempScroll.layer.borderWidth = 0.5f;
int countnews = 0;
for (News *news in allCurrentNews)
{
UIView *newsContainer = [[UIView alloc] initWithFrame:CGRectMake(160 * countnews, 30, 156, 126)];
newsContainer.tag = countnews;
[newsContainer addGestureRecognizer:recognizer];
//newsContainer.NewsId = news.NewsId;
LazyImageView *image = [[LazyImageView alloc] initWithURl:[NSURL URLWithString:news.Thumbnail]];
image.frame = CGRectMake(0, 0 , 156, 96);
UILabel *newsTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 96, 156, 30)];
newsTitle.backgroundColor = myblack;
newsTitle.numberOfLines = 2;
newsTitle.font = [UIFont systemFontOfSize:11];
newsTitle.text = news.Title;
newsTitle.textColor = [UIColor whiteColor];
newsTitle.textAlignment = UITextAlignmentCenter;
newsContainer.layer.borderColor = [UIColor colorWithRed:0.34 green:0.34 blue:0.34 alpha:1].CGColor;
newsContainer.layer.borderWidth = 0.5f;
[newsContainer addSubview:image];
[newsContainer addSubview:newsTitle];
countnews ++;
[tempScroll setContentSize:CGSizeMake(allCurrentNews.count * 156, 96)];
[tempScroll addSubview:newsContainer];
//[image release];
}
[tempScroll setBackgroundColor: tempcolor];
[categories addSubview:tempScroll];
[categories addSubview:categoryTitle];
[tempcolor release];
[tempScroll release];
counter ++;
}
self.detailsView.layer.masksToBounds = NO;
self.detailsView.layer.shadowOffset = CGSizeMake(-10, 5);
self.detailsView.layer.shadowRadius = 5;
self.detailsView.layer.shadowOpacity = 0.3;
[self.detailsView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.amazon.com"]]];
[self.view addSubview:categories];
[self.view addSubview:self.detailsView];
[self.view addSubview:MainSubTitle];
[self.view addSubview:MainTitle];
}
- (void)processTap:(UITapGestureRecognizer *)recognizer {
UIView *view = recognizer.view;
NSLog(#"HELLO, %d", view.tag);
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)dealloc {
[super dealloc];
}
- (void)loadDetailedContent:(NSString *)s
{
}
#end
Change
initWithTarget:self.view
to
initWithTarget:self
EDIT:
You also have forgotten colon:
initWithTarget:self action:#selector(processTap:)
EDIT2:
You have created _detailsView (with assigned UITapGestureRecognizer) but have not added it to any subview. How it will work?
I think the problem is that the scrollView, which contains your views, has its own internal gesture recognizer that is "taking away" touch events from your tap gesture recognizer. Try implementing the following gesture recognizer delegate method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Have you checked the Interaction setting?
Interaction shall be set to "User Interaction Enabled" in the Attributes inspector for the image.
Change #selector(processTap) to #selector(processTap:)
Because now you calling method that doesn't exist.
try this code
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(processTap)];
[newsContainer addGestureRecognizer::gestureRecognizer];
gestureRecognizer.cancelsTouchesInView = NO;
I assume the problem is, you might have missed to add <UIGestureRecognizerDelegate> in the header file.
You're adding a UIGestureRecognizer to a UIWebview, which is not recommended. UIWebview has its ownUIGestureRecognizer`s which are tricky to over-ride. See this SO question for more info.
you can first add your gestureRecognizer to self.view and check your method called or not... as well check this thread
Issue with a UITapGestureRecognizer

How To Combine 2 UIView into 1 View Controller?

I have a ViewController with 3 views: Rootview that shows toolbar with UISegmentedControl, tableView, and calendarView.
I have XIB for the rootView, and tableView, but the calendarView doesn't have a XIB.
I need to somehow combine the code to load the calendar view to fit with with this ViewController. Before, I was using the calendarView as its own viewController.
The code for calendarView:
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
calendar = [[TKCalendarMonthView alloc] init];
calendar.delegate = self;
calendar.dataSource = self;
}
return self;
}
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
UIBarButtonItem *actionButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(dismissCalendarView)];
self.navigationItem.leftBarButtonItem = actionButton;
[actionButton release];
int statusBarHeight = 0;
CGRect applicationFrame = (CGRect)[[UIScreen mainScreen] applicationFrame];
self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, statusBarHeight, applicationFrame.size.width, 300.0)] autorelease];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.backgroundColor = [UIColor clearColor];
self.title = #"Select Workout";
calendar.frame = CGRectMake(0, 0, calendar.frame.size.width, calendar.frame.size.height);
NSLog(#"%f height", applicationFrame.size.height);
[self.view addSubview:calendar];
[calendar reload];
}
If I put that code directly into this new viewController, it does not respect the UISegmentedControl and just shows up at launch.
Here is the code for the UISegmentedConrol:
- (void)segmentedControl:(SVSegmentedControl*)segmentedControl didSelectIndex:(NSUInteger)index
{
switch (index)
{
case 0:
{
[self.view addSubview: tableView1];
tableView1.hidden = NO;
calendar.hidden = YES;
[calendar removeFromSuperview];
break;
}
case 1:
{
[self.view addSubview: calendar];
tableView1.hidden = YES;
calendar.hidden = NO;
[tableView1 removeFromSuperview];
break;
}
}
}
Does using two different init-Methods fit your needs? initWithNibName:#"nib1" or likewise initWithNibName:#"nib2"??
Otherwise you will need to specify what you want to achieve a little more

Iphone UIButton not working in nested UIViews

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.