【问题标题】:How to Refresh Map with IBAction如何使用 IBAction 刷新地图
【发布时间】:2014-11-14 17:56:19
【问题描述】:

这里仍然使用 Ojective-C :)

我目前正在尝试通过触摸按钮重新加载 mapView 及其所有属性,但它没有填充。这是我正在使用的:

-(IBAction)refreshLocation:(id)sender {
 [self refresh];
}

-(void)refresh {
[mapView setNeedsDisplay];
}

什么都没有发生:这是我聚合数据点的方式,可能不是最实用的,但对我的情况来说是最合理的:

MapViewController.h

#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>

@interface MapViewController : UIViewController
{
IBOutlet MKMapView *mapView;
IBOutlet UISegmentedControl *segmentedControl;
__weak IBOutlet UIBarButtonItem *refreshLocation;

}

@property (nonatomic, retain) MKMapView *mapView;
@property (nonatomic, retain) UISegmentedControl *segmentedControl;

- (IBAction)segmentedControllChanged:(id)sender;
- (IBAction)refreshLocation:(id)sender;

@end

MapViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];

double cost2 = [self.latLocation doubleValue];
double cost = [self.longLocation doubleValue];
CLLocationCoordinate2D location;
location.latitude =  cost2;
location.longitude = cost;

MKCoordinateRegion region;
region.center.latitude = (double)  cost2;
region.center.longitude = (double) cost;
region.span = MKCoordinateSpanMake(.99, .99);
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];

-(NSString *)latLocation {
NSError *error = nil;
NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://mywebsite.com"] encoding:NSASCIIStringEncoding error:&error];
if(html) {
    NSLog(@"HTML %@", html);

    NSRange r = [html rangeOfString:@"<h3 class=\"font\">"];
    if (r.location != NSNotFound) {
        NSRange r1 = [html rangeOfString:@"</h3>"];
        if (r1.location != NSNotFound) {
            if (r1.location > r.location) {
                NSString *latitude = [html substringWithRange:NSMakeRange(NSMaxRange(r), r1.location - NSMaxRange(r))];
                NSLog(@"%@", latitude);
                latLocation = latitude;

            }
        }
    }
}
return latLocation;
}

-(NSString *)longLocation {
NSError *error = nil;
NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://mywebsite.com"] encoding:NSASCIIStringEncoding error:&error];
if(html) {
    NSLog(@"HTML %@", html);

    NSRange r = [html rangeOfString:@"<h4 class=\"font\">"];
    if (r.location != NSNotFound) {
        NSRange r1 = [html rangeOfString:@"</h4>"];
        if (r1.location != NSNotFound) {
            if (r1.location > r.location) {
                NSString *longitude = [html substringWithRange:NSMakeRange(NSMaxRange(r), r1.location - NSMaxRange(r))];
                NSLog(@"%@", longitude);
                longLocation = longitude;

            }
        }
    }
}
return longLocation;

}

-(NSString *)annotationTitle {
NSError *error = nil;
NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://mywebsite.com"] encoding:NSASCIIStringEncoding error:&error];
if(html) {
    NSLog(@"HTML %@", html);

    NSRange r = [html rangeOfString:@"<h1 class=\"font\">"];
    if (r.location != NSNotFound) {
        NSRange r1 = [html rangeOfString:@"</h1>"];
        if (r1.location != NSNotFound) {
            if (r1.location > r.location) {
                NSString *titleString = [html substringWithRange:NSMakeRange(NSMaxRange(r), r1.location - NSMaxRange(r))];
                NSLog(@"%@", titleString);
                annotationTitle = titleString;

            }
        }
    }
}
return annotationTitle;

}
-(NSString *)subTitle {

NSError *error = nil;
NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://mywebsite.com"] encoding:NSASCIIStringEncoding error:&error];
if(html) {
    NSLog(@"HTML %@", html);

    NSRange r = [html rangeOfString:@"<h2 class=\"font\">"];
    if (r.location != NSNotFound) {
        NSRange r1 = [html rangeOfString:@"</h2>"];
        if (r1.location != NSNotFound) {
            if (r1.location > r.location) {
                NSString *subtitleString1 = [html substringWithRange:NSMakeRange(NSMaxRange(r), r1.location - NSMaxRange(r))];
                NSLog(@"%@", subtitleString1);
                subTitle = subtitleString1;

            }
        }
    }
}
return subTitle;
}

- (IBAction)refreshLocation:(id)sender {

[self refresh];

}

-(void)refresh
{
[self.mapView setNeedsDisplay];

    // Create the request.
}

我需要重新加载所有这些字符串方法以从这些标题中显示的 mywebsite.com 获取新数据。但目前什么都没有发生。有什么建议吗?

【问题讨论】:

    标签: ios refresh screen reload ibaction


    【解决方案1】:

    试试这个

    - (IBAction)refreshLocation:(id)sender {
    
    [self viewDidLoad];
    
    }
    

    【讨论】:

    • 如果它不起作用,告诉我你在哪里调用annotationTitle,subTitle方法进入mainmethod
    猜你喜欢
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 2019-04-23
    • 1970-01-01
    相关资源
    最近更新 更多