iphone - UITextField 和有趣的程序

标签 iphone xcode

首先,对不起我的英语...我想编写一个程序来计算两个城市之间的距离。例如,在 UITextField 中我们写巴黎,在第二个 UITextField 中我们写第二个“伦敦”。我们有所有城市的经度和纬度作为基础。我们的程序有使用这四个数字的距离公式。我们知道这个公式。

当用户在 UITextField 中插入名称时,我想获取此 .text 并将其与我们的基础进行比较。怎么做??我可以编写这样的程序,但它......愚蠢:

@interface City : UIViewController{

    IBOutlet UITextField *city1;

    IBOutlet UITextField *city2;
}

@property (nonatomic, retain) IBOutlet UITextField *city1;
@property (nonatomic, retain) IBOutlet UITextField *city2;

-(void) calculate;

@end




#import "City.h"

@implementation City

@synthesize city1, city2;

-(void) viewDidLoad
{
    // to check changes in textfields i`m using NSTimer, I know it`s stupid but I haven`t learned how to make it in different way

    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(calculate) userInfo:nil repeats:YES];

}

-(void) obliczenia
{
    double distance;


    if([city1 is EqualToString:@"Paris"] && [city2 is EqualToString:@"London"])
    {

        double Paris_lat = x1;
        double Paris_lon = y1;
        double London_lat = x2;
        double London_lon = y2;

        distance =...; // we know the formula for distance but this is not important for now.
              // distance shows in some label but this is not a point of my problem. 

    }
}

它会运行,但是当我们的城市很少时。但当我们有上千个城市时,编写代码就毫无意义了。

我正在开始 iPhone 编程。 感谢您的耐心等待,请帮助我。这很重要,但我找不到解决方案。

最佳答案

看看这个指南: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html

基本上,将所有数据放入属性列表中,如下所示:

<dict>
    <key>Paris</key>
    <dict>
        <key>x</key>
        <real>20.2</real>
        <key>y</key>
        <real>30.4</real>
    </dict>
...
</dict>

然后像这样加载它:

NSDictionary * data = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NAMEOFPLIST" ofType:@"plist"]];
NSDictionary * paris = [data objectForKey:@"Paris"];
float xparis = [[paris objectForKey:@"x"] floatValue];
float yparis = [[paris objectForKey:@"y"] floatValue];

在你的情况下,你会做这样的事情:

NSDictionary * data = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NAMEOFPLIST" ofType:@"plist"]];
NSDictionary * city1Data = [data objectForKey:city1.text];
NSDictionary * city2Data = [data objectForKey:city2.text];
if (city1Data != nil && city2Data != nil) {
     // Do what you need...
}

然后对数据执行您需要的操作。

关于iphone - UITextField 和有趣的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8702505/

相关文章:

swift - 启用核心数据 Xcode Swift

iphone - 无法在 UITableview 的单元格内单击 UIButton

ios - 以编程方式添加约束

xcode - 新 Xcode 项目中的 OSX 10.7 预编译错误

iphone - 串联添加和删除 subview

objective-c - #define 宏和 xcode 警告以及缺少代码提示

iphone - 如何在 iPhone 中不使用 NSDictionary/NSMutableDictionary 中的 Key 来获取值?

ios - 如何在 Swift 2 中为引脚移动设置动画?

ios - 如何在 ios 中获取 YTPlayer 完成按钮状态?

iphone - 图像数据的 Base64 解码问题-iphone