简体   繁体   中英

Which View Controller is instantiating the Destination View Controller?

So here is what I'm trying to achieve.

I have a common MapViewController and two other view controllers that I want to send data from. ExploreViewController and SavedPlacesViewController .

I'm not using segues to do this. Instead I'm using something like this:

[mapButton addTarget:self action:@selector(showExploreMap:)
            forControlEvents:UIControlEventTouchUpInside];

and then declaring the showExploreMap method to push the MapViewController to the navigationController .

I've done this for ExploreViewController and it works perfectly but I can't figure out how I can do this for SavedPlacesViewController since the data is coming from a different model.

I think somehow I need to figure out which ViewController is calling MapViewController so I can figure out which data model to use.

Is there any way I can do this?

Synthesize an NSString in MapViewController.

MapViewController.h:-

NSString  *fromClass;
@property(nonatomic,retain)NSString *fromClass;

MapViewController.m:-

@synthesize fromClass;

ExploreViewController.m

-(IBAction)showExploreMap:(id)sender{
    MapViewController *obj=[[MapViewController alloc]initWithNibName:@"MapViewController Bundle:nil];
    obj.fromClass=[NSString stringWithString:@"ExploreViewController"];
    [self.navigationController pushViewController:obj animated:YES];
}

SavedPlacesViewController.m

MapViewController *obj=[[MapViewController alloc]initWithNibName:@"MapViewController Bundle:nil];
obj.fromClass=[NSString stringWithString:@"SavedPlacesViewController"];
[self.navigationController pushViewController:obj animated:YES];

MapViewController.m

if([fromClass isEqualToString @"ExploreViewController"])
{
    //use datasource for ExploreViewController
}
else
{
    //use datasource for SavedPlacesViewController
}

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