Question

I am trying to push DetailViewController when a detail disclosure button is pushed within the callout of an annotation. However when I am directed to the DetailViewController I am receiving only a black screen instead of the labels I have set up. What is the reason for the black screen? Here is my code for my first ViewController.

#import "ViewController.h"
#import "DetailViewController.h"
#import "Annotation.h"

#import "City.h"

@interface ViewController ()
@property (nonatomic, strong) IBOutlet DetailViewController *detailViewController;


@end
#define getDatalURL @"http://www.club-hop.com/apptest.php"

@implementation ViewController

@synthesize mapView,jsonArray,citiesArray;



- (void)viewDidLoad
{
[super viewDidLoad];
[self retrieveData];
self.detailViewController = [[DetailViewController alloc] init];
/* Zoom the map to current location.
[self.mapView setShowsUserLocation:YES];
[self.mapView setUserInteractionEnabled:YES];
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow];*/
City * cityObject;

// load external page into UIWebView
NSMutableArray * locations= [[NSMutableArray alloc]init];
CLLocationCoordinate2D location;
Annotation * myAnn;

for(int u=0; u<citiesArray.count;u++){
cityObject=[citiesArray objectAtIndex:u];

myAnn=[[Annotation alloc]init];
NSNumber *aLat= cityObject.Latitude;
NSNumber *aLon= cityObject.Longitude;

double lat = [aLat doubleValue];
double lon = [aLon doubleValue];

location.latitude= lat;
location.longitude=lon;
myAnn.coordinate = location;
myAnn.title=cityObject.clubName;
myAnn.subtitle=cityObject.cityName;
[locations addObject:myAnn];}

[self.mapView addAnnotations:locations];


}

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



//class methods
-(void) retrieveData{
NSURL * url= [NSURL URLWithString:getDatalURL];
NSData * data= [NSData dataWithContentsOfURL:url];
jsonArray= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

//setup cities array
citiesArray=[[NSMutableArray alloc]init];

for(int i=0; i<jsonArray.count;i++){
    NSString * cID= [[jsonArray objectAtIndex:i] objectForKey:@"id"];
    NSString * cName= [[jsonArray objectAtIndex:i] objectForKey:@"cityName"];
    NSString * cCountry= [[jsonArray objectAtIndex:i] objectForKey:@"cityCountry"];
    NSString * cLine= [[jsonArray objectAtIndex:i] objectForKey:@"clubLine"];
    NSString * clName= [[jsonArray objectAtIndex:i] objectForKey:@"clubName"];
    NSNumber * cLatitude= [[jsonArray objectAtIndex:i] objectForKey:@"Latitude"];
    NSNumber * cLongitude= [[jsonArray objectAtIndex:i] objectForKey:@"Longitude"];

    [citiesArray addObject:[[City alloc]initWithCityName:cName andCityCountry:cCountry   andClubName:clName andClubLine:cLine andLatitude:cLatitude andLongitude:cLongitude andCityId:cID]];

}

}

#pragma mark - MKMapViewDelegate

// user tapped the disclosure button in the callout
//
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view   calloutAccessoryControlTapped:(UIControl *)control
{
 [self.navigationController pushViewController:self.detailViewController animated:YES];

}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id   <MKAnnotation>)annotation
{

MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView   dequeueReusableAnnotationViewWithIdentifier:@"pinView"];

if (!pinView)
{
    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation    reuseIdentifier:@"pinView"] ;
    pinView.pinColor=MKPinAnnotationColorGreen;
    pinView.animatesDrop=YES;
    pinView.canShowCallout=YES;
    UIButton * rightButton= [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pinView.rightCalloutAccessoryView=rightButton;
}
else{
    pinView.annotation=annotation;
}
return pinView;

    }


@end

detail controller file

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

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

@end
Était-ce utile?

La solution

OK you don't seem to understand what I'm asking so i'm making a guess your using a .xib file to add you UI stuff?

if so you need to use this:

[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]

change NIBName to whatever yours is.

EDIT

To do this with a storyboard you need to give the viewController a storyboardID and init it manually. Follow the steps outlined here: http://averagepro.com/2013/02/07/instantiate-storyboard-viewcontrollers-manually/

its roughly:

self.detailViewController = [[self.storyboard storyboardWithName:@"storyboard"bundle:nil] instantiateViewControllerWithIdentifier:@"DetailViewController"];

Autres conseils

You need to initialize your DetailViewController with the view.

For example, if the interface is a .xib, declare and initialize your DetailViewController using something like:

self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

If it's a storyboard, use the identifier you've attached to the DetailViewController in your Storyboard:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"StoryboardName"
                                          bundle:nil];
self.detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewControllerID"];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top