简体   繁体   English

如何将UIPickerView对象的选择导出到CSV文件?

[英]How to export the selection of a UIPickerView object to a CSV file?

I am writing a small iOS app that I hope would facilitate my field work. 我正在编写一个小型iOS应用程序,希望对我的现场工作有所帮助。 I am on OS X El Capitan 10.11.4, using Xcode 7.3 and writing the app for iOS 9.3. 我在OS X El Capitan 10.11.4上,使用Xcode 7.3并为iOS 9.3编写应用程序。

Here is the whole idea: instead of plugging the data in to a piece of paper I want to write everything in to a basic form-like app to be used on an iPad, and then have the app transfer the data to a .csv file. 这是整个想法:我不想将数据插入纸中,而是将所有内容都写到了要在iPad上使用的基本表单形式的应用程序中,然后让该应用程序将数据传输到.csv文件中。

I have pretty much everything figured out (the structure of the app, the protocol to transfer and retrieve data etc. etc.), but there is one thing that is driving me crazy. 我已经弄清了几乎所有东西(应用程序的结构,传输和检索数据的协议等),但是有一件事情使我发疯。 Before I show you the code that I am using, here is the problem. 在向您展示我正在使用的代码之前,这是问题所在。 When I click the Save button and later on go to inspect the saved data, I cannot get the value selected by the UIPickerView objects to be transferred to the .csv file. 当我单击“保存”按钮,然后再去检查已保存的数据时,我无法获得UIPickerView对象选择的值以将其传输到.csv文件。 Everything else works fine. 其他一切正常。 I have two UIPickerView objects. 我有两个UIPickerView对象。 In one I have sex information, coded as M, F, and M/F; 我有一个性别信息,编码为M,F和M / F。 the other has information about age coded as Adu, Juv, Hat. 另一个具有关于年龄的信息,编码为Adu,Juv,Hat。 What I would like is for the app to save the value that is selected, but I couldn't quite figure that out. 我想要的是该应用程序保存所选的值,但我不太清楚。

Any help is much appreciated. 任何帮助深表感谢。 Thanks. 谢谢。

Here is the code that I am using for my ViewController.h 这是我用于ViewController.h的代码

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>
@property (strong, nonatomic) IBOutlet UIDatePicker *day;
@property (strong, nonatomic) IBOutlet UITextField *species;
@property (strong, nonatomic) IBOutlet UITextField *island;
@property (strong, nonatomic) IBOutlet UITextField *cay;
@property (strong, nonatomic) IBOutlet UITextField *pit;
@property (strong, nonatomic) IBOutlet UITextField *coordinatesN;
@property (strong, nonatomic) IBOutlet UITextField *coordinatesW;
@property (strong, nonatomic) IBOutlet UIPickerView *sex;
@property (strong, nonatomic) IBOutlet UIPickerView *age;

- (IBAction)saveInfo:(id)sender;
- (IBAction)retrieveInfo:(id)sender;
- (IBAction)retractKeyboard:(id)sender;

@property (strong, nonatomic) IBOutlet UITextView *resultView;

@end

And this is the code that I am using for my ViewConntroller.m 这是我用于ViewConntroller.m的代码

#import "ViewController.h"

@interface ViewController ()
{
    NSArray *pickerDataSex;
    NSArray *pickerDataAge;
}
@end

@implementation ViewController

@synthesize resultView;
@synthesize day;
@synthesize species;
@synthesize island;
@synthesize cay;
@synthesize pit;
@synthesize coordinatesW;
@synthesize coordinatesN;
@synthesize sex;
@synthesize age;

- (IBAction)retractKeyboard:(id)sender {
    [self resignFirstResponder];
}

- (IBAction)saveInfo:(id)sender {
    NSString *resultLine=[NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@,%@,%@,%@\n",
                          self.day.date,
                          self.species.text,
                          self.island.text,
                          self.cay.text,
                          self.pit.text,
                          self.coordinatesN.text,
                          self.coordinatesW.text,
                          self.sex.dataSource,
                          self.age.dataSource];
    NSString *docPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES )objectAtIndex:0];

    //resultView.text = docPath;}

    NSString *surveys=[docPath stringByAppendingPathComponent:@"results.csv"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:surveys]) {
       [[NSFileManager defaultManager] createFileAtPath:surveys contents:nil attributes:nil];
    }
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:surveys];
    [fileHandle seekToEndOfFile];
    [fileHandle writeData:[resultLine dataUsingEncoding:NSUTF8StringEncoding]];
    [fileHandle closeFile];
    self.species.text=@"";
    self.island.text=@"";
    self.cay.text=@"";
    self.pit.text=@"";
    self.coordinatesN.text=@"";
    self.coordinatesW.text=@"";
    NSLog(@"info saved");
}

- (IBAction)retrieveInfo:(id)sender {
    NSString *docPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES )objectAtIndex:0];
    //resultView.text = docPath;
    NSString *surveys=[docPath stringByAppendingPathComponent:@"results.csv"];

    if ([[NSFileManager defaultManager] fileExistsAtPath: surveys]) {
        NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:surveys];
        NSString *surveyResults = [[NSString alloc]initWithData:[fileHandle availableData] encoding:NSUTF8StringEncoding];
        [fileHandle closeFile];
        self.resultView.text = surveyResults;
    }


}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //Initialize picker data
    pickerDataSex = @[@"M",@"F",@"M/F"];
    pickerDataAge = @[@"Adu",@"Juv",@"Hat"];

    //Connect data to picker
    self.sex.dataSource = self;
    self.sex.delegate = self;

    self.age.dataSource = self;
    self.age.delegate = self;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//Number of columns of the data
- (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView
{
    if (pickerView==self.sex){
        return 1;
    } else if (pickerView==self.age){
       return 1;
    }

    return 0;
}

//Number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView==self.sex){
        return pickerDataSex.count;
    } else if (pickerView==self.age){
        return pickerDataAge.count;
    }
    return 0;
}

//The data to return for the row and component (column) that's being passed
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView==self.sex){
        return pickerDataSex[row];
    } else if (pickerView==self.age){
        return pickerDataAge[row];
    }
    return 0;
}

@end

You can get values from your UIPickerViews that way : 您可以通过以下方式从UIPickerViews中获取值:

NSInteger selectedRowSex = [self.sex selectedRowInComponent:0];
NSInteger selectedRowAge = [self.age selectedRowInComponent:0];

NSString *selectedSex = [pickerDataSex objectAtIndex:selectedRowSex];
NSString *selectedAge = [pickerDataAge objectAtIndex:selectedRowAge];

Then 然后

NSString *resultLine = [NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@,%@,%@,%@\n",
                      self.day.date,
                      self.species.text,
                      self.island.text,
                      self.cay.text,
                      self.pit.text,
                      self.coordinatesN.text,
                      self.coordinatesW.text,
                      selectedSex,
                      selectedAge];

You have code that populates a your picker view, but have not shown any code that gets the selected value from the picker view. 您具有填充选择器视图的代码,但未显示任何从选择器视图获取选定值的代码。

The easiest way to handle that is to add code in your saveInfo method that calls selectedRowInComponent on your picker views to get the current value. 处理该问题的最简单方法是在您的saveInfo方法中添加代码,该方法在您的选择器视图上调用selectedRowInComponent来获取当前值。

Alternately you can implement the pickerView:didSelectRow:inComponent delegate method and read the newly selected picker value each time it changes. 或者,您可以实现pickerView:didSelectRow:inComponent委托方法,并在每次更改时读取新选择的选择器值。

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

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