从两个独立的视图控制器到相同的详细视图控制器

时间:2014-07-24 15:09:00

标签: ios objective-c segue

这是我的第一个iPhone应用程序,也是我第一次接触目标c和Xcode。老实说,我不知道如何在一行(标题)中提出这个问题,所以我道歉。我有一个标签视图应用程序,其中一个标签是一个包含项目列表的表格视图,另一个标签是带有图钉的地图视图。这些引脚用于表视图中的相同项目。当选择表视图中的一个单元格时,将segue转换为带有标签,图像等的详细视图。根据选择的单元格确定将哪些信息传递给详细视图。我希望地图引脚以相同的方式工作......当选择引脚公开按钮然后转到详细视图并将相应的信息传递给该视图。

我不知道如何在地图视图控制器的表视图控制器中加入相同的方法和功能。我应该为地图视图制作一个全新的细节视图控制器吗?我当然会有2个具有相同布局的详细视图控制器。这样做最合适的方法是什么?我有一种感觉,有人会说使用表格视图的方法和属性作为代表,但我不知道从哪里开始这个。寻找一些方向。

ListTableViewController.h

#import <UIKit/UIKit.h>

@interface ListTableViewController : UITableViewController

@property (nonatomic, strong) NSArray *Parks;

@end

ListTableViewController.m

    #import "ListTableViewController.h"
    #import "TableCell.h"
    #import "ListDetailViewController.h"

    @interface ListTableViewController ()

    @end

    @implementation ListTableViewController

    - (id)initWithStyle:(UITableViewStyle)style
    {
        self = [super initWithStyle:style];
        if (self) {
        // Custom initialization
    }
    return self;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        // Uncomment the following line to preserve selection between presentations.
        // self.clearsSelectionOnViewWillAppear = NO;

        // Uncomment the following line to display an Edit button in the navigation bar     for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    //Parks declared in ListTableViewController.h
    _Parks = @[@"BOMBAY", @"BOONE'S CAVE", @"EAST DAVIDSON", @"HAMBY CREEK",
               @"LAKE THOM-A-LEX", @"LINWOOD", @"OPTIMIST", @"SOUTHMONT"];
    }

    - (void)didReceiveMemoryWarning
   {
       [super didReceiveMemoryWarning];
       // Dispose of any resources that can be recreated.
   }

   #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        // Return the number of sections.
        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:  (NSInteger)section
    {
    // Return the number of rows in the section.
        return _Parks.count;
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"TableCell";
        TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        // Configure the cell...

        //int row = [indexPath row]; out of the blue I received warning 'implicit conversion loses integer precision:
        //'NSInteger' (aka 'long') to 'int' - solution on SO was to use NSInteger
        NSInteger row = [indexPath row];

    cell.ParksLabel.text = _Parks[row];
    // Configure the cell...

    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
        ListDetailViewController *detailviewcontroller = [segue destinationViewController];

        NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];

        //int row = [myindexPath row]; out of the blue I received warning 'implicit conversion loses integer precision:
        //'NSInteger' (aka 'long') to 'int' - solution on SO was to use NSInteger
        NSInteger row = [myIndexPath row];

        detailviewcontroller.DetailModal = @[_Parks[row]];
    }

}
@end

MapViewController.h

    #import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapViewController : UIViewController {

    MKMapView *mapview;
}

@property (nonatomic, retain) IBOutlet MKMapView *mapview;


-(IBAction)SetMap:(id)sender;

@end

MapViewController.m - 我没有包含所有针脚的代码 - 只是一个给你一个想法

    @interface MapViewController ()

@end

@implementation MapViewController

@synthesize mapview;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //set initial extent
    MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
    region.center.latitude = 35.8207;
    region.center.longitude = -80.2563;
    region.span.longitudeDelta = 0.475f; //sets zoom extent
    region.span.latitudeDelta = 0.475f;
    [mapview setRegion:region animated:YES];

    CLLocationCoordinate2D bombayPin;
    bombayPin.latitude = 35.6300;
    bombayPin.longitude = -80.1030;
    MapPin *bombayanno = [[MapPin alloc] init]; //anno = annotation
    bombayanno.title = @"Bombay";
    bombayanno.coordinate = bombayPin;
    [mapview addAnnotation:bombayanno];

    - (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//MapView Delegate Methods
//each time an annotation appears on map this method is called
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

    //check to see if anno is not the ones we created but the default one that shows current location - don't want this one
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;


    //create pin view
    MKPinAnnotationView *myPin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MapPinIdentifier"];

    myPin.pinColor = MKPinAnnotationColorGreen;
    myPin.animatesDrop = YES;
    myPin.canShowCallout = YES;

    //add detail disclosure button to display details in another view
    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [detailButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
    myPin.rightCalloutAccessoryView = detailButton;

    return myPin;
}

// user tapped the disclosure button in the callout
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [self performSegueWithIdentifier:@"ShowDetails" sender:view];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ShowDetails"]) {


    }
}

-(IBAction)SetMap:(id)sender; {

    switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
        case 0:
            mapview.mapType = MKMapTypeStandard;
            break;
        case 1:
            mapview.mapType = MKMapTypeSatellite;
            break;
        case 2:
            mapview.mapType = MKMapTypeHybrid;
            break;

        default:
            break;
    }
}

@end

2 个答案:

答案 0 :(得分:-1)

只需在故事板中为同一个控制器创建一个单独的segue。

您还可以使用

在代码中调用命名的segue
[[self performSegueWithIdentifier:@"showDetail"]; 

答案 1 :(得分:-1)

这可能不是最好的方法,但这可以满足您的需求,您可以使用NSUserDefaults来存储特定设置。

将内容保存到NSUserDefaults

[[NSUserDefaults standardUserDefaults] setObject:yourObject forKey:@"mySavedKey"];
[[NSUserDefaults standardUserDefaults] synchronize];

要检索某些内容:

yourObject = [[NSUserDefaults standardUserDefaults] objectForKey:@"mySavedKey"];

您的对象可能是NSStringNSArray等。您可以在Apple开发者网页上查找所需的和合适的对象。

Apple Developer Resources

NSUserDefaults可在应用范围内使用,因此您可以在应用中的任何位置访问它,这就是为什么它适合您将数据从一个ViewController“传输”到另一个{{1}}的需要。 / p>