Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: HELP: with UIButton actions (new to objective c and iphone dev)



This question is not valid for this list as it's a framework specific question. Please take it somewhere else like the Cocoa mailing list.

That being said, your mistake is thinking the button passes its value to your action method. First there's no such thing as a "button's value", and what you're talking about is the button's title. Second, the object being passed to your, and any other action method, is the *sender* of the action, which is the button object itself in this case. To get the button's title you'll call the appropriate method on it (probably -title but I'm not that familiar with UIKit), and define your action method like this:
- (void) insertButtonValue:(id)sender;

Ofri

On 07/04/2008, at 15:10, Matthew Pearson wrote:
i am new to objective c and iphone dev and have been working through examples. I am trying to build a simple iphone Calculator to help me learn.

The situation:

- I have 10 UIbuttons with setTitles of 0 through to 9, these display correctly in the iphone simulator. 
- I have a UILabel which I want to display the input from the 10 buttons.

The problem:

How do I pass arguments ( ie the button number 0-9, or in future pi (3.142)  ) to a method using the addTarget: action: syntax

Button code as follows:

//button one
UIButton * alloc] initWithFrame:genericButtonFrame];

    [oneButton setTitle:@"1" forStates:UIControlStateNormal];
oneButton.font = [UIFont boldSystemFontOfSize:BUTTON_FONT_SIZE];
    oneButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    oneButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
   
[oneButton setBackgroundImage:genericImage forStates:UIControlStateNormal];
[oneButton setBackgroundImage:buttonBackgroundPressed forStates:UIControlStateHighlighted];

NSString *test = @"1";

[oneButton addTarget:self action:@selector(insertButtonValue:???????????) forControlEvents:UIControlEventTouchUpInside];  //hello: is sent when the button is touched
oneButton.center = CGPointMake( 45.0, 240.0);
oneButton.backgroundColor = [UIColor clearColor];
    [aContentView addSubview:oneButton];
    [oneButton release];

//button two etc etc etc


Method to be called:

- (void) insertButtonValue: (NSString *) valueSentFromButton
{
NSString *buttonValue = [NSString stringWithFormat:@"button sent %@!", valueSentFromButton];
self.label.text = buttonValue;
}

Obviously the numeric buttons will all have different values, and I don't want to call a separate method  for each one. I have tried loads of ways of specifying it and each time meet with various compiler errors. Starting to go MAD!!

With the code as is  ( obviously no ????? ) the method is receiving an object reference to the UIButton, can I set a values within the UIButton object??? All help greatly appreciated.

Thanks

Matt

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/objc-language/email@hidden

This email sent to email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/objc-language/email@hidden

This email sent to email@hidden

References: 
 >HELP: with UIButton actions (new to objective c and iphone dev) (From: Matthew Pearson <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.