Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top