Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 762
Amir iDev Avatar asked Jan 26 '12 10:01

Amir iDev


People also ask

How do you pass data from one viewController to another using segue in Swift?

Pass Data using Seque. After outlet is connected to respective viewController, select Enter Last Name button and then CTRL + Drag to NextViewController(i.e, FirstViewController) for showing the viewController from LandingPageViewController. This lets you set what kind of segue you want to use.


1 Answers

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
like image 120
Ricard Pérez del Campo Avatar answered Oct 11 '22 23:10

Ricard Pérez del Campo