在视图控制器之间传递数据
Pass data between view controller
我正在尝试将一个对象 (PFObject) 从一个视图控制器传递到另一个视图控制器,
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
RestauCardViewController *restauCard = [[RestauCardViewController alloc]init];
RestaurantAnnotation *restoAnnotation = (RestaurantAnnotation *)view.annotation;
restauCard.restaurant = restoAnnotation.restaurant;
[self performSegueWithIdentifier:@"segueToCard" sender:nil];
}
当我尝试在另一个视图控制器中显示对象时,我得到了 null:
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface RestauCardViewController : UIViewController
@property(nonatomic) PFObject *restaurant;
@end
这是我的 viewDidLoad 函数
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"The restaurant name is : %@",_restaurant[@"nom"]);
}
您必须在 UIViewController 方法“prepareSegue...
”中设置餐厅。它是在你 performeSegueWithIdentifier
之后调用的,所以目标控制器是可访问的,你可以测试 segue.identifier 并将餐厅设置为控制器。
示例:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"segueRestaurentDetails"]) {
RestauCardViewController *destController = (RestaurentDetailsViewController *)segue.destinationViewController;
destController.restaurant = (RestaurantAnnotation *)view.annotation;
}
在ViewController
中实现prepareForSegue:sender:
方法
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"segueToCard"]) {
RestauCardViewController *controller = (RestauCardViewController *)segue.destinationViewController;
controller.restaurant = (RestaurantAnnotation *)view.annotation;
}
}
您需要使用 -prepareForSegue 来管理这种情况,并且您需要一个 iVar 来保留餐厅名称。
所以在地图的 .m 文件顶部,添加一个 ivar NSString
@implementation yourViewController{
NSString *sRestName; //This is empty until the user selects a restaurant
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
sRestName = //Set the name of your restaurent here, it's just a string.
//You could set any other type of object (a restaurent object or a PFOjbect or anything,
//just change the ivar accordingly
[self performSegueWithIdentifier:@"segueToCard" sender:nil];
}
你要做的是用上面的代码替换你的旧代码,你只需要执行 segue 来调用下面的方法。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"fromHomeToList"]){
RestauCardViewController *vc = (RestauCardViewController*)segue.destinationViewController;
vc.restaurant = sRestName; //here you're just giving the property of the new controller the content of your ivar.
}
这样你就可以将一个对象从你的地图点击传递到你的下一个控制器。您还确定它永远不会为零,因为用户点击了它;如果它是零,那么,他一开始就不可能点击它!
请注意,我假设您使用的是字符串作为您的餐厅名称,但如果您更改顶部的 ivar,您可以使用任何您想要的,只要您可以通过点击检索它在地图上。如果您不能,我需要更多详细信息来引导您完成另一个解决方案。
如果您有任何问题,请问我,否则这应该有效!
我正在尝试将一个对象 (PFObject) 从一个视图控制器传递到另一个视图控制器,
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
RestauCardViewController *restauCard = [[RestauCardViewController alloc]init];
RestaurantAnnotation *restoAnnotation = (RestaurantAnnotation *)view.annotation;
restauCard.restaurant = restoAnnotation.restaurant;
[self performSegueWithIdentifier:@"segueToCard" sender:nil];
}
当我尝试在另一个视图控制器中显示对象时,我得到了 null:
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface RestauCardViewController : UIViewController
@property(nonatomic) PFObject *restaurant;
@end
这是我的 viewDidLoad 函数
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"The restaurant name is : %@",_restaurant[@"nom"]);
}
您必须在 UIViewController 方法“prepareSegue...
”中设置餐厅。它是在你 performeSegueWithIdentifier
之后调用的,所以目标控制器是可访问的,你可以测试 segue.identifier 并将餐厅设置为控制器。
示例:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"segueRestaurentDetails"]) {
RestauCardViewController *destController = (RestaurentDetailsViewController *)segue.destinationViewController;
destController.restaurant = (RestaurantAnnotation *)view.annotation;
}
在ViewController
中实现prepareForSegue:sender:
方法
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"segueToCard"]) {
RestauCardViewController *controller = (RestauCardViewController *)segue.destinationViewController;
controller.restaurant = (RestaurantAnnotation *)view.annotation;
}
}
您需要使用 -prepareForSegue 来管理这种情况,并且您需要一个 iVar 来保留餐厅名称。
所以在地图的 .m 文件顶部,添加一个 ivar NSString
@implementation yourViewController{
NSString *sRestName; //This is empty until the user selects a restaurant
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
sRestName = //Set the name of your restaurent here, it's just a string.
//You could set any other type of object (a restaurent object or a PFOjbect or anything,
//just change the ivar accordingly
[self performSegueWithIdentifier:@"segueToCard" sender:nil];
}
你要做的是用上面的代码替换你的旧代码,你只需要执行 segue 来调用下面的方法。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"fromHomeToList"]){
RestauCardViewController *vc = (RestauCardViewController*)segue.destinationViewController;
vc.restaurant = sRestName; //here you're just giving the property of the new controller the content of your ivar.
}
这样你就可以将一个对象从你的地图点击传递到你的下一个控制器。您还确定它永远不会为零,因为用户点击了它;如果它是零,那么,他一开始就不可能点击它!
请注意,我假设您使用的是字符串作为您的餐厅名称,但如果您更改顶部的 ivar,您可以使用任何您想要的,只要您可以通过点击检索它在地图上。如果您不能,我需要更多详细信息来引导您完成另一个解决方案。
如果您有任何问题,请问我,否则这应该有效!