Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-17-2009, 01:03 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 4
pollyp is on a distinguished road
Question subclassed UIButton and compiler warnings

Hi,

I'm a objective-C newbie trying to wring the last few compiler warnings out of my code. I've got one case that's got me stumped, and the mighty google does not seem to be able to help me.

I've got a button class that's subclassed from UIButton. (I've since learned that's problematic, and I should have created a whole new class. But I'd like to avoid rewriting this piece if I can.) Anyway, inside my class I'm doing things like calling setTitle:forState:, which generates the following compiler warning:

warning: 'UIButton' may not respond to '+setTitle:forState:'

I've changed my code to invoke this message on self, self cast to UIButton *, and super -- but I always get this warning. The code runs fine. I'm also perplexed about why the compiler thinks setTitle:forState: is a class method; of course it is an instance method.

So, what am I missing here?

Thanks in advance,

Polly
pollyp is offline   Reply With Quote
Old 10-17-2009, 03:46 PM   #2 (permalink)
Beast Iphone Developor
 
justill45's Avatar
 
Join Date: Aug 2009
Location: Atlanta, Georgia
Age: 16
Posts: 1,302
justill45 is on a distinguished road
Default

Quote:
Originally Posted by pollyp View Post
Hi,

I'm a objective-C newbie trying to wring the last few compiler warnings out of my code. I've got one case that's got me stumped, and the mighty google does not seem to be able to help me.

I've got a button class that's subclassed from UIButton. (I've since learned that's problematic, and I should have created a whole new class. But I'd like to avoid rewriting this piece if I can.) Anyway, inside my class I'm doing things like calling setTitle:forState:, which generates the following compiler warning:

warning: 'UIButton' may not respond to '+setTitle:forState:'

I've changed my code to invoke this message on self, self cast to UIButton *, and super -- but I always get this warning. The code runs fine. I'm also perplexed about why the compiler thinks setTitle:forState: is a class method; of course it is an instance method.

So, what am I missing here?

Thanks in advance,

Polly
post the code please
justill45 is offline   Reply With Quote
Old 10-19-2009, 07:54 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 4
pollyp is on a distinguished road
Default

Sure thing. Here's my code:

Code:
#import "MySubclassedButton.h"

@implementation MySubclassedButton
+ (id)withFrame:(CGRect)frame withTitle:(NSString *)title
{
	if ( self = [super buttonWithType:UIButtonTypeRoundedRect]) {
		[ self setFrame:frame];
		[ self setTitleEdgeInsets: UIEdgeInsetsMake(0.0,10.0,0.0,10.0) ];
		[ self setTitle:title forState:UIControlStateNormal];
	}
    return self;
}
@end
The setFrame generates this compiler warning:
warning: 'MySubclassedButton' may not respond to '+setFrame:'

I get similar warnings for setTitleEdgeInsets and setTitle. If I cast to a UIButton object, I get this instead:
warning: 'UIButton' may not respond to '+setFrame:'

Here's the associated header file:

Code:
#import <Foundation/Foundation.h>

@interface MySubclassedButton : UIButton {
}

+ (id)withFrame:(CGRect)frame withTitle:(NSString *)title;
@end
I'm using version 3.1.4 of Xcode and I'm compiling for a 3.0 device target.

Thanks in advance for any light you can shed on this headscratcher.

Polly
pollyp is offline   Reply With Quote
Old 10-19-2009, 08:51 PM   #4 (permalink)
Beast Iphone Developor
 
justill45's Avatar
 
Join Date: Aug 2009
Location: Atlanta, Georgia
Age: 16
Posts: 1,302
justill45 is on a distinguished road
Default

Quote:
Originally Posted by pollyp View Post
Sure thing. Here's my code:

Code:
#import "MySubclassedButton.h"

@implementation MySubclassedButton
+ (id)withFrame:(CGRect)frame withTitle:(NSString *)title
{
	if ( self = [super buttonWithType:UIButtonTypeRoundedRect]) {
		[ self setFrame:frame];
		[ self setTitleEdgeInsets: UIEdgeInsetsMake(0.0,10.0,0.0,10.0) ];
		[ self setTitle:title forState:UIControlStateNormal];
	}
    return self;
}
@end
The setFrame generates this compiler warning:
warning: 'MySubclassedButton' may not respond to '+setFrame:'

I get similar warnings for setTitleEdgeInsets and setTitle. If I cast to a UIButton object, I get this instead:
warning: 'UIButton' may not respond to '+setFrame:'

Here's the associated header file:

Code:
#import <Foundation/Foundation.h>

@interface MySubclassedButton : UIButton {
}

+ (id)withFrame:(CGRect)frame withTitle:(NSString *)title;
@end
I'm using version 3.1.4 of Xcode and I'm compiling for a 3.0 device target.

Thanks in advance for any light you can shed on this headscratcher.

Polly
well, i just did a very basic test on changing it, i made and IBAction and i made and IBOUtlet named theButton. heres what i tried first and it worked fine by using th eoutlet i created.


Code:
-(IBAction)button:(id)sender {
	
	[theButton setTitle:@"Test" forState:UIControlStateNormal];
}
However, when i didnt use the Outlet and just did self i got the same exact error as you did , heres the code

Code:
-(IBAction)button:(id)sender {
	
	[self setTitle:@"hello" forState:UIControlStateNormal];
}
my advice, just make and out of the UIButton, adn connect it to the button, its pretty simple , very basic. Is there something you're trying to do where you cant use and IBOutlet???
justill45 is offline   Reply With Quote
Old 10-20-2009, 06:15 AM   #5 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 3
Toro Liu is on a distinguished road
Thumbs up

[quote=pollyp;135037]Sure thing. Here's my code:

Code:
#import "MySubclassedButton.h"

@implementation MySubclassedButton
+ (id)withFrame:(CGRect)frame withTitle:(NSString *)title
{
	if ( self = [super buttonWithType:UIButtonTypeRoundedRect]) {
		[ self setFrame:frame];
		[ self setTitleEdgeInsets: UIEdgeInsetsMake(0.0,10.0,0.0,10.0) ];
		[ self setTitle:title forState:UIControlStateNormal];
	}
    return self;
}
@end
Maybe the codes should be written as following:
Code:
+ (id)buttonWithFrame:(CGRect)frame withTitle:(NSString *)title
{
        UIButton *retBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
         [retBtn setFrame:frame];
         [retBtn ....];
         return retBtn;
}
Toro Liu is offline   Reply With Quote
Old 10-20-2009, 09:20 AM   #6 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

Within a class method ( +(id) with ... ) , any reference to self means the class object. So you are really calling +setFrame, +setTitle, etc.

Toro Liu way is appropriate. Just remember to autorelease the resulting object.
nobre84 is offline   Reply With Quote
Old 10-22-2009, 08:22 PM   #7 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 4
pollyp is on a distinguished road
Default

[Slaps self on forehead]

D'oh! You're completely correct. Thanks for pointing out the right way to do this.

Polly
pollyp is offline   Reply With Quote
Reply

Bookmarks

Tags
compiler warnings, subclassing uibutton, uibutton

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 331
8 members and 323 guests
givensur, glenn_sayers, guusleijsten, ipodphone, mtl_tech_guy, Punkjumper, whitey99, yys
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:36 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0