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:
uibutton subclass:
.h file
code from .m file
Problem is that i cant add a selector to my button!
i tried to do this in my main class:
or this in my subclass:
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:
of course if i add button this way and selector is ok, but button does not receive touches.
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 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!