背景:
我有一个自定义 UIViewController 类,我在其中使用自定义注释填充 MKMapView。当用户选择注释时,会显示有关该注释的详细信息,并且还会出现一个按钮供用户选择并调出另一个 UIViewController,其中包含有关地图上该点的详细信息。
编码:
我通过创建具有 UserID 的类来初始化新的视图控制器(注意:- (id) initWithUserID:(NSInteger) userID;
在 SecondViewController 的头文件中声明:
@interface SecondViewController ()
@property (nonatomic) NSInteger userID;
@end
@implementation RainFallDetailedViewController
@synthesize userID = _userID;
- (id) initWithUserID:(NSInteger) userID{
_userID = userID;
NSLOG(@"UserID: %i",self.userID); //correctly displays user id
return self;
}
- (void) viewWillAppear{
NSLOG(@"UserID: %i",self.userID); //userid is now 0
按下按钮时创建视图控制器,然后立即执行第二个视图控制器的 segue:
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
if ([(UIButton*)control buttonType] == UIButtonTypeInfoLight){ //I'm looking for recognition of the correct button being pressed here.
//SecondViewController is the second view controller with the detailed information about the map point.
//DataPoint is a custom class that contains the details regarding the map point.
SecondViewController *detailedMapController = [[SecondViewController alloc] initWithUserID:((DataPoint *)view.annotation).userID];
NSLOG(@"UserID: %i", ((DataPoint *)view.annotation).userID); //correctly displays userID
[self performSegueWithIdentifier:@"PinDetail" sender:detailedMapController];
}
}
问题:
使用 NSLOG 我能够确认在创建类时该值被正确传递。但是,当我userID
稍后在代码 (viewWillAppear) 中使用该属性时,它不再可供我使用。我假设有一个我没有处理的内存问题,但我似乎无法弄清楚。如何确保在创建时传递给类的值/对象留在那里?
旁注:我最初尝试传递一个PinData
对象,但遇到了同样的问题,所以我知道这不是 NSInteger 问题。我也遵循了 noa 的建议并使用prepareForSegue
了,但是我遇到了上面提到的同样的问题