使用segue设置详细视图控制器 [英] Setting up a detail view controller using a segue
问题描述
背景:
我有一个自定义的UIViewController类,我用自定义注释填充MKMapView。当用户选择注释时,将显示有关该注释的详细信息,并且还存在一个按钮,供用户选择并启动另一个UIViewController,其中包含有关该点上该点的详细信息。
I have a custom UIViewController class where I populate a MKMapView with custom annotations. When the user selects the annotation, details about that annotation are displayed, and a button is also present for the user to select and bring up another UIViewController with details about that point on the map.
代码:
我通过创建类来初始化新的视图控制器使用UserID(注意: - (id)initWithUserID:(NSInteger)userID;
在SecondViewController的头文件中声明:
I initialize the new view controller by creating the class with a UserID (Note: - (id) initWithUserID:(NSInteger) userID;
is declared in the header file of the 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到第二个视图控制器:
Creating the view controller is done when the button is pressed, and then immediately performs the segue to the second view controller:
- (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我能够确认在创建类时正确传递了值。但是,当我稍后在代码(viewWillAppear)中使用属性 userID
时,我不再需要使用它。我假设有一个内存问题,我没有照顾,但我似乎无法搞清楚。如何确保我在创建时传入类中的值/对象保持在那里?
Using NSLOG I'm able to confirm that the value is being passed correctly when creating the class. However, when I go to use the property userID
later on in the code (viewWillAppear), it's not longer there for me to use. I'm assuming that there's a memory issue that I'm not taking care of but I can't seem to figure it out. How do I ensure that the values/object I pass into a class upon creation stay there?
Side注意:我最初尝试过,传递 PinData
对象,但遇到了同样的问题,所以我知道它不是NSInteger问题。我也遵循了noa的建议并使用了 prepareForSegue
然而,我得到了上面提到的相同问题
Side Note: I originally tried, passing a PinData
object, but ran into the same issue, so I know it's not a NSInteger problem. I've also followed noa's suggestion and used prepareForSegue
however, I get the same problem presented above
推荐答案
segue负责实例化控制器。不要实例化另一个 - 它只是被丢弃,这就是为什么属性值似乎不坚持。
The segue is responsible for instantiating the controller. Don't instantiate another one – it'll just be discarded, which is why the property values don't seem to stick.
要设置视图控制器,请覆盖 -prepareForSegue:
而是在父视图控制器中:
To set up the view controller, override -prepareForSegue:
instead, in the parent view controller:
- (void) prepareForSegue:(UIStoryboardSegue *) segue sender:(id) sender {
if ([segue.identifier isEqualToString:@"PinDetail"]) {
SecondViewController *vc = segue.destinationViewController;
vc.userID = self.userID;
}
}
用上面的代码替换上面的最后一段代码:
Replace the last block of code above with this:
self.userID = ((RainFallDataPoint *)view.annotation).userID;
[self performSegueWithIdentifier:@"PinDetail" sender:self];
这篇关于使用segue设置详细视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!