gpt4 book ai didi

ios - 保持函数之间的定义

转载 作者:行者123 更新时间:2023-11-29 05:05:28 28 4
gpt4 key购买 nike

有没有办法在多个函数之间保留一些定义?我可以将其定义在其中,但在 {} 之外无法使用它。我需要能够获取以查看创建 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 访问文本吗?

最佳答案

这就是属性的用途。

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

UITextField *myTextField;

下面,输入:

@property (nonatomic, retain) UITextField *myTextField;

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

@synthesize myTextField;

然后,您可以在类中的任何其他位置将其作为 self.myTextField(或者如果没有相同名称的局部变量,则简称为 myTextField)进行访问。

您应该获得一本有关 Objective-C 的优秀介绍性文本。你真的很投入,但你缺少一些基础知识。显然你已经具备了这方面的能力,所以慢慢来,彻底一点。

关于ios - 保持函数之间的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5506689/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com