目标C-将NSString传递给另一个视图控制器将返回null

时间:2018-06-29 15:32:59

标签: objective-c nsstring

我想将包含批注标题的NSString(“ theData”)传递给另一个视图控制器,当我在第一个视图控制器中使用NSLog时,它确实返回正确的标题,但是当我尝试在其中显示时下一视图控制器中的文本标签显示为(空),当我在第二个视图控制器中使用NSLog时,它也不返回任何内容。

UbicacionVC.m

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


 }


- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
   {
    for (MKPointAnnotation *annotation in mapView.annotations) {
         if (view.annotation != annotation) {
        [mapView removeAnnotation:annotation];
        NSLog(@"yes!");

    }

}
NSString *theData = view.annotation.title;
DireccionVC *direccionVC = [[DireccionVC alloc] init];
direccionVC.theData = theData;


}
- (IBAction)Continue:(id)sender {
UIViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"DireccionVC"];
[self.revealViewController setFrontViewController:VC animated:YES];



 }

DireccionVC.h

 @interface DireccionVC : UIViewController{
     NSString *theData;

 }

 @property (nonatomic, retain) NSString *theData;


 @end

DireccionVC.m

 @interface DireccionVC ()
 @end

 @implementation DireccionVC

 @synthesize theData;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.     
self.lblDireccion.text = [NSString stringWithFormat:@"%@", theData];

}

1 个答案:

答案 0 :(得分:0)

尝试一下。替换

- (IBAction)Continue:(id)sender {
    UIViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"DireccionVC"];
    [self.revealViewController setFrontViewController:VC animated:YES];
 }

使用

- (IBAction)Continue:(id)sender {
    DireccionVC *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"DireccionVC"] as! DireccionVC;
    VC.theData = theData;
    [self.revealViewController setFrontViewController:VC animated:YES];
 }

只需确保情节提要中的DirectionVC实际上是DirectionVC类。 enter image description here