保持功能之间的定义

时间:2011-03-31 21:43:07

标签: ios xcode function textfield

有没有在多个功能之间保留定义的东西?我可以将它定义为一个,但在{}之外它不能被使用。我需要能够获取以查看代码创建的文本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 访问文本?

1 个答案:

答案 0 :(得分:1)

这就是属性的用途。

在你的h文件中,在界面中输入:

UITextField *myTextField;

下面,放:

@property (nonatomic, retain) UITextField *myTextField;

在你的m文件中,在实现部分的开头,输入:

@synthesize myTextField;

然后你可以在类中的任何其他位置以self.myTextField(或者只是myTextField,如果没有相同名称的局部变量)访问它。

你有责任在Objective-C上获得一篇很好的介绍性文章。你真的潜水了,但是你错过了一些基础知识。你显然已经掌握了自己的能力,所以要花时间并且要彻底。