开发者

String value cannot assigned to a string variable in iphone?

In my application, I take a UITextField value and trim it and assign to a string Variab开发者_运维知识库le declared in an Appdelegate. It assigns to a appdelegate variable and works well, sometimes It does not assign to the appdelegate variable.(This value is used in another view,so declared in appdelegate). Plz help...

NSString *txtTemp=[NSString stringWithFormat:@"%@",[txtName.text  stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
txtName.text=txtTemp;
[self appDelegate].customSearchQuery=[NSString stringWithFormat:@"%@",txtTemp];

NSLog(@"--appDelegate.customSearchQuery =%@",appDelegate.customSearchQuery);


This is most probably a memory management problem. NSString creates an autoreleased object. You will have to retain it if you want to use it outside the method you showed above. The easiest thing is to delcare as

@property (nonatomic, retain) NSString *customSearchQuery; 

in your Appdelegate.h. That should do the trick.

In the dealloc-method of the appdelegate, you'll need to release it - otherwise you leak the NSString; with the declaration above, you'll add

customSearchQuery = nil;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜