objective C: Buttons created from subclass of UIButton class not working - objective-c

I am creating a subclass of UIButton in order to create my own customized buttons. My code as follows:
//interface file (subclass of uIButton
#interface UICustomButton : UIButton
{
Answer *answer;
NSString *btnType;
}
#property (nonatomic, retain) Answer *answer;
#property (nonatomic, assign) NSString *btnType;
- (id)initWithAnswer:(Answer *)ans andButtonType:(NSString *)type andFrame:(CGRect)frame;
- (void)buttonPressed;
#end
//Implementation file (.m)
#implementation UICustomButton
#synthesize answer,btnType;
- (id)initWithAnswer:(Answer *)ans andButtonType:(NSString *)type andFrame:(CGRect)frame;
{
self = [super initWithFrame:frame];
if (self)
{
self = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
self.backgroundColor = [UIColor colorWithHexString:#"#E2E4E7"];
}
[self addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlStateNormal];
self.answer = ans;
self.btnType = type;
return self;
}
I am facing some issues in getting the above code to work. I have 2 problems
1) The buttons are not responding to the selector method "buttonPressed"
2) I am hitting a runtime error for the lines 'self.answer = ans' and 'self.btnType = type' Stack trace as follows:
-[UIButton setAnswer:]: unrecognized selector sent to instance 0x614ebc0
2011-06-23 00:55:27.038 onethingaday[97355:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setAnswer:]: unrecognized selector sent to instance 0x614ebc0'
What am I doing wrong here?

This is happening because you are creating a UIButton type object and not a UICustomButton type inside the init method when you do
self = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
Try replacing your init method for
- (id)initWithAnswer:(Answer *)ans andButtonType:(NSString *)type andFrame:(CGRect)frame;
{
self = [self initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
if (self)
{
self.backgroundColor = [UIColor colorWithHexString:#"#E2E4E7"];
[self addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
self.answer = ans;
self.btnType = type;
}
return self;
}
This will cause self to be a UICustomButton type object.
Also, you are using a wrong type for the UIControlState parameter when you add the target to your button using the addTarget:action:forControlEvents: method
You should use value among the ones bellow:
UIControlEventTouchDown
UIControlEventTouchDownRepeat
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
UIControlEventTouchCancel
EDIT:
Notes on UIButton subclassing
Many references on the web say you should NOT subclass the UIButton class, but not only anybody said why but what also deeply annoyed me was that the UIButton Class Reference does not say anything about it at all.
If you take UIWebView Class Reference for example, it explicitly states that you should not subclass UIWebView
Subclassing Notes The UIWebView class
should not be subclassed.
the big deal with UIButton is that it inherits from UIControl and a good and simple explanation is on the UIControl Class Reference itself
Subclassing Notes You may want to
extend a UIControl subclass for either
of two reasons:
To observe or modify the dispatch of
action messages to targets for
particular events
To provide custom
tracking behavior (for example, to
change the highlight appearance)
So, this means that you CAN subclass a UIButton, but you should be careful on what you are doing. Just subclass it to change its behavior and not its appearance. To modify a UIButton appearance you should use the interface methods provided for that, such as:
setTitle:forState:
setBackgroundImage:forState:
setImage:forState:
References worth reading
The UIView Programming Guide: View and Window Architecture -> Tips for Using Views Effectively -> Do Not Customize Controls by Embedding Subviews
Source: my post here

Not sure this was in the docs before, but anyway these are the current notes on + (id)buttonWithType:(UIButtonType)buttonType...
To me it looks like subclassing is OK as long as you use init instead of buttonWithType. I have yet to try it myself however.
Discussion This method is a convenience constructor for creating
button objects with specific configurations. It you subclass UIButton,
this method does not return an instance of your subclass. If you want
to create an instance of a specific subclass, you must alloc/init the
button directly.
When creating a custom button—that is a button with the type
UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0)
initially. Before adding the button to your interface, you should
update the frame to a more appropriate value.

If you want to get notifications when the user is interacting with your buttons, just sublcass UIButton and implement these methods:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesBegan");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesEnded");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesCancelled");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesMoved");
}
No init method required.

Edit
This answer reaches back several years, and things have changed - as Apple docs now explicitly mention subclassing and gives some hints.
So the following answer might be irrelevant or wrong for current development and might be ignored if you're interested in the current state of the art.
UIButton is not meant to be subclassed.
You are better off making a category and defining a factory method that delivers your needed button (with proper call to buttonWithType:). initWithFrame: is not the correct way to initialize a button anyway.

//
// BtnClass.m
#import "BtnClass.h"
#implementation BtnClass
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
//added custum properities to button
-(id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(#"initWithCoder");
self = [super initWithCoder: aDecoder];
if (self) {
// Initialization code
_numberOfItems=[[UILabel alloc]initWithFrame:CGRectMake(40, 8, 160, 30)];
_numberOfItems.textAlignment=NSTextAlignmentLeft;
_numberOfItems.font = [UIFont boldSystemFontOfSize:18.0];
_numberOfItems.textColor = [UIColor darkGrayColor];
[self addSubview:_numberOfItems];
_leftImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 25, 25)];
[self addSubview:_leftImage];
_rightImage=[[UIImageView alloc]initWithFrame:CGRectMake(280, 10, 15, 15)];
[self addSubview:_rightImage];
[self setImage:[UIImage imageNamed:#"list-bg2-1.png"] forState:UIControlStateNormal];
[_rightImage setImage:[UIImage imageNamed:#"carat.png"]];
self.backgroundColor=[UIColor blackColor];
if(self.tag==1)
{
[_leftImage setImage:[UIImage imageNamed:#"notes-icon.png"]];
}
if(self.tag==2)
{
[_leftImage setImage:[UIImage imageNamed:#"photos-icon.png"]];
}
if(self.tag==3)
{
[_leftImage setImage:[UIImage imageNamed:#"videos-icon.png"]];
}
}
return self;
}
//selected method of uibutton
-(void)setSelected:(BOOL)selected
{
[super setSelected:selected];
if(selected)
{
[self setImage:nil forState:UIControlStateNormal];
_numberOfItems.textColor = [UIColor whiteColor];
[_rightImage setImage:[UIImage imageNamed:#"carat-open.png"]];
if(self.tag==1)
{
[_leftImage setImage:[UIImage imageNamed:#"white-notes-icon.png"]];
}
else if(self.tag==2)
{
[_leftImage setImage:[UIImage imageNamed:#"white-photo-icon.png"]];
}
else
{
[_leftImage setImage:[UIImage imageNamed:#"white-video-icon.png"]];
}
}
else{
_numberOfItems.textColor = [UIColor darkGrayColor];
if(self.tag==1)
{
[_leftImage setImage:[UIImage imageNamed:#"notes-icon.png"]];
}
if(self.tag==2)
{
[_leftImage setImage:[UIImage imageNamed:#"photos-icon.png"]];
}
if(self.tag==3)
{
[_leftImage setImage:[UIImage imageNamed:#"videos-icon.png"]];
}
[self setImage:[UIImage imageNamed:#"list-bg2-1.png"] forState:UIControlStateNormal];
[_rightImage setImage:[UIImage imageNamed:#"carat.png"]];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end

Related

Issue overriding the default constructor of the UIButton Class?

When an object is placed on a View Controller, it is initialised. I want to know if it's possible for you to place a UIButton on a Storyboard, with a custom class that is a subclass of the UIButton class, and have it automatically initialise with custom properties and run some custom methods - as defined in the custom class - instead of having to type out the method that modifies the properties within the View Controller's class.
Effectively, I'm trying to save space within my View Controller's class by having the custom button automatically construct itself as any ordinary UIButton does, but with custom properties and methods that are also run.
I'm unsure how to approach solving my problem, I essentially want to have the setupButton method run within an overridden constructor.
#implementation TransitionButton
#synthesize borderRadius, borderWidth, borderColor, highlightColor;
- (void)setupButton
{
borderRadius = 5;
borderWidth = 2;
borderColor = [TransitionButton customRedColor];
highlightColor = [UIColor redColor];
[self addBorder];
[self setupEventEffects];
}
- (void)setupEventEffects
{
[self addTarget:self action:#selector(highlight) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:#selector(unhighlight) forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:#selector(unhighlight) forControlEvents:UIControlEventTouchDragExit];
}
- (void)addBorder
{
self.layer.borderColor = borderColor.CGColor;
self.layer.cornerRadius = borderRadius;
self.layer.borderWidth = borderWidth;
}
- (void)highlight
{
self.layer.borderColor = highlightColor.CGColor;
self.titleLabel.textColor = highlightColor;
}
- (void)unhighlight
{
self.layer.borderColor = borderColor.CGColor;
self.titleLabel.textColor = borderColor;
}
Storyboards do not initialize their objects with init. They initialize with initWithCoder:, so that's what you need to implement if you want customization from a Storyboard.
Something along these lines:
- (instancetype) initWithCoder: (NSCoder *)coder {
self = [super initWithCoder: coder];
[self setupButton];
return self;
}

UIView Doesn't Appear and drawRect Method Never Called

I've made a UIView subclass called MiniView.
I try adding it to my viewController as follows:
#interface SomeViewController ()
#property (strong, nonatomic) MiniView *miniView;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
self.miniView = [[MiniView alloc] initWithFrame:CGRectMake(20.f, 20.f, 200.f, 200.f)];
_miniView.backgroundColor = [UIColor blackColor];
[self.view addSubview:_miniView];
}
The MiniView class looks like this:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
NSLog(#"DRAW RECT");
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor * redColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
CGContextSetFillColorWithColor(context, redColor.CGColor);
CGContextFillRect(context, self.bounds);
}
But drawRect never gets called, unless I explicity call it, from within the setNeedsLayout method. It also doesn't draw anything. I've tried just adding a UIImageView in the drawRect method, and this appears fine. But the above code produces nothing.
I also get the error:
: CGContextSetFillColorWithColor: invalid context 0x0. This is
a serious error. This application, or a library it uses, is using an
invalid context and is thereby contributing to an overall degradation
of system stability and reliability. This notice is a courtesy: please
fix this problem. It will become a fatal error in an upcoming update.
If I print a log statement in the drawRect method and output the 'rect' values, it's correct at 200 x 200, so I don't know why the context is 0x0.
So one problem is that drawRect is never called, and the other problem is that if I call it explicitly, nothing is displayed...
You probably need to call setNeedsDisplay on your custom UIView subclass:
- (void)viewDidLoad {
[super viewDidLoad];
self.miniView = [[MiniView alloc] initWithFrame:CGRectMake(20.f, 20.f, 200.f, 200.f)];
_miniView.backgroundColor = [UIColor blackColor];
[_miniView setNeedsDisplay]; // Added this
[self.view addSubview:_miniView];
}
This is basically a prod to tell the system your UIView needs to be redrawn. See the docs for more info.
Posting as a separate answer as requested:
The actual problem was that I had redefined the setNeedsDisplay method in the MiniView class, like so:
- (void)setNeedsDisplay
{
NSLog(#"Redrawing info: %#", [_info description]);
}
Because I had neglected to make a call to [super setNeedsDisplay], drawRect was never being called and nothing was drawing. So this fixed it:
- (void)setNeedsDisplay
{
[super setNeedsDisplay];
NSLog(#"Redrawing info: %#", [_info description]);
}
Never call drawRect explicity, try this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.miniView = [[MiniView alloc] initWithFrame:CGRectMake(20.f, 20.f, 200.f, 200.f)];
_miniView.backgroundColor = [UIColor blackColor];
[self.view addSubview:_miniView];
[_miniView setNeedsDisplay];
}
Well in your drawrect method include this line:-
[super drawRect:rect];

Reading touch events in a QLPreviewController

I've got a QuickLook view that I view some of my app's documents in. It works fine, but I'm having my share of trouble closing the view again. How do I create a touch event / gesture recognizer for which I can detect when the user wants to close the view?
I tried the following, but no events seem to trigger when I test it.
/------------------------ [ TouchPreviewController.h ]---------------------------
#import <Quicklook/Quicklook.h>
#interface TouchPreviewController : QLPreviewController
#end
//------------------------ [ TouchPreviewController.m ]---------------------------
#import "TouchPreviewController.h"
#implementation TouchPreviewController
- (id)init:(CGRect)aRect {
if (self = [super init]) {
// We set it here directly for convenience
// As by default for a UIImageView it is set to NO
UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleDoubleTap:)];
singleFingerDTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:singleFingerDTap];
[self.view setUserInteractionEnabled:YES];
[self.view setMultipleTouchEnabled:YES];
//[singleFingerDTap release];
}
return self;
}
- (IBAction)handleSingleDoubleTap:(UIGestureRecognizer *) sender {
CGPoint tapPoint = [sender locationInView:sender.view.superview];
[UIView beginAnimations:nil context:NULL];
sender.view.center = tapPoint;
[UIView commitAnimations];
NSLog(#"TouchPreviewController tap!" ) ;
}
// I also tried adding this
- (BOOL)gestureRecognizer:(UIGestureRecognizer *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*) otherGestureRecognizer {
return YES;
}
#end
Edit: For clarification, this is how I instantiate the controller:
documents = [[NSArray alloc] initWithObjects: filename , nil ] ;
preview = [[TouchPreviewController alloc] init];
preview.dataSource = self;
preview.delegate = self;
//set the frame from the parent view
CGFloat w= backgroundViewHolder.frame.size.width;
CGFloat h= backgroundViewHolder.frame.size.height;
preview.view.frame = CGRectMake(0, 0,w, h);
//refresh the preview controller
[preview reloadData];
[[preview view] setNeedsLayout];
[[preview view] setNeedsDisplay];
[preview refreshCurrentPreviewItem];
//add it
[quickLookView addSubview:preview.view];
Also, I've defined the callback methods as this:
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
return [documents count];
}
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index
{
return [NSURL fileURLWithPath:[documents objectAtIndex:index]];
}
Edit2: One thing i noticed. If I try making swiping gestures, I get the following message. This could shed some light on what is wrong/missing?
Ignoring call to [UIPanGestureRecognizer setTranslation:inView:] since
gesture recognizer is not active.
I think your example code is incomplete. It isn't clear how you are instantiating the TouchPreviewController (storyboard, nib file or loadView.)
I have never used the class so I could be way out in left field.
If you've already instantiated a UITapGestureRecognizer in the parent viewController, it is absorbing the tap events and they aren't passed on to your TouchPreviewController.
I would implement the view hierarchy differently by attaching the UITapGestureRecognizer to the parent viewController and handle presentation and unloading of the QLPreviewController there.
I think you might not have to subclass QLPreviewController by instantiating the viewController from a nib file.
When your parent viewController's UITapGestureRecognizer got an event you would either push the QLPreviewController on the navigation stack or pop it off the navigation stack when done.
Hope this is of some help.

How do I get NSTextFinder to show up

I have a mac cocoa app with a webview that contains some text. I would like to search through that text using the default find bar provided by NSTextFinder. As easy as this may seem reading through the NSTextFinder class reference, I cannot get the find bar to show up. What am I missing?
As a sidenote:
- Yes, I tried setting findBarContainer to a different view, same thing. I reverted back to the scroll view to eliminate complexity in debugging
- performTextFinderAction is called to perform the find operation
**App Delegate:**
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.textFinderController = [[NSTextFinder alloc] init];
self.webView = [[STEWebView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 200)];
[[self.window contentView] addSubview:self.webView];
[self.textFinderController setClient:self.webView];
[self.textFinderController setFindBarContainer:self.webView.enclosingScrollView];
[[self.webView mainFrame] loadHTMLString:#"sample string" baseURL:NULL];
}
- (IBAction)performTextFinderAction:(id)sender {
[self.textFinderController performAction:[sender tag]];
}
**STEWebView**
#interface STEWebView : WebView <NSTextFinderClient>
#end
#implementation STEWebView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
}
- (NSUInteger) stringLength {
return [[self stringByEvaluatingJavaScriptFromString:#"document.documentElement.textContent"] length];
}
- (NSString *)string {
return [self stringByEvaluatingJavaScriptFromString:#"document.documentElement.textContent"];
}
In my tests, WebView.enclosingScrollView was null.
// [self.textFinderController setFindBarContainer:self.webView.enclosingScrollView];
NSLog(#"%#", self.webView.enclosingScrollView);
Using the following category on NSView, it is possible to find the nested subview that extends NSScrollView, and set that as the container, allowing the NSTextFinder to display beautifully within a WebView
#interface NSView (ScrollView)
- (NSScrollView *) scrollView;
#end
#implementation NSView (ScrollView)
- (NSScrollView *) scrollView {
if ([self isKindOfClass:[NSScrollView class]]) {
return (NSScrollView *)self;
}
if ([self.subviews count] == 0) {
return nil;
}
for (NSView *subview in self.subviews) {
NSView *scrollView = [subview scrollView];
if (scrollView != nil) {
return (NSScrollView *)scrollView;
}
}
return nil;
}
#end
And in your applicationDidFinishLaunching:aNotification:
[self.textFinderController setFindBarContainer:[self scrollView]];
To get the Find Bar to appear (as opposed to the default Find Panel), you simply have to use the setUsesFindBar: method.
In your case, you'll want to do (in your applicationDidFinishLaunching:aNotification method):
[textFinderController setUsesFindBar:YES];
//Optionally, incremental searching is a nice feature
[textFinderController setIncrementalSearchingEnabled:YES];
Finally got this to show up.
First set your NSTextFinder instances' client to a class implementing the <NSTextFinderClient> protocol:
self.textFinder.client = self.textFinderController;
Next, make sure your NSTextFinder has a findBarContainer set to the webView category described by Michael Robinson, or get the scrollview within the webView yourself:
self.textFinder.findBarContainer = [self.webView scrollView];
Set the find bar position above the content (or wherever you wish):
[self.webView scrollView].findBarPosition = NSScrollViewFindBarPositionAboveContent;
Finally, tell it to show up:
[self.textFinder performAction:NSTextFinderActionShowFindInterface];
It should show up in your webView:
Also, not sure if it makes a difference, but I have the NSTextFinder in the XIB, with a referencing outlet:
#property (strong) IBOutlet NSTextFinder *textFinder;
You may also be able to get it by simply initing it like normal: self.textFinder = [[NSTextFinder alloc] init];

calling object's properties using super (or how to access super view button in a sub view)

I have main view with a button sub view
StartScene.h:
#class PlayScene
#interface StartScene : UIView <UITextFieldDelegate> {
PlayScene* tView;
UIButton *Ok;
}
somewhere in StartScene.m:
Ok = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[Ok setFrame:CGRectMake(100, 190, 65, 30)];
[Ok setTitle:(NSString *)#"OK" forState:(UIControlState)UIControlStateNormal];
[Ok addTarget:self action:#selector(gameOverDidLoad) forControlEvents:UIControlEventTouchDown];
[self addSubview:Ok];
Ok.hidden=YES;
Ok.enabled=NO;
tView = [[PlayScene alloc]initWithFrame:CGRectMake(0, 0, 320, 520)];
tView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"backGround.jpg"]];
[self addSubview:tView];
and sub view:
PlayScene.h:
#import "StartScene.h"
#interface PlayScene : StartScene {
}
PlayScene.m:
- (void)ooneFingerTwoTaps {
[NSThread detachNewThreadSelector:#selector(oneFingerTwoTaps) toTarget:self withObject:nil];
}
is called and method for it is
- (void)oneFingerTwoTaps
{
// some code
if (self.GameState==GameLost) {
[super checkScore:xScore];
Ok.hidden=NO;
Ok.enabled=YES;
[[NSThread currentThread] cancel];
}
}
}
so need Ok button to appear when something happens in PlayScene.
I've tried
Ok.hidden=NO;
Ok.enabled=YES;
But it doesn't work. I guess, due to the fact that child view can see only an UIButton variable, that is not yet initialized.
So I got 2 questions here:
1) I can call Ok variable using self.Ok or super.Ok . I thought that super.Ok would totally work but I don't know and I can't find out the right syntax here. What is it?
2) May be the problem can be solved in some other way?