【发布时间】:2018-06-29 15:32:59
【问题描述】:
我想将包含注释标题的 NSString(“theData”)传递给另一个视图控制器,当我在第一个视图控制器中使用 NSLog 时,它确实返回了正确的标题,但是当我尝试在下一个视图控制器中的文本标签显示(null),当我在第二个视图控制器中使用 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];
}
【问题讨论】:
-
DireccionVC *direccionVC = [[DireccionVC alloc] init];:这是在创建一个全新的对象。它与您在- (IBAction)Continue:(id)sender中使用的VC不是同一个对象。不是,有什么理由使用retain?你不使用ARC吗?如果是,请改用strong。 -
我开始使用Objective C。如何通过不创建一个全新的对象来做到这一点?我不能在那个 didSelectAnnotationView 函数中使用 VC,也不能在 Continue IBAction 中使用数据?是的,你是对的,我将保留更改为强
-
轻松解决我的问题。更改行 DireccionVC *direccionVC = [[DireccionVC alloc] init];以 DireccionVC *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"DireccionVC"] 为!方向VC;
-
那么保留它?将
@property (nonatomic, strong) NSString *selectedData;添加到UbicacionVC.m和_selectedData = view.annotation.title;,然后在Continue:上添加VC.theData = _selectedData -
@TheNitram 感谢您的帮助,但没有奏效。
标签: objective-c nsstring