1

Im very new to objective c and iOS. This is my first app ever so bear with me.

Im just trying to make a page with some buttons that are dynmically created from a text file, that will open a page in webview.

I'm basing my code off this: how can i pass any kind of parameter in UiButton action?

MyButton.h

#import <Foundation/Foundation.h>

@interface MyButton : UIButton
{
    NSString *_linkURL;
}
@property (nonatomic, retain) NSString *linkURL;
@end

MyButton.m

#import "MyButton.h"

@implementation MyButton
@synthesize linkURL = _linkURL;
@end

So then in my ViewController.m file I'm importing MyButton and trying to use it to create buttons that have the URL as their own property.

MyButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button setLinkURL:URL]; //THIS IS THE LINE THATS GIVING ME THE unrecognized selector at instance error

Then I go on to get the URL in the buttonPressed method but I cant seem to get past this one error. I have no idea what Im doing wrong.

Community
  • 1
  • 1
Kierchon
  • 2,271
  • 1
  • 19
  • 24
  • 2
    Is this a compile error or a runtime error? And you want `MyButton *button = [MyButton buttonWithType:UIButtonTypeRoundedRect];` – rmaddy Dec 04 '13 at 17:24
  • Its a runtime error but that fixed it. Thanks I just didnt see that typo. Post as answer and ill accept if you want – Kierchon Dec 04 '13 at 17:27
  • 1
    First, get rid of the ivar declaration and the "@synthesize" line. No need to do either. If you create a property, linkURL, the compiler automatically creates the ivar _linkURL. Also, post the actual error, don't paraphrase. – rdelmar Dec 04 '13 at 17:27
  • You don't need the ivar anymore. You'll get the same result as before if you just have the property declaration. See [this answer](http://stackoverflow.com/questions/12632285/declaration-definition-of-variables-locations-in-objectivec/12632467#12632467) for more detail. – DrummerB Dec 04 '13 at 17:30

2 Answers2

5

The problem is this line:

MyButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

It should be:

MyButton *button = [MyButton buttonWithType:UIButtonTypeRoundedRect];

As you had it you were creating an instance of UIButton instead of MyButton.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
2

Your problem is with this line:

MyButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

You're casting a UIButton to a MyButton, but objects of type UIButton do not have the properties of a MyButton object. You need something like:

MyButton *button = [[MyButton alloc] initWithType:UIButtonTypeRoundedRect];
godel9
  • 7,340
  • 1
  • 33
  • 53
  • Yeah rmaddy mentioned that and it fixed it but it wasnt giving me a compile error or warning. That would have saved me about 30 minutes of my life – Kierchon Dec 04 '13 at 17:30
  • @Kierchon Right... The `buttonWithType` method returns type `id`, which can be cast to anything without a warning. – godel9 Dec 04 '13 at 17:31