i have NSString value in addViewController and i w

2020-02-07 13:34发布

I am newbie to programming. Any help could be greatly appreciated.

It's in AddViewController.

NSLog(@"Am Currently at %@",locatedAt);

DetailsOfPeopleViewController *details=[DetailsOfPeopleViewController alloc];
   details.testAddressStr=[NSString stringWithFormat:@"%@",locatedAt];

details.testAddressStr is displaying the value here, but it's not passing this value to DetailsViewController.

The value of locatedAt is displaying in the AddViewController. When I try to display this value in UILabel of DetailsViewController class it's not displaying that value in the label.

In DetailsViewController.h:

NSString *testAddressStr;

@property(nonatomic,strong) NSString *testAddressStr;
@property(nonatomic,retain)IBOutlet UILabel *addressLabel;

In DetailsViewController.m:

 [addressLabel setText:testAddressStr];

Am also trying it this way:

addressLabel.text=[NSString stringWithFormat:@"%@",testAddressStr];

I'm not really getting where I'm making a mistake...

5条回答
Explosion°爆炸
2楼-- · 2020-02-07 13:48

retain your string locatedAt, where you have initialized it. like this

[locatedAt retain];

Enjoy Programming

查看更多
地球回转人心会变
3楼-- · 2020-02-07 13:53

There's no such thing as the "scope of an object" in Objective-C. Scope rules have nothing to do with an object's lifetime — the retain count is everything.

You usually need to claim ownership of your instance variables. See the Objective-C memory management rules. With a retain property, your property setter claims ownership of the new value and relinquishes ownership of the old one. With an assign property, the surrounding code has to do this, which is just as mess in terms of responsibilities and separation of concerns. The reason you would use an assign property is in a case where you can't retain the value (such as non-object types like BOOL or NSRect) or when retaining it would cause unwanted side effects.

Incidentally, in the case of an NSString, the correct kind of property is usually copy. That way it can't change out from under you if somebody passes in an NSMutableString (which is valid — it is a kind of NSString).

查看更多
何必那么认真
4楼-- · 2020-02-07 14:00

try to replace

 [addressLabel setText:testAddressStr];

with the following

 [addressLabel setText:self.testAddressStr];
查看更多
走好不送
5楼-- · 2020-02-07 14:01

The issue is due to you are assigning an autoreleased variable. It's released before you display it on the detailView. So you need to retain it. Or You need to allocate the testAddressStr.

Just replace this line:

details.testAddressStr=[NSString stringWithFormat:@"%@",locatedAt];

with:

details.testAddressStr = [[NSString stringWithFormat:@"%@",locatedAt] retain];

or

details.testAddressStr = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@",locatedAt]];
查看更多
贪生不怕死
6楼-- · 2020-02-07 14:04

You can send data using NSUserDefault

SetValue

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSString stringWithFormat:@"%@",txtView.text]; forKey:@"testPersonNotesStr"];
[defaults synchronize];

GetValue

testPersonNotesStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"testPersonNotesStr"];
NSLog(@"PERSON NOTE is........ %@",testPersonNotesStr);  

:)

查看更多
登录 后发表回答