2011-03-31 73 views
0

有没有办法在多个功能之间进行定义?我可以将它定义为一个,但在{}之外不能使用。我需要能够获取来自代码创建的UITextField的文本。我使用的代码如下:保持功能之间的定义

- (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 ]; 
} 

这是定义myTextField将作为UIAlertView中内的文本框的功能。

但我希望能够将myTextField文本保存为我可以在以后的函数中访问的数据,例如我在下面的代码中需要它的位置。

- (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 ]; 
} 

柜面有帮助,我打电话给另一个函数的不同功能有:

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

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

那只是调用这两个功能的一部分,但我定义Alerttime有:

#define int *Alerttime; 
Alerttime = 0; 

有没有人有任何想法如何我可以从myTextField af功能?

回答

1

这就是属性。

在您的H文件,在界面,把:

UITextField *myTextField; 

下面,放:

@property (nonatomic, retain) UITextField *myTextField; 

在你的M档,在实现部分的开始,放:

@synthesize myTextField; 

然后,您可以以self.myTextField的形式访问它(或者,如果没有完全相同的本地变量,只需使用myTextField)班上的其他地方。

你应该为自己找一份关于Objective-C的良好介绍性文章。你真的潜入,但你缺少一些基本知识。你明显有能力,所以请花时间和彻底。

+0

哦,我不认为这会保留函数之间的代码定义对象的文本,我认为每个函数从它的默认开始。 – cowbear16 2011-04-01 10:46:01

+0

我忘了使用自己的线,谢谢。 – cowbear16 2011-04-01 21:12:58