SISYN
SISYN

Reputation: 2269

How do I add a method to a button I made programmatically?

I'm working with a pre-existing xcode project for an iPhone app that retrieves flickr pictures. I want to add a favorite button so that a user can add a photo to an array of favorites, but I'm stumped because I've never made UI objects programmatically before.

Here is the code for my button, but how would I add a method do it? Also, where would this method definition go?

// Create favorites button
UIButton *favButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
favButton.frame = CGRectMake(200, 50, 100, 50);
[favButton setTitle:@"Favorite" forState:UIControlStateNormal];

favButton.backgroundColor = [UIColor clearColor];
[favButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:@"blueButton.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[favButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:@"whiteButton.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[favButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[favButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:favButton];

Is the addTarget method the way to add a method? Because whenever I click the button, it sends me back to xcode and shows me the main.m file and highlights this line of code:

int retVal = UIApplicationMain(argc, argv, nil, nil);

Sorry, I'm totally new to this. The project has the following files: JSONFlickrAppDelegate.h JSONFlickrAppDelege.m JSONFlickrViewController.h JSONFlickrViewController.m ZoomedImageView.h ZoomedImageView.m

Here is the xcode project that I am working off of: http://compsci.cis.uncw.edu/~pattersone/courses/275/resources/JSONFlickrPart3.zip

Upvotes: 2

Views: 1541

Answers (2)

iDev
iDev

Reputation: 23278

Looks like there are some confusions with how you need to add a target to a method.

Is the addTarget method the way to add a method?

Yes, you are correct. addTarget method is the way to add a method associated with the button and the method definition should be written on the class which is mentioned as addTarget param(In this case self, which is an object of current class).

When you use this line,

[favButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];

It means that you are adding a method named playAction: to button which should be executed in target self on the event of UIControlEventTouchUpInside i.e, when an event occurs as user taps inside the button. So the action playAction should be inside the class which is represented by the object self i.e, the class in which you have written the above [favButton addTarget:... line. You can add any number of targets to this same button using similar addTarget lines.

but how would I add a method do it? Also, where would this method definition go?

In this scenario, you need to define the method playAction: in the same class since you added target as self. This can be changed to any object of any class and the method can be written in that class.

for eg:-

- (void)playAction:(id)button {
  //write the required code here, button is same as the favbutton which is written above.
}

Because whenever I click the button, it sends me back to xcode and shows me the main.m file and highlights this line of code:

The reason why it is going back to xcode and shows the main.m is because it is a crashing since you haven't provided any playAction: method in your class. Since it cannot find any such method in the target self, it will crash and sometimes it just point to the main.m class.

You can also add target as follows,

[favButton addTarget:self action:@selector(playAction) forControlEvents:UIControlEventTouchUpInside];//notice that it is 'playAction' and not 'playAction:' with a colon at the end.

In this case your method will look like,

- (void)playAction {
  //write the required code here
}

These are just different ways to do this.

For more details, please check this apple documentation.

Upvotes: 2

esreli
esreli

Reputation: 5071

Yes, this is the method that adds the method:

[favButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];

The method, however, that will be called is playAction:. Whatever is located after addTarget is simply the object that will call the method, in the case of self, it is the object where this button is being created that it rests inside.

Upvotes: 4

Related Questions