Objective C - 将 NSString 传递给另一个视图控制器返回 null
Objective C - Passing NSString to another view controller is returning null
我想将包含注释标题的 NSString ("theData") 传递给另一个视图控制器,当我在第一个视图控制器中使用 NSLog 时,它会 return 正确的标题,但是,当我尝试在下一个视图控制器的文本标签中显示它时,它显示 (null) 并且当我在第二个视图控制器中使用 NSLog 时,它也没有 return 任何东西。
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];
}
试试这个。替换
- (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 class。
我想将包含注释标题的 NSString ("theData") 传递给另一个视图控制器,当我在第一个视图控制器中使用 NSLog 时,它会 return 正确的标题,但是,当我尝试在下一个视图控制器的文本标签中显示它时,它显示 (null) 并且当我在第二个视图控制器中使用 NSLog 时,它也没有 return 任何东西。
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];
}
试试这个。替换
- (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 class。