我是目标c的初学者,我想创建一个场景,说明如何在包含MapKit和TableView的主视图控制器中使用2个子视图控制器。我已经在互联网上搜索了1天,但是由于每个建议的不同步骤均失败,因此我无法提供任何解决方案。我知道有很多方法可以在视图控制器之间传递数据,我认为最好的方法是在这种情况下使用委托逻辑。 (请让我知道我是否错了)。顺便说一句,当我在MapKitViewController上设置虚拟按钮时,可以将光标更新或移动到特定位置,因此MapKit部分不在我无法解决的地方。我很确定问题出在两个同时激活的View Controller之间。
问题:
到目前为止已尝试:
goToLocation:(Location*) location
”,然后从TableViewController向该方法发送参数。 =>( goToLocation()接收到的纬度和经度参数,但MapKitView没有得到更新 )我想要的东西:
TableViewController.h
@class TableViewController; //define class
@protocol TableViewDelegate <NSObject> //define delegate protocol
//define delegate method to be implemented within another class
- (void) locationSelected: (TableViewController *) sender object:(Location *) location;
@end
@property (nonatomic, weak) id <TableViewDelegate> delegate; //define TableViewVCDelegate as delegate
TableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self notifyNow:[_locations objectAtIndex:indexPath.row]];
}
- (void) notifyNow:(Location *) location {
//this will call the method implemented in your other class
[self.delegate locationSelected:self object:(Location *) location];
NSLog(@"Selected: %@",[location name]);
}
MapViewController.h
//make it a delegate for TableViewVCDelegate
@interface MapViewController : UIViewController <TableViewDelegate>
MapViewController.m
- (void)viewDidLoad {
_tableViewController = [[TableViewController alloc]init];
_tableViewController.delegate = self; //set its delegate to self somewhere
}
!!_ This Method Doesn't Get Triggered _!!
- (void)locationSelected:(TableViewController *)sender object:(Location *)location {
NSLog(@"%@ is great!", [location name]);
}
MainViewController.m(主/根视图控制器)
#import "MainViewController.h"
#import "MapViewController.h"
#import "TableViewController.h"
@interface MainViewController ()
@property (strong, nonatomic) IBOutlet UIView *topCont;
@property (strong, nonatomic) IBOutlet UIView *bottomCont;
@property (strong, nonatomic) IBOutlet UIView *safeArea;
@end
@implementation MainViewController
MapViewController *mapViewVC;
TableViewController *tableViewVC;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationController setNavigationBarHidden:YES animated:YES];
tableViewVC = [self.storyboard instantiateViewControllerWithIdentifier:@"TableViewController"];
mapViewVC = [self.storyboard instantiateViewControllerWithIdentifier:@"TopChildVC"];
[self addChildViewController:tableViewVC];
[tableViewVC.view setFrame:CGRectMake(0.0f,
0.0f,
self.safeArea.frame.size.width,
self.safeArea.frame.size.height/2)];
[self.bottomCont addSubview:tableViewVC.view];
[tableViewVC didMoveToParentViewController:self];
[self addChildViewController:mapViewVC];
[mapViewVC.view setFrame:CGRectMake(0.0f,
0.0f,
self.safeArea.frame.size.width,
self.safeArea.frame.size.height/2)];
[self.topCont addSubview:mapViewVC.view];
[mapViewVC didMoveToParentViewController:self];
}
@end