我正在寫一個小型的iOS應用程序,我希望能夠方便我的現場工作。我在OS X El Capitan 10.11.4上,使用Xcode 7.3並編寫iOS 9.3的應用程序。如何將UIPickerView對象的選擇導出爲CSV文件?
下面是整個想法:不是將數據插入到一張紙中,而是想將所有內容寫入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];
[email protected]"";
[email protected]"";
[email protected]"";
[email protected]"";
[email protected]"";
[email protected]"";
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
感謝的人。這太棒了。我解決了這個問題。我正在研究使用代表,我無法弄清楚。現在它工作得很好。 – Cinghio