gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:03:15 25 4
gpt4 key购买 nike

我正在编写一个小型 iOS 应用程序,希望它能为我的实地工作提供便利。我在 OS X El Capitan 10.11.4 上,使用 Xcode 7.3 并为 iOS 9.3 编写应用程序。

这是整个想法:我不想将数据插入一张纸,而是想将所有内容写入一个类似基本形式的应用程序,以便在 iPad 上使用,然后让该应用程序将数据传输到.csv 文件。

我几乎想通了所有事情(应用程序的结构、传输和检索数据的协议(protocol)等),但有一件事让我发疯。在我向您展示我正在使用的代码之前,这里是问题所在。当我单击“保存”按钮并稍后检查保存的数据时,我无法将 UIPickerView 对象选择的值传输到 .csv 文件。其他一切正常。我有两个 UIPickerView 对象。在一个中,我有性别信息,编码为 M、F 和 M/F;另一个有关于年龄编码为 Adu、Juv、Hat 的信息。我想要的是让应用程序保存所选的值,但我不太明白。

非常感谢任何帮助。谢谢。

这是我用于我的 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

这是我用于 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

最佳答案

您可以通过这种方式从 UIPickerViews 获取值:

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

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

然后

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];

关于ios - 如何将 UIPickerView 对象的选择导出到 CSV 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36754600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com