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

那年仲夏 提交于 2019-12-23 03:13:17

问题


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

  1. Name :UITextField
  2. Date of Birth :UITExtField
  3. Age: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


回答1:


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.




回答2:


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?



来源:https://stackoverflow.com/questions/2346951/iphone-how-i-can-access-data-in-a-view-controller-class-from-another-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!