小编Bra*_*don的帖子

计算两组坐标之间的距离(NSStrings)

我有两组地理坐标,我正在尝试计算它们之间的距离.我已经做了一些挖掘,但无法弄清楚如何做到这一点.我试图获得用户(userLatitude/userLongitude)和地点(placeLatitude/placeLongitude)之间的距离.我的坐标存储为NSStrings,如下所示.有谁知道如何做到这一点?谢谢你们!

NSString *userLatitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
getUserLatitude];

NSString *userLongitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
getUserLongitude];

NSString *placeLatitude = [element1 objectForKey:@"placeLatitude"];

NSString *placeLongitude = [element1 objectForKey:@"placeLongitude"];
Run Code Online (Sandbox Code Playgroud)

xcode objective-c nsstring cllocationmanager ios

2
推荐指数
1
解决办法
3415
查看次数

在 NSUserDefaults 中存储日期抛出异常

我正在将 date1 与 date2 与一小段代码进行比较,并收到崩溃通知。

我在 NSUserDefaults 中得到 date1 :

NSDate *date1 = [[NSUserDefaults standardUserDefaults] 
objectForKey:@"date1"];

NSDate *date2 = [[NSDate alloc] init]; 

if (fabs([date2 timeIntervalSinceDate:date1]) < 60)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"60 seconds or less" message:@"Text" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
    [alert show];
    return;
}
Run Code Online (Sandbox Code Playgroud)

我将 date1 存储在 NSUserDefaults 中,如下所示:

NSDate *date1 = [[NSDate alloc] init];

[[NSUserDefaults standardUserDefaults] setObject:date1 forKey:@"date1"];

[[NSUserDefaults standardUserDefaults] synchronize];
Run Code Online (Sandbox Code Playgroud)

该应用程序崩溃说 -[__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance. 谁有想法?

谢谢!

objective-c nsdate nsuserdefaults ios

2
推荐指数
1
解决办法
3599
查看次数

iOS JSON数组和MapKit

我正在尝试使用MapKit映射JSON数组.我可以使用下面的代码在地图上获得一个点,但是我需要标记几十个引脚,并且我准备了一个JSON数组.我的单点代码如下.

在我的.h文件中:

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

@interface MapViewController : UIViewController {

MKMapView *mapView;
NSData *data;

}

@property (nonatomic, retain) IBOutlet MKMapView *mapView;

@end
Run Code Online (Sandbox Code Playgroud)

在我的.m文件中:

NSData *data = @"[{"id":"1","name":"Jiffy Lube","lat":"21.306","lon":"-157.861"},    
{"id":"2","name":"Bills
Oil","lat":"21.301","lon":"-157.863"},{"id":"3","name":"Auto Zone","lat":"21.307","lon":"-
157.862"}]";

// parse the JSON into a NSArray

NSError *error;
NSArray *array = [NSJSONSerialization JSONObjectWithData:data
                                                 options:0
                                                   error:&error];
Run Code Online (Sandbox Code Playgroud)

iphone xcode objective-c mapkit ios

2
推荐指数
1
解决办法
4343
查看次数

将图像添加到MapViewAnnotation

我正在尝试将图像添加到我的MKAnnotation中.我有下面的代码.我该怎么把@"map_icon.png"添加到这个?任何帮助都会很棒.谢谢!

mapView.m文件:

// retrieve latitude and longitude from the dictionary entry

location.latitude = [dictionary[@"placeLatitude"] doubleValue];
location.longitude = [dictionary[@"placeLongitude"] doubleValue];

// create the annotation

newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"name"]
                                               andCoordinate:location];
Run Code Online (Sandbox Code Playgroud)

MapViewAnnotation.m文件

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d 
{
title = ttl;
coordinate = c2d;
return self;
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

iphone objective-c mapkit mkmapview ios

2
推荐指数
1
解决办法
590
查看次数

在位置发生重大更改时重新加载/刷新View Controller(来自App委托)

我试图通过更改重要位置来刷新视图。位置管理器在我的应用程序委托中,并且我尝试使用NSNotificationCenter通知视图控制器是否在用户更改位置时刷新。我不确定如何刷新以刷新视图。我目前已将其设置如下。我制作了一个“视图控制器(刷新视图)”块,在该块中,我认为将进行刷新的代码。有人知道如何进行这项工作吗?谢谢!

应用程序委托(收听重要的位置更改):

- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation  
*)newLocation fromLocation:(CLLocation *)oldLocation {

[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshView" object:nil];

}
Run Code Online (Sandbox Code Playgroud)

视图控制器(收听NSNotificationCenter):

- (void)viewDidLoad {

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshView:) 
name:@"refreshView" object:nil];

}
Run Code Online (Sandbox Code Playgroud)

视图控制器(刷新视图):

-(void)refreshView:(NSNotification *) notification {

//CODE TO REFRESH THE VIEW HERE

}
Run Code Online (Sandbox Code Playgroud)

iphone xcode objective-c ios

2
推荐指数
1
解决办法
4万
查看次数

iPhone书签UIWebView - 显示页面标题而不是URL

我有一个完全可操作的Web浏览器应用程序,用于存储书签页面.单击书签按钮时,将显示存储的网站的列表视图.我希望listview显示页面的标题,而不是显示URL.我能够使用下面的代码获取页面的标题,但不知道如何或在何处实现它.

NSString*webSiteTitle = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];

我已经为下面的两个ViewControllers包含了.m和.h文件.请告诉我该怎么做.

谢谢!

ExplorerViewController.h

@interface ExplorerViewController : UIViewController <UITextFieldDelegate, UIWebViewDelegate, UIActionSheetDelegate>{
    UITextField *urlField;
    UIBarButtonItem *refreshButton;
    UIBarButtonItem *backButton;
    UIBarButtonItem *forwardButton;
    UIBarButtonItem *bookMarksButton;
    UIActivityIndicatorView *loadingActivity;
    UIWebView *webView;
    UINavigationBar *navigationBar;
}

@property (nonatomic, retain) IBOutlet UITextField *urlField;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *refreshButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *backButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *forwardButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *bookMarksButton;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *loadingActivity;
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) IBOutlet UINavigationBar *navigationBar; …
Run Code Online (Sandbox Code Playgroud)

iphone xcode cocoa-touch objective-c uiwebview

1
推荐指数
1
解决办法
2758
查看次数

从ActionSheet中删除红色破坏性按钮

我试图在没有破坏性按钮的情况下弹出一个动作表.如果我只是尝试从下面的代码中删除破坏性按钮,我会收到一个错误:UIActionSheet没有可见的界面.有谁知道为什么会这样?如何删除红色破坏性按钮?谢谢!

UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Title" 
delegate:self cancelButtonTitle:@"Cancel Button" destructiveButtonTitle:@"Destructive 
Button" otherButtonTitles:@"Other Button 1", @"Other Button 2", nil];
Run Code Online (Sandbox Code Playgroud)

objective-c uiactionsheet ios

1
推荐指数
1
解决办法
1929
查看次数

停止NSTimer持续增加间隔

我的countDown计时器有一个奇怪的问题.该计时器从设定时间(即60秒)开始倒计时.这段代码放在myViewDidLoad方法中.除非我回去再加载视图,否则一切正常.每次加载视图时,倒计时都会增加1秒.

例如:

  1. 首次装载:60,59,58 ......
  2. 第二次加载:60,58,56 ......
  3. 第三次加载:60,57,54 ......

我的代码如下.有谁知道为什么会这样?我需要在某个地方发布一些东西吗?谢谢!

countDown=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self 
selector:@selector(TimeOver) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nstimer ios

1
推荐指数
1
解决办法
321
查看次数

显示警报中当前视图控制器的名称

我试图显示一个警报,如果屏幕上显示的视图控制器是"PrimaryViewController"我不确定如何获取视图控制器的名称,然后将其转换为NSString ...任何帮助将非常感谢! !

//get name of current view controller
UIViewController *currentVC = self.navigationController.visibleViewController;
if ([currentVC isEqualToString:@"PrimaryViewController"])
{
    //display name of current view controller in alert    
    UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Your current view controller:" message:currentVC delegate:nil 
cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}
Run Code Online (Sandbox Code Playgroud)

iphone xcode objective-c ios

1
推荐指数
1
解决办法
3058
查看次数

防止用户点击UIImageView后面的按钮

我有一个带有几个按钮的视图.单击其中一个按钮时,会在所有其他按钮上弹出"图像视图".即使图像覆盖了所有按钮,它们仍然可以点击.在XIB中是否有办法使用户无法点击图像视图?我知道我可以以编程方式进入并一次禁用/启用按钮,但我确信有一种更简单的方法可以做到这一点.有任何想法吗?谢谢!

以下建议之一是我在图像上启用用户交互.但是,我仍然可以单击图像视图下方的按钮.请以图片为例.

在此输入图像描述

iphone objective-c ios

1
推荐指数
1
解决办法
1105
查看次数