Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pwturben

macrumors newbie
Original poster
Jan 27, 2010
11
0
Hello.

I have some problems with subclassing UIButton.
UIButton does not receive touches( i mean TouchesBegan and TouchesMoved methods), and i decided to subclass all my uibuttons,but i have a problem:

code from main class:
Code:
#import "bottombarButtonClass.h"
@interface bottombar : UIView {

	bottombarButtonClass * MyPageButton;
}
@property(nonatomic,retain) bottombarButtonClass * MyPageButton;

Code:
@synthesize MyPageButton;
Code:
MyPageButton = [[bottombarButtonClass alloc] initWithFrame:CGRectMake(10, 10, 60, 60)];
		
		[self addSubview:MyPageButton];
		
		NSLog(@"%@",MyPageButton);
 /*shows <bottombarButtonClass: 0x4032730; baseClass = UIButton; frame = (10 10; 60 60); opaque = NO; layer = <CALayer: 0x40330d0>*/

uibutton subclass:
.h file
Code:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>


@interface bottombarButtonClass : UIButton {

}
-(id)initWithFrame:(CGRect)frame;
@end

code from .m file
Code:
-(id)initWithFrame:(CGRect)frame
{
	
	self = [super initWithFrame:frame];
	return self;
}



Problem is that i cant add a selector to my button!
i tried to do this in my main class:
Code:
NSLog(@"%@",MyPageButton);
 /*shows <bottombarButtonClass: 0x4032730; baseClass = UIButton; frame = (10 10; 60 60); opaque = NO; layer = <CALayer: 0x40330d0>*/

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

or this in my subclass:
Code:
[self addTarget:self.nextResponder action:@selector(MyMethod:) forControlEvents:UIControlEventTouchUpInside];
        NSLog(@"nextresp = %@", self.nextResponder); 
/*shows "null" :(*/


But it does not work.
Why it doesnt work second time - i can understand, coz self.nextResponder is null(but why it isnt null in TouchesBegan method?), but why it doesnt work at first time - dont.



This methods works ok:
Code:
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
	
	[self.nextResponder touchesBegan:touches withEvent:event];
	
}

-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
	
	[self touchesBegan:touches withEvent:event];

}




of course if i add button this way and selector is ok, but button does not receive touches.
Code:
	MyMessagesButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
		[MyMessagesButton initWithFrame:CGRectMake(90, 10, 60, 60)];
		[self addSubview:MyMessagesButton];
	
	
		[MyMessagesButton addTarget:self action:@selector(MyMethod:) forControlEvents:UIControlEventTouchUpInside];



Question:how i can send message from my subclassed button into main class? I should define a target when sending message addTarget:action:forControlEvents to my button.

or how i can handle touches with TouchBegan without subclassing ? i need to handle this because i need the coordinates of a touch.

Thanks!
 
i added
Code:
[self setBackgroundImage:[UIImage imageNamed:@"mypage - 002.png"] forState:UIControlStateNormal];
	[self setBackgroundImage:[UIImage imageNamed:@"001 - Start. 00.png"] forState:UIControlStateHighlighted];
	[self setBackgroundImage:[UIImage imageNamed:@"000 - Start. 00.png"] forState:UIControlStateSelected];

filenames dont matter, i have 3 images with this names.

when im pressing button nothing change. But it should
My button doesnt receive TouchUpInside (and other) events
 
Could you maybe make a view, place it on top of the button, make the alpha so it's transparent and then subclass that view? The only problem is you would have to manually write code to change the alpha and the background so it would highlight when you pressed it. That's pretty darn hacky though.
 
thanks.
i think it will work.

my target is :make draggable buttons like in iPhone Springboard interface
how apple did that? its interesting:D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.