Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Send NSString as argument in UIButton

BenCrosthwaiteBenCrosthwaite Posts: 49Registered Users
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 address. What i would like to do is send the value of this 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 . I would like to send a NSString, the contents of the 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
Post edited by BenCrosthwaite on

Replies

  • ozierozier Posts: 10Registered Users
    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.
  • BenCrosthwaiteBenCrosthwaite Posts: 49Registered Users
    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
  • ozierozier Posts: 10Registered Users
    Try subclassing:

    @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.
  • BenCrosthwaiteBenCrosthwaite Posts: 49Registered Users
    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

    @interface Button : UIButton {
    NSString* emailContent;
    }
    @property (nonatomic, retain) NSString* emailContent;
    @end

    Button.m

    @implementation Button
    @synthesize emailContent;

    - (void)dealloc {
    [emailContent release];
    [super dealloc];
    }
    @end


    i import "Button.h" in the main header

    main Implementation

    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

    -(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.
  • BenCrosthwaiteBenCrosthwaite Posts: 49Registered Users
    My problem is where i try and assign a NSString to Button class property emailContent. If i edit the line out the application runs.


    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

    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
  • ozierozier Posts: 10Registered Users
    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:
    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 ;)
  • BenCrosthwaiteBenCrosthwaite Posts: 49Registered Users
    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
Sign In or Register to comment.