【问题标题】:Annotation button switches from one mapView to another注释按钮从一个 mapView 切换到另一个
【发布时间】:2013-07-02 07:22:19
【问题描述】:

我现在想弄清楚,我应该如何使用注释按钮进行切换 到另一个地图视图。我正在开发的应用程序使用 MapBox - 地图。我检查了例子 由他们提供,但始终可以在两个地图之间以编程方式切换 通过标签栏(我不想使用这种情况)。 我正在使用故事板,我理解它很好,应该如何制作segue 在界面生成器中,但我认为我没有使用以编程方式集成的 地图视图上的按钮。我在两个头文件中都启动了“id”,并在 身份检查员也是如此。

这是代码的一部分,我在其中实现了带有注释的 RMMMapView 主视图控制器 - 视图控制器,它完美地工作:

- (void)viewDidLoad{

[super viewDidLoad];

RMMapBoxSource *onlineSource = [[RMMapBoxSource alloc] initWithMapID:(([[UIScreen    mainScreen] scale] > 1.0) ? kRetinaMapID : kNormalMapID)];

_mapView = [[RMMapView alloc] initWithFrame:self.view.bounds andTilesource:onlineSource];

_mapView.tileSource = [[RMMapBoxSource alloc] initWithMapID:(([[UIScreen mainScreen] scale] > 1.0) ? kRetinaMapID : kNormalMapID)];
_mapView.centerCoordinate = CLLocationCoordinate2DMake(0,0);

 _mapView.adjustTilesForRetinaDisplay = YES;
_mapView.zoom = 4;

_mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

[self.view addSubview:_mapView];
_mapView.showsUserLocation = YES;

[_mapView setConstraintsSouthWest:[_mapView.tileSource latitudeLongitudeBoundingBox].southWest
                            northEast:[_mapView.tileSource latitudeLongitudeBoundingBox].northEast];
RMPointAnnotation *annotation = [[RMPointAnnotation alloc] initWithMapView:_mapView
                                                                coordinate:_mapView.centerCoordinate andTitle:@"Hello, world!"];
[_mapView addAnnotation:annotation];
}

这是我尝试从 ViewController - 主 ViewController 调用 LowContentMap viewController 的部分:

- (void) prepareForSegue:(UIStoryboardSegue *) segue sender:(id) sender {
if ([segue.identifier isEqualToString:@"Hello, world!"]) {
    //LowContentMap *lowContentMap = segue.destinationViewController;

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
    LowContentMap *lowContentMap = [storyboard instantiateViewControllerWithIdentifier:@"lowContentMap"];
    lowContentMap.lcm = _vc;
}}

这是代码的一部分,应该填写:

- (void)mapView:(RMMapView *)mapView annotationView:(RMPointAnnotation *)annotation calloutAccessoryControlTapped:(UIControl *)control{
[self performSegueWithIdentifier:@"ShowSomeViewController" sender:annotation];
}

如果有人能尝试解决问题,那就太好了。 我在以下位置关注了 Noa 和 Kronos 之间的讨论: Setting up a detail view controller using a segue 但我仍然认为,带有“id”的部分是我做错的事情。提前致谢。

【问题讨论】:

    标签: ios annotations storyboard uistoryboardsegue mapbox


    【解决方案1】:

    1。我认为您的问题是您不知道如何显示另一个 viewController

    你应该好好阅读“View Controller Programming Guide”,尤其是“Prepresenting View Controllers from Other View Controllers”部分

    http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html

    我还建议您观看一些展示如何使用 ViewControllers 的精彩 WWDC 视频。

    2。如何创建和显示 viewController

    有很多方法可以做到这一点。例如。使用情节提要创建和显示viewController,在子控制器中定义协议,覆盖“performSegueWithIdentifier”以设置委托并实现协议以关闭vc。

    但是,在您的情况下,以编程方式完成所有操作似乎是有意义的。因此,您需要:

    a) 找到合适的位置添加您的操作

    b) 分配并初始化您的视图控制器:

    MyController *myController = [[MyController alloc] init];
    // setup as required, there should be at least a delegate (being able to dismiss the view)
    myController.delegate = self;
    

    如果您在情节提要中设计了 viewController,您可能希望改用这个 alloc/init 例程:

    MyController *myController = [[UIStoryboard storyboardWithName:@"Main.storyboard" bundle:nil] instantiateViewControllerWithIdentifier:@"MyController"];
    myController.delegate = self;
    

    c) 显示您的新视图控制器; 这取决于你是否有一个导航控制器:

    [self.navigationController pushViewController:myController animated:YES];
    

    ...或者如果您想以模态方式呈现它:

    [self presentViewController:myController animated:YES completion:NULL];
    

    d) 完成后关闭; 当你的另一个控制器完成它所做的一切时,它应该通知它的委托(实现你自己的协议!)它已经完成并且应该被解雇。委托是创建(分配/初始化)“myController”的原始视图控制器:

    // this method should be defined in a protocol and implemented in the vc that created (and owns) the child view controller
    // typically, this is the parent view controller 
    - (void)done {
        [self dismissViewControllerAnimated:YES completion:NULL];
    }
    

    如果你使用了navigationController,它不是-dismissViewControllerAnimated:,而是

    [self.navigationController popViewControllerAnimated:YES];
    

    希望这有助于澄清事情

    【讨论】:

    • 你好迈克尔,感谢您的回复。我在-tapOnCalloutAccessoryControl: 中覆盖了-prepareForSegue 方法,之后我将-[self performSegueWithIdentifier:@"switchToMap" sender:control]; 更改为[self presentViewController:lcm animated:YES completion:NULL]; 现在它可以工作了。 (我一直在尝试通过 segue 进行通信,但我以编程方式实现的 segue 不知道通过故事板制作的 segue。我还尝试使用 customContainer viewController。)
    猜你喜欢
    • 2020-08-24
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多