iOS:将数据从视图控制器传递到Detail视图控制器,在storyboard中返回null

时间:2014-07-22 04:33:28

标签: ios objective-c json model-view-controller

我有json文件,我可以将所有信息打印到我的Log,从这个json我在地图中显示不同的位置,当我点击注释按钮时我必须显示detailview,我可以显示我的详细信息views,但我的数据为空,请您在此实施中帮助我,提前致谢!

这是我的方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self fetchData];
}


-(void)fetchData
{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:BASED_URL]];
    [request setHTTPMethod:@"GET"];
    [request setValue:@"/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

    NSURLResponse *response;
    NSData *GETReply = [NSURLConnection sendSynchronousRequest:request 
returningResponse:&response error:nil];

    NSString *theReply = [[NSString alloc] initWithBytes:[GETReply bytes] length:[GETReply 
 length] encoding: NSASCIIStringEncoding];



    NSLog(@"Reply: %@", theReply);

    CLLocationCoordinate2D location;                         
    NSMutableArray *newAnnotations = [NSMutableArray array]; 
    NSError *error;
    NSArray *array = [NSJSONSerialization JSONObjectWithData:GETReply
                                                     options:0
                                                       error:&error];
    if (error != nil)
    {
        // handle the error
    }

    for (NSDictionary *dictionary in array)
    {
        // retrieve latitude and longitude from the dictionary entry

        location.latitude = [dictionary[@"latitude"] doubleValue];
        location.longitude = [dictionary[@"longitude"] doubleValue];

        // create the annotation
        MyAnnotation *newAnnotation;

        newAnnotation = [[MyAnnotation alloc] init];
        newAnnotation.title = dictionary[@"applicant"];
        newAnnotation.subtitle = dictionary[@"company"];
        newAnnotation.status = dictionary[@"status"];
        newAnnotation.company = dictionary[@"company"];
        newAnnotation.coordinate = location;

        [newAnnotations addObject:newAnnotation];
    }

    [self.mapView addAnnotations:newAnnotations];
}

我有一个方法应该将数据传递给我的详细视图

 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control
{
     MyAnnotation *annotation = view.annotation;
    DetailViewController *detail = [[DetailViewController alloc] initWithNibName:nil 
bundle:nil];
    detail.status.text = annotation.status;
    NSLog(@"status is%@",annotation.status );

    [self.navigationController pushViewController:detail animated:YES];
}

在我的日志中我有:状态为活动

但在我的详细视图中Log I有Null

#import "ViewController.h"

@interface DetailViewController : ViewController
@property (weak, nonatomic) IBOutlet UILabel *status;

@end

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

    self.status.text = _status.text;
    NSLog(@"Status is %@",_status );
    NSLog(@"Status is %@ ",self.status.text);
 }

我的状态打印为:状态为空 状态为空

更新:这是我的标签

@property (strong, nonatomic) IBOutlet UILabel *status;

5 个答案:

答案 0 :(得分:1)

您需要传递一个字符串,而不是尝试在ViewController中设置标签的文本,因为在您尝试执行此操作时尚未加载详细控制器的视图。标签将是零。在DetailController中,创建一个字符串属性

@interface DetailViewController : ViewController
@property (weak, nonatomic) IBOutlet UILabel *status;
@property (strong,nonatomic) NSString *passedInString;
@end

- (void)viewDidLoad {
    [super viewDidLoad];
    self.status.text = self.passedInString;
 }

在ViewController中,传递字符串

 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control
{
     MyAnnotation *annotation = view.annotation;
    DetailViewController *detail = [[DetailViewController alloc] initWithNibName:nil 
bundle:nil];
    detail.passedInString = annotation.status;
    NSLog(@"status is%@",annotation.status );

    [self.navigationController pushViewController:detail animated:YES];
}

答案 1 :(得分:0)

DetailViewController *detail = [[DetailViewController alloc] initWithNibName:nil bundle:nil];

// detail's viewDidLoad invoked here!

detail.status.text = annotation.status;

这是关于时间的。在调用viewDidLoad之后设置实例的status.text。

<强> DetailViewController.h

@interface DetailViewController : UIViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil text:(NSString *)text;
@end

<强> DetailViewController.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil text:(NSString *)text
{
    if(self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        self.status.text = text;
    }
    return self;
}

这个怎么样?当然,您必须将viewController实例化为...

DetailViewController *detail = [[DetailViewController alloc] initWithNibName:nil bundle:nil text:annotation.status];

答案 2 :(得分:0)

DetailViewController标头文件中创建一个NSString:

@property (nonatomic, strong) NSString *passData;

viewDidLoad中的DetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.status.text = _passData;
}

mapView method

中的viewController
detail.passData = annotation.status;

答案 3 :(得分:0)

您可以尝试将身份检查器中的故事板标识符分配给DetailViewController

并使用它:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control
{
     MyAnnotation *annotation = view.annotation;
    DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"YOUR_IDENTIFIER"];;
    detail.status.text = annotation.status;
    NSLog(@"status is%@",annotation.status );

    [self.navigationController pushViewController:detail animated:YES];
}

答案 4 :(得分:-1)

试试这个,我认为状态标签属性没有从storyboard或nib文件中删除。多数民众赞成就是零。请检查

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
     MyAnnotation *annotation = view.annotation;
     DetailViewController *detail = [[DetailViewController alloc]initWithNibName:nil bundle:nil];
     detail.status= annotation.status;
     NSLog(@"status is%@",annotation.status );
     [self.navigationController pushViewController:detail animated:YES];
}

#import "ViewController.h"

@interface DetailViewController : ViewController
@property (strong, nonatomic) NSString *status;

@end

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"Status is %@",_status );
}