为什么我的字符串不会显示在 UIAlertView 中

发布于 2024-12-11 06:40:01 字数 1250 浏览 0 评论 0原文

首先,我对此进行了彻底的研究,但找不到任何与我正在寻找的东西相匹配的东西。

我的问题是这样的:我在 Class1 中创建了一个字符串属性,然后合成如下所示。

@interface Class1 : UIViewController 
{
    NSString *testvar;
}

@property (nonatomic, retain) NSString *testvar;

@end



@implementation Class1

@synthesize testvar

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.testvar = @"Test variable";
}
@end

这对于定义变量/属性的值来说效果很好,因为它在由定义它的同一 viewDidLoad 方法显示的测试 UIAlertView 中工作。当另一个类, Class2,尝试检索和使用该属性。第二个类中使用的代码如下:

Class1 *foo = [[Class1 alloc]init];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Title"
                                               message:foo.testvar
                                              delegate:self
                                     cancelButtonTitle:@"OK"
                                     otherButtonTitles:nil];
[alert show];
[alert release];
[foo release];

发生的情况是 UIAlertView 显示,但没有消息。我相信 Class1 testvar 属性的值在 Class2 尝试检索它之前已被释放,或者我设置不正确。我这样说是因为我尝试了同样的事情,但使用了 int 而不是 NSString。我在Class1的viewDidLoad中将int的值设置为3,当显示UIAlertView时,它显示了int的值,但显示了“0”,即int的默认值,而不是比我设定的值。

由于对 Objective-C 相对较新,而且还没有很好地掌握内存管理,我认为该错误与我在何处或如何设置属性值有关。任何见解将不胜感激。提前致谢。

First, I have thoroughly researched this and not been able to find anything matching what I'm looking for.

My problem is this: I have a string property created in Class1 and then synthesized as shown below.

@interface Class1 : UIViewController 
{
    NSString *testvar;
}

@property (nonatomic, retain) NSString *testvar;

@end



@implementation Class1

@synthesize testvar

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.testvar = @"Test variable";
}
@end

This works fine for defining the variable/property's value as it works in a test UIAlertView displayed by the same viewDidLoad method it is defined in. The problem occurs when the other class, Class2, makes an attempt to retrieve and use the property. The code used in the second class is as follows:

Class1 *foo = [[Class1 alloc]init];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Title"
                                               message:foo.testvar
                                              delegate:self
                                     cancelButtonTitle:@"OK"
                                     otherButtonTitles:nil];
[alert show];
[alert release];
[foo release];

What happens is that the UIAlertView displays, but has no message. I believe that the value of the Class1 testvar property is being released before Class2 attempts to retrieve it, or I am setting it incorrectly. I say this because I tried the same thing, but used an int instead of NSString. I set the int's value to 3 in Class1's viewDidLoad and when the UIAlertView displayed, it showed the int's value, but displayed "0", the default value of the int, rather than my set value.

Being relatively new to Objective-C and not yet having an excellent grasp of memory management, I think the error has something to do with where or how I set the value of my property. Any insight would be appreciated. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

病女 2024-12-18 06:40:01

你这样做是错误的。 viewDidLoad 方法仅在绘制该视图时调用,而不是在另一个类中初始化它时调用。

在 Class1 -(id)init 中定义一个方法并移动 self.testvar = @"Test Variable";进入其中。然后它应该按预期工作。

- (id)init
{
   if(self = [super init])
   {
     self.testvar = @"Test Variable";
   }
   return self;
}

You are doing it incorrectly. viewDidLoad method is called only when that view is drawn not when you initialize it in another class.

Define a method in Class1 -(id)init and move your self.testvar = @"Test Variable"; into it. Then it should work as expected.

- (id)init
{
   if(self = [super init])
   {
     self.testvar = @"Test Variable";
   }
   return self;
}
揪着可爱 2024-12-18 06:40:01

您还没有为 testvar 分配任何内容。此时它仍然是一个 nil 对象。

无论如何,您需要

foo.textvar = @"my message";

根据上面的代码进行类似的操作。

顺便说一句,此时您不需要释放警报。您可以在返回警报引用的委托方法中释放它。

您可以粘贴完整的代码以便更好地理解吗?

You haven't assigned anything to testvar yet. It will still be a nil object at this point.

You need to go something like

foo.textvar = @"my message";

based on your code above anyway.

BTW you don't need to release the alert at that point. You release it in the delegate method that returns the alert reference.

Could you paste the full code for a better understanding?

梦与时光遇 2024-12-18 06:40:01

您可以为此使用全局变量。

在一个类中声明和定义它,您可以在项目内的任何其他类中使用它。

就像您可以在 ClassA 中声明一个字符串,然后可以在 ClassB 中使用它一样。

A类:

NSString *textVar = nil;

textVar = @"Your Message";

B类:

extern NSString *textVar;

UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Title
                                 message:textVar     
                                 delegate:self
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
                                 [alert show];
                                 [alert release];

You can user Global variable for this.

Declare and define it in one class and you can use it in any other class within the project.

Like you can declare a string in ClassA and you can user it in ClassB.

ClassA:

NSString *textVar = nil;

textVar = @"Your Message";

ClassB:

extern NSString *textVar;

UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Title
                                 message:textVar     
                                 delegate:self
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
                                 [alert show];
                                 [alert release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文