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 07-08-2010, 03:45 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Cambridge, UK
Posts: 52
Werdan is on a distinguished road
Default Adding selector to button created outside of class

Hi everyone,

I am designing an application which will be using pop-Up windows extensively, so I have decided to write a piece of code to programmatically set it up.

The idea is that every pop-Up will be effectively the same – it will have message, asking if the user really wants to do something and two buttons – yes and no. Standard alert pop-Ups are of no use as I want to customize the appearance.

The only difference in the pop-Ups is what happens when button "Yes" is pressed. I therefore wish to be able to place the code of creation of pop_up window in the application delegate file and then, when running that code, return the instance of the Yes button. I then wish to be able to add a selector to it from whichever part of program to do the desired action.

However, once I add a selector from the class that calls for delegate to display the pop-Up I get the warning: unrecognized selector sent to instance....

Th obvious solution is to duplicate the pop-up creation code in every class that might want to use it, but I would like to make a more flexible design.

Basically, can I add a selector from the class other then the one which created the button?

here is what I have in my appDelegate:

UIButton *yesButton = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:trialView.frame];

Code:
-(UIButton *)showPopUp:(NSString *)messageText and:(UIView *)view and:(NSString *)imageName and:
(NSString *)noButtonName and :(NSString *)noButtonNameHighlighted and:
(NSString *)yesButtonName and :(NSString *)yesButtonNameHighlighted {

THERE IS ALSO SOME CODE HERE WHIC IS IRRELEVANT

UIButton *yesButton = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:trialView.frame];
	[yesButton setImage:[UIImage imageNamed:yesButtonName] forState:UIControlStateNormal];
	[yesButton setImage:[UIImage imageNamed:yesButtonNameHighlighted] forState:UIControlStateHighlighted];
	[yesButton addTarget:self action:@selector(hideSemiTransparentView:) forControlEvents:UIControlEventTouchUpInside];
	yesButton.center = CGPointMake(0, 0);
	[mainPopUpView addSubview:yesButton];
return yesButton;
}
This is the part of code, as the whole function is huge and the rest of the code is only displaying and works very well. The method hideSemiTransparentView: is declared in the appDelegate.

Now I want to be able to call this pop-up creation method and add a method to the action from another class like this:

Code:
einstein2AppDelegate *delegate = (einstein2AppDelegate *)[[UIApplication sharedApplication] delegate];
	UIButton *button = [delegate showPopUp:@"aaaaaaa!!" and:self.view and:@"Background.png" and:@"yes_noiPhone.png" and:
						@"yes_noiPhoneHighlighted.png" and:@"yes_noiPhone.png" and:@"yes_noiPhoneHighlighted.png"];

	
	[button addTarget:button action:@selector(reset2) forControlEvents:UIControlEventTouchUpInside];
Does not work when I press on the button Any ideas?

Last edited by Werdan; 07-08-2010 at 03:50 PM.
Werdan is offline   Reply With Quote
Old 07-08-2010, 04:43 PM   #2 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Cambridge, UK
Posts: 52
Werdan is on a distinguished road
Default

OK, sorted it out!

It is really easy. Basically, when you pass a selector its name refers to the method which must be contained within the class that has initialised a button.

The easiest way in my case (and I guess in every other is the following):

We have 2 classes. Delegate and Class1 (they can be any other, this is just to illustrate). In delegate we write the method which takes the view, draws the pop-Up window in it and adds two buttons. It might also draw some other things. The method returns the instance of yesButton.

Now, we call this method from class1 with appropriate view passed to it. We add a selector to the received button. Say, we want to perform method called "reset". We do add a selector "reset, but we now have to return to delegate class and write the following function:

-(void)reset {
Class1 *controller = [[Class1 alloc] init];
[controller reset];
}

That's it! It is a little bit of work, but let's face it – redirecting is easy.

The obvious thing is that your delegate has to know about Class1 (i.e. have appropriate connections to it). Otherwise it won't recognise the command
Werdan is offline   Reply With Quote
Old 07-08-2010, 06:32 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Cambridge, UK
Posts: 52
Werdan is on a distinguished road
Default

Quote:
Originally Posted by Werdan View Post
OK, sorted it out!

It is really easy. Basically, when you pass a selector its name refers to the method which must be contained within the class that has initialised a button.

The easiest way in my case (and I guess in every other is the following):

We have 2 classes. Delegate and Class1 (they can be any other, this is just to illustrate). In delegate we write the method which takes the view, draws the pop-Up window in it and adds two buttons. It might also draw some other things. The method returns the instance of yesButton.

Now, we call this method from class1 with appropriate view passed to it. We add a selector to the received button. Say, we want to perform method called "reset". We do add a selector "reset, but we now have to return to delegate class and write the following function:

-(void)reset {
Class1 *controller = [[Class1 alloc] init];
[controller reset];
}

That's it! It is a little bit of work, but let's face it – redirecting is easy.

The obvious thing is that your delegate has to know about Class1 (i.e. have appropriate connections to it). Otherwise it won't recognise the command
Sorry, I was stupid here. This is not the solution.

All you have to do is make sure that addTarget points to the class which has the desired method implemented. In my case, since I add the selector in Class 1 code I needed to put "self" instead of the button instance. Then no need to redirect
Werdan is offline   Reply With Quote
Reply

Bookmarks

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: 337
13 members and 324 guests
akphyo, cgokey, EXOPTENDAELAX, GHuebner, guusleijsten, Mirotion22, ohmniac, Paul Slocum, PavelSea, Pudding, SLIC, Sloshmonster, Sonuye857
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,653
Threads: 94,115
Posts: 402,888
Top Poster: BrianSlick (7,990)
Welcome to our newest member, ohmniac
Powered by vBadvanced CMPS v3.1.0

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