Take the 2-minute tour ×

Is there anyway to keep something defined between multiple functions? I can have it defined in one, but outside of the {} it can't be used. I need to be able to fetch to see what the text from a code created UITextField is. The code I am using is below:

- (void)AlertShow32 {

UIAlertView * alert = [[ [ UIAlertView alloc ] initWithTitle:@"The Most Annoying Button!" message:@"What's your name?" 
                                                    delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil ]autorelease];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];;
//  [myTextField release];
[alert show ];

That is the function that defines myTextField as a text box inside a UIAlertView.

But I want to be able to save the myTextField text as data that I can access in a later function, such as where I need it in the code below.

- (void)AlertShow33 {

UIAlertView * alert = [[[[ UIAlertView alloc ] initWithTitle:@"The Most Annoying Button!" message:@"Nice to meet you %i, you have a wierd name!", myTextField.text ]
                                                    delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil ]autorelease];
alert.transform = CGAffineTransformTranslate( alert.transform, 0.0, 0.0 );
[alert show ];

Incase it helps, I called the different functions in another function with:

    if (Alerttime == 31) {
        [self performSelector:@selector(AlertShow31) withObject:self afterDelay:0.01];

    if (Alerttime == 32) {
        [self performSelector:@selector(AlertShow32) withObject:self afterDelay:0.01];

That is only the part that calls those two functions, but I defined Alerttime with:

#define int *Alerttime;
Alerttime = 0;

Does anyone have any idea on how I can access the text from myTextField after the function?

Sorry if this is messy.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

That's what properties are for.

In your h file, in the interface, put:

UITextField *myTextField;

Below, put:

@property (nonatomic, retain) UITextField *myTextField;

In your m file, at the start of the implementation section, put:

@synthesize myTextField;

Then you can access it as self.myTextField (or simply myTextField if there's no identically named local variable) anywhere else in the class.

And you owe it to yourself to get a good introductory text on Objective-C. You're really diving in, but you're missing some basics. You've clearly got the aptitude, so take your time and be thorough.

share|improve this answer
Oh, I didn't think that would keep the text of a code defined object between functions, I thought each function started from it's default. –  cowbear16 Apr 1 '11 at 10:46
I forgot about using the self line, thanks. –  cowbear16 Apr 1 '11 at 21:12
add comment

Your Answer


