简体   繁体   中英

How do I pass object from one ViewController to another ViewController

I have a RootViewController that calls an AddQuoteViewController and there is a variable "subject_id" that I set in the RootViewController that does not show up in AddQuoteViewController and I need help understanding why and how to fix it.

    Subject *sub = [self.tableDataSource objectAtIndex:indexPath.row];

    self.subject_id = sub.subject_id;

    [self addQuote_Clicked: sub];

...

- (void) addQuote_Clicked:(id)sender {

    if(aqvController == nil)
        aqvController = [[AddQuoteViewController alloc] initWithNibName:@"AddQuoteView" bundle:nil];

    if(addNavigationController == nil)
        addNavigationController = [[UINavigationController alloc] initWithRootViewController:aqvController];

    [self.navigationController presentModalViewController:addNavigationController animated:YES];
}

Then in my AddQuoteViewController I try to access this variable like this:

RootViewController *rv = [RootViewController alloc] ;

NSLog(@"rv.Subject_id = %d", rv.subject_id);

But get nothing. There must be a simple way to do this.

First of all, there's an error in your third block of code. This:

RootViewController *rv = [RootViewController alloc] ;

Should be this:

RootViewController *rv = [[RootViewController alloc]init];

But strictly speaking that's not why you aren't seeing your instance variable.

If I understand correctly, the first two blocks of code are in RootViewController , and they instantiate an AddQuoteViewController and present it. Then, from your third block of code, which is in AddQuoteViewController , you want to access a member variable ( subject_id ) of the RootViewController that brought it up.

The approach of instantiating a RootViewController from within the AddQuoteViewController wouldn't work, because you're creating a different instance of RootViewController . What you're after is the value in the instance you just came from.

Perhaps the easiest way to do it is to create a corresponding property on AddQuoteViewController and set it when it's created:

- (void) addQuote_Clicked:(id)sender {

if(aqvController == nil)
    aqvController = [[AddQuoteViewController alloc] initWithNibName:@"AddQuoteView" bundle:nil];

if(addNavigationController == nil)
    addNavigationController = [[UINavigationController alloc] initWithRootViewController:aqvController];

aqvController.subject_id = self.subject_id;

[self.navigationController presentModalViewController:addNavigationController animated:YES];
}

You'll need to create the subject_id property on AddQuoteViewController the same way you did on RootViewController .

You can't access variables across view controllers like that. Have a look at creating a singleton class which will be globally accessible. One exception I think is you can access them in the AppDelegate, but it's not advisable to have global vars.

There are various ways of doing this, but a short answer - You can set a reference to the RootViewController as a property on your AddQuoteViewController

ie

in AddQuoteViewController.h

RootViewController *rvc

...

@property (nonatomic,assign) RootViewController *rvc;

and corresponding synthesize and release in your implementation class. (AddQuoteViewController.m)

Then when you create your AddQuoteViewController inside your RootViewController, also set this property:

- (void) addQuote_Clicked:(id)sender {

    if(aqvController == nil)
        aqvController = [[AddQuoteViewController alloc] initWithNibName:@"AddQuoteView" bundle:nil];
    aqvController.rvc = self;
... etc.

Then you can access any property of the root view controller inside your AddQuoteViewController via this property:

NSLog(@"rv.Subject_id = %d", self.rv.subject_id);

As a side note there are a few things you are doing in your question that are a bit unusual, like trying to get a reference to an object by allocating a new one, and also creating a new navigation controller and presenting it as a modal view controller typically you would do one or the other (and wouldn't need to create a new navigation controller). ie you would either create a view controller and present it modally, or you would create a view controller and push it onto your current navigation controller stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM