I am New to the iPhone Development. How can i carry a string value from view2 to view1 when using navigation bar.
I have no problem in carrying string values from view1 to view2 to....by using pushviewcontroller But when i come back to previous views using Back button of navigation bar, I cannot able to hold string values.
I already seen post related this "http://stackoverflow.com/questions/2903967/how-return-a开发者_Go百科-value-from-view2-to-view1-when-using-navigation-bar" and that is not worked for me or may be i did wrong.
I need your help in solving this issue.
Thanks in advance,
Nagarajan Govindarajan.
You could use a delegate. You could also have a property in view1 that view2 can access and store the string into. The delegate is the better way to do it.
Take a look at Apple's samples to see how they use delegates. The question you refer to is correct, so I think you just need to understand it well enough to be able to debug your implementation.
In Your project app delegate class declare and define a string(exp str1). Also alloc and initialize this string.
In your view 2 class inport Appdelegate class. Declare its object like:
TestAppDelegate *appDeleg;
In viewDidLoad of class 2 define:appDeleg = [[UIApplication sharedApplication] delegate];
now store appDeleg.str1 = your string in view2 which you want to store and use in view 1.
do same declaration in view 1 and use there strView1= appDeleg.str1;
The best way I recommend is to use a Data class. Declare variables in a Data class and use them throughout. This is how it works :
//DataClass.h
@interface DataClass : NSObject {
NSString *str;
}
@property(nonatomic,retain)NSString *str;
+(DataClass*)getInstance;
@end
//DataClass.m
@implementation DataClass
@synthesize str;
static DataClass *instance =nil;
+(DataClass *)getInstance
{
@synchronized(self)
{
if(instance==nil)
{
instance= [DataClass new];
}
}
return instance;
}
Now in your view controller you need to call this method as :
DataClass *obj=[DataClass getInstance];
obj.str= @"I am Global variable";
This variable will be accessible to every view controller. You just have to create an instance of Data class.
精彩评论