开发者

iPhone: How I can Access Data in a View Controller Class from Another Controller Class?

In my application I have one Entry form in which there are six Text fields

  1. Name :UITextField
  2. Date of Birth :UITExtField
  3. Ag开发者_C百科e:UITextField
  4. Address :UITextField
  5. Phone No :UITextField
  6. Image : UIIMageView

Now What I want is that I want all these fields value in the same fields of other ViewController class. How it could be possible .

Please help me I really need help for this..

Special Thanks in Advance


You can achieve that by implementing getter and setters in the delegate class.

In delegate .h file

Include UIApplication delegate

 @interface DevAppDelegate : NSObject <UIApplicationDelegate>

 NSString * currentTitle;

- (void) setCurrentTitle:(NSString *) currentTitle;
- (NSString *) getCurrentTitle; 

In Delegate implementation class .m

-(void) setCurrentLink:(NSString *) storydata{
currentLink = storydata;

}

-(NSString *) getCurrentLink{
if ( currentLink == nil ) {
currentLink = @"Display StoryLink";
}
return currentLink;
}

So the variable you to assess is set in the currentlink string by setters method and class where you want the string ,just use the getter method.


What you really need is a data model object.

A data model is an object of dedicated class that stores and logically manipulates the applications data. It should be an entirely separate class from either the views or the view controllers. It should universally accessible within the app preferably as a singleton.

(Warrior's solution is a lightweight solution that turns the app delegate into the data model object. It will work for small, quick and dirty apps. It will breakdown as the data grows more complex.)

In your case, controller A would write the collected data to the data model and then it would close its view. Controller B would upon activating, check the data model and read out the information it needed.

The beauty of using the data model is that it has the flexibility of an old style global variable combined with the safety of using a class dedicated to to maintaining the data's integrity.

For more details see: Pattern for Ownership and References Between Multiple Controllers and Semi-Shared Objects?

and

Simple MVC setup / design?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜