[英]How to export the selection of a UIPickerView object to a CSV file?
我正在編寫一個小型iOS應用程序,希望對我的現場工作有所幫助。 我在OS X El Capitan 10.11.4上,使用Xcode 7.3並為iOS 9.3編寫應用程序。
這是整個想法:我不想將數據插入紙中,而是將所有內容都寫到了要在iPad上使用的基本表單形式的應用程序中,然后讓該應用程序將數據傳輸到.csv文件中。
我已經弄清了幾乎所有東西(應用程序的結構,傳輸和檢索數據的協議等),但是有一件事情使我發瘋。 在向您展示我正在使用的代碼之前,這是問題所在。 當我單擊“保存”按鈕,然后再去檢查已保存的數據時,我無法獲得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];
您具有填充選擇器視圖的代碼,但未顯示任何從選擇器視圖獲取選定值的代碼。
處理該問題的最簡單方法是在您的saveInfo方法中添加代碼,該方法在您的選擇器視圖上調用selectedRowInComponent
來獲取當前值。
或者,您可以實現pickerView:didSelectRow:inComponent
委托方法,並在每次更改時讀取新選擇的選擇器值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.