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

时间:2021-12-18 13:02:06

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.

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

@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:

它可以很好地定义变量/属性的值,就像它在测试UIAlertView中一样,它由定义在其中的viewDidLoad方法显示。当另一个类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];

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.

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

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.

由于Objective-C相对较新,而且还没有很好的内存管理知识,我认为这个错误与我在哪里或如何设置属性值有关。任何见解都将受到赞赏。提前谢谢。

3 个解决方案

#1


6  

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

你做得不对。只有当在另一个类中初始化该视图时,viewDidLoad方法才被调用。

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

在Class1 -(id)init中定义一个方法并移动你自己。testvar = @“测试变量”;进去。然后它应该像预期的那样工作。

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

#2


0  

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

您还没有为testvar分配任何东西。它仍然是一个nil对象。

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?

你能粘贴完整的代码以更好地理解吗?

#3


0  

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中声明一个字符串并且你可以在ClassB中使用它一样。

ClassA:

ClassA:

NSString *textVar = nil;

textVar = @"Your Message";

ClassB:

ClassB:

extern NSString *textVar;

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

#1


6  

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

你做得不对。只有当在另一个类中初始化该视图时,viewDidLoad方法才被调用。

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

在Class1 -(id)init中定义一个方法并移动你自己。testvar = @“测试变量”;进去。然后它应该像预期的那样工作。

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

#2


0  

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

您还没有为testvar分配任何东西。它仍然是一个nil对象。

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?

你能粘贴完整的代码以更好地理解吗?

#3


0  

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中声明一个字符串并且你可以在ClassB中使用它一样。

ClassA:

ClassA:

NSString *textVar = nil;

textVar = @"Your Message";

ClassB:

ClassB:

extern NSString *textVar;

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