简体   繁体   English

iOS如何在多个ViewController中的一个ViewController中启动按钮?

[英]iOS how to make button in one ViewController start actions in many ViewControllers?

I have similar problem to the problem which was discussed here: iOS how to make a button run a code for another view controller? 我有一个与此处讨论的问题类似的问题: iOS如何使按钮为另一个视图控制器运行代码? but it didn't seem to be working on my app.. I think I am just making some stupid mistake cause I am really a beginner so I would appreciate any help. 但是它似乎无法在我的应用程序上运行。.我想我只是在犯一些愚蠢的错误,因为我确实是一个初学者,所以我将不胜感激。 So my app (app is something like Endomondo but much simpler of course) which is tabbed bar app with navigation contoller because from FirstViewController there is connection to a MapViewController which is simply a map. 因此,我的应用程序(类似于Endomondo的应用程序,但当然要简单得多)是带有导航控制器的选项卡式应用程序,因为从FirstViewController可以连接到MapViewController,后者只是一个地图。 In FirstViewController there is a button - Start and I would like that pressing that buttton start action myLocation which is: 在FirstViewController中,有一个按钮-“开始”,我希望按下该按钮开始动作myLocation,即:

-(void)myLocation:(id)sender{
    locationManager = [[CLLocationManager alloc]init];
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    [locationManager startUpdatingLocation];
    [mapview setMapType:MKMapTypeStandard];
    [mapview setZoomEnabled:YES];
    [mapview setScrollEnabled:YES];
    MKCoordinateRegion region = { {0.0, 0.0}, {0.0, 0.0} };
    region.center.latitude = locationManager.location.coordinate.latitude;
    region.center.longitude = locationManager.location.coordinate.longitude;
    region.span.longitudeDelta = 0.007f;
    region.span.latitudeDelta = 0.007f;
    [mapview setRegion:region animated:YES];
    [mapview setDelegate:sender];

}

and my IBAction in FirstViewController looks like this: 我在FirstViewController中的IBAction看起来像这样:

 -(IBAction)pressStart{
    [countingSeconds invalidate];
    countingSeconds = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timer) userInfo:nil repeats:YES];
  MapViewController *mapvc = [[MapViewController alloc]init];
    [mapvc myLocation];
    [self.navigationController pushViewController:mapvc animated:YES];

}

and of course the line with [mapvc myLocation]; 当然还有[mapvc myLocation]的行; is wrong. 是错的。 The error is: no visible @interface for MapViewController declares the selector myLocation. 错误是:MapViewController的无可见@interface声明选择器myLocation。 I have declared: -(void)myLocation:(id)sender; 我已经声明-(void)myLocation:(id)sender; in MapViewController.h and imported MapViewController.h in FirstViewController.m MapViewController.h导入,并在FirstViewController.m导入MapViewController.h

The error is: no visible @interface for MapViewController declares the selector myLocation. 错误是:MapViewController的无可见@interface声明选择器myLocation。 I have declared -(void)myLocation:(id)sender; 我已经声明了-(void)myLocation:(id)sender; in MapViewController.h and imported MapViewController.h in FirstViewController.m 在MapViewController.h中导入,并在FirstViewController.m中导入MapViewController.h

If this is the case, to call that method, you need to use this instead: 在这种情况下,要调用该方法,您需要使用以下方法:

[mapvc myLocation:self];

Note that it has a parameter (sender) that you need to pass. 请注意,它具有您需要传递的参数(发送者)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM