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?

有帮助吗?

解决方案

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

其他提示

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.

...

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;

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top