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 02-18-2011, 07:23 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 49
BenCrosthwaite is on a distinguished road
Default Send NSString as argument in UIButton

Hey Guys,

I have given my self a crash course in iphone dev over the last 2-3 weeks, so im still finding my feet with objective-c.

I have a small project where i read in data from an XML off the internet. One of the parameters is an <email></email> email address. What i would like to do is send the value of this <email></email> tag to the UIButton selector, so from there i can open the mail client on the phone and send an email.

Each UIButton is programatically in a for loop because there are going to be multiple instances of the <email></email>. I would like to send a NSString, the contents of the <email></email> tag, as an argument to the @selector. I am aware of comparing the title and im also aware of the tag but i dont think i can use these as the title of the buttons will always be "email" and the tag is an integer.

I would be grateful if someone could point me in the right direction.

Regards,
-Ben
BenCrosthwaite is offline   Reply With Quote
Old 02-18-2011, 07:31 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 10
ozier is on a distinguished road
Default

You can try use NSArray, that contains NSStrings with email texts, and tag buttons with indexes from array.

You can also try subclassing of UIButton adding email NSString property, then in action method you can easily obtain this.
__________________


Get the most Recent Photos of your friends!

Last edited by ozier; 02-18-2011 at 07:33 AM.
ozier is offline   Reply With Quote
Old 02-18-2011, 07:55 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 49
BenCrosthwaite is on a distinguished road
Default

Hey ozier,

Thanks for your reply. Im not quite sure how to subclass UIButton, but i can certainly implement the array approach. In your opinion which would be the best approach?

Regards,
-Ben
BenCrosthwaite is offline   Reply With Quote
Old 02-18-2011, 08:18 AM   #4 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 10
ozier is on a distinguished road
Default

Try subclassing:

Code:
@interface ButtonWithEmail : UIButton {
	NSString* emailContent;
}

@property (nonatomic, retain) NSString* emailContent;

@end
Your ButtonWithEmail now is normall UIButton controll with additional NSString property, where you can store your content. Don't forget to release it in dealloc.
__________________


Get the most Recent Photos of your friends!
ozier is offline   Reply With Quote
Old 02-18-2011, 08:41 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 49
BenCrosthwaite is on a distinguished road
Default

I think i must be misunderstanding subclassing somewhere along the line. I get an error every time i compile. Please let me explain what i am doing.

Button.h
Code:
@interface Button : UIButton {
	NSString* emailContent;
}
@property (nonatomic, retain) NSString* emailContent;
@end
Button.m
Code:
@implementation Button
@synthesize emailContent;

- (void)dealloc {
	[emailContent release];
    [super dealloc];
}
@end
i import "Button.h" in the main header

main Implementation
Code:
Button *emailBtn = [[Button buttonWithType:UIButtonTypeRoundedRect] retain];
emailBtn.frame = CGRectMake(2, 108, 60, 20);
emailBtn.backgroundColor = [UIColor clearColor];
emailBtn.emailContent = email;
[emailBtn setTitle:@"e-mail" forState:UIControlStateNormal];
[emailBtn addTarget:self action:@selector(emailButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[textView addSubview:emailBtn];
Button Method
Code:
-(IBAction)emailButtonPressed:(id)sender{
    NSLog(@"Phone Button Fire");
	Button *button = (Button*)sender;

	NSLog(@"email content: %@", button.emailContent);
}
I receive an SIGABRT error every time i compile.
BenCrosthwaite is offline   Reply With Quote
Old 02-18-2011, 09:44 AM   #6 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 49
BenCrosthwaite is on a distinguished road
Default

My problem is where i try and assign a NSString to Button class property emailContent. If i edit the line out the application runs.

Code:
Button *emailBtn = [[Button buttonWithType:UIButtonTypeRoundedRect] retain];
emailBtn.frame = CGRectMake(2, 93, 60, 20);
emailBtn.backgroundColor = [UIColor clearColor];
emailBtn.tag = i;
//emailBtn.emailContent = email;
[emailBtn setTitle:@"email" forState:UIControlStateNormal];
[emailBtn addTarget:self action:@selector(emailButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[textView emailBtn];
[emailBtn release];
Error Message
Code:
2011-02-18 15:35:14.255 ParseXML[3373:207] -[UIRoundedRectButton setEmailContent:]: unrecognized selector sent to instance 0x4e4b190
2011-02-18 15:35:14.257 ParseXML[3373:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIRoundedRectButton setEmailContent:]: unrecognized selector sent to instance 0x4e4b190'
I have read some threads on various forums and the general consensus is that UIButton can not be subclassed, is this true?

Regards,
-Ben
BenCrosthwaite is offline   Reply With Quote
Old 02-18-2011, 09:53 AM   #7 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 10
ozier is on a distinguished road
Default

The problem is you are creating button using factory method buttonWithType. In your subclass you don't override it and it is invoked on super: UIButton. UIButton buttonWithType returns new UIButton object but NOT your extended subclass object. All works when you are creating button by:
Code:
button *emailBtn = [[button alloc] init];						 
								 
emailBtn.frame = CGRectMake(2, 108, 60, 20);
	
emailBtn.emailContent = @"blabla";
[emailBtn setTitle:@"e-mail" forState:UIControlStateNormal];
[emailBtn addTarget:self action:@selector(emailButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:emailBtn];
[emailBtn release];
but you will lose RoundedRectStyle.

In my case in past it was ok, cause i had custom buttons with image in my project. Problem is that after init you can't set button style because this property is read-only in UIButton.

I think in this case easier way will be my first idea with array
__________________


Get the most Recent Photos of your friends!
ozier is offline   Reply With Quote
Old 02-18-2011, 10:05 AM   #8 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 49
BenCrosthwaite is on a distinguished road
Default

Ahhh ok that makes sense. However, i have started down this road so im going to try and follow it through

I have amended my code and the app does not crash, but there is no button being create (visibly at least). Please could you explain how i would go about creating the custom buttons so it get displayed on my app?

Regards,
-Ben
BenCrosthwaite is offline   Reply With Quote
Reply

Bookmarks

Tags
cocoa objective c iphone, iphone, objective - c, 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: 369
14 members and 355 guests
cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, mottdog, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

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