【问题标题】:Keeping defined between functions在函数之间保持定义
【发布时间】:2011-03-31 21:43:06
【问题描述】:

无论如何要在多个函数之间定义一些东西吗?我可以将它定义为一个,但在 {} 之外它不能被使用。我需要能够从创建的 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 访问文本?

【问题讨论】:

    标签: ios xcode function textfield


    【解决方案1】:

    这就是属性的用途。

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

    UITextField *myTextField;
    

    下面,放:

    @property (nonatomic, retain) UITextField *myTextField;
    

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

    @synthesize myTextField;
    

    然后,您可以在类中的其他任何地方将其作为 self.myTextField (如果没有同名的局部变量,则直接作为 myTextField )访问。

    而且你应该为自己获得一个关于 Objective-C 的好的介绍性文本。你真的很投入,但你缺少一些基础知识。你显然有能力,所以慢慢来,彻底。

    【讨论】:

    • 哦,我不认为会在函数之间保留代码定义对象的文本,我认为每个函数都是从默认值开始的。
    猜你喜欢
    • 2023-01-20
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2016-01-29
    • 2018-05-09
    • 2019-07-03
    • 2021-08-29
    • 2020-12-15
    相关资源
    最近更新 更多