How many ways to pass/share data b/w view controller

笑着哭i 提交于 2019-12-01 19:39:38

问题


im new to IOS and Objective-C and the whole MVC paradigm and i'm stuck with the following.

I am working on (replica) Contact app, also available in iphone as build in app. i want to pass data through another view controller and the data is pass (null) :(.

My Question is, How do I transfer the data from one view to another?


回答1:


As most the answers you got, passing data between one controller and another just means to assign a variable from one controller to the other one. If you have one controller to list your contacts and another one to show a contact details and the flow is starting from the list and going to detail after selecting a contact, you may assign the contact variable (may be an object from the array that is displayed in your list) and assign it to the detail view controller just before showing this one.

- (void)goToDetailViewControllerForContact:(Contact *)c
{
    ContactDetailViewController *detailVC = [[[ContactDetailViewController alloc] init] autorelease];
    detailVC.contact = c;
    [self.navigationController pushViewController:c animated:YES];
    //[self presentModalViewController:detailVC animated:YES]; //in case you don't have a navigation controller
}

On the other hand, if you want to insert a new contact from the detail controller to the list controller, I guess the best approach would be to assign the list controller as a delegate to the detail one, so when a contact is added the delegate is notified and act as expected (insert the contact to the array and reload the table view?).

@protocol ContactDelegate <NSObject>
- (void)contactWasCreated:(Contact *)c;
// - (void)contactWasDeleted:(Contact *)c; //may be useful too...
@end

@interface ContactListViewController : UIViewController <ContactDelegate>
@property (nonatomic, retain) NSArray *contacts;
...
@end

@implementation ContactListViewController
@synthesize contacts;
...

- (void)goToDetailViewControllerForContact:(Contact *)c
{
    ContactDetailViewController *detailVC = [[[ContactDetailViewController alloc] init] autorelease];
    detailVC.contact = c;
    detailVC.delegate = self;
    [self.navigationController pushViewController:c animated:YES];
    //[self presentModalViewController:detailVC animated:YES]; //in case you don't have a navigation controller
    }

- (void)contactWasCreated:(Contact *)c
{
    self.contacts = [self.contacts arrayByAddingObject:c]; //I'm not sure this is the correct method signature...
    [self reloadContacts]; //may be [self.tableView reloadData];

}

...
@end


@interface ContactDetailViewController : UIViewController

@property (nonatomic, assign) id<ContactDelegate> delegate;
...
@end


@implementation ContactDetailViewController
@synthesize delegate; //remember to don't release it on dealloc as it is an assigned property
...

- (void)createContactAction
{
    Contact *c = [[[Contact alloc] init] autorelease];
    [c configure];
    [self.delegate contactWasCreated:c];
}

...
@end



回答2:


Technically, you shouldn't!
The whole idea is not for "views" to control what happens to the data.

What you want to do is to pass data between controllers (which I imagine is exactly what you are planning to do anyway).

You can have shared model (an instance of an object that both view controllers would access) keeping the data you want to share,

You can use notifications to pass data (it is best suited for certain cases).

You can write something to disk and read it again later.

You can use NSUserDefaults.

You can use KeyChain.

...




回答3:


The best way is:

  • declare the appropriate @property in the second view controller
  • when you create it, simply set the property with

viewController.property = valueYouWantToPass;




回答4:


I'm a big fan of delegates and protocols.
And in some occasions use a Singleton pattern.




回答5:


two ways to pass/share data between view controller

create an object and sent the data like this

QGraduteYr *tableverify=[[QGraduteYr alloc]initWithStyle:UITableViewStyleGrouped];
    tableverify.mystring=myString
    [self.navigationController pushViewController:tableverify animated:YES];

another method is stor it in the delegates and use it via shared delegates

MedicalAppDelegate *appdelegate=(MedicalAppDelegate *)[[UIApplication sharedApplication]delegate];
    appdelegate.collnameStr=collStr;

and ust this appdelegates value whereever you need



来源:https://stackoverflow.com/questions/9016680/how-many-ways-to-pass-share-data-b-w-view-controller

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