2016-04-20 41 views
0

我正在寫一個小型的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 

回答

1

您可以從您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]; 
+0

感謝的人。這太棒了。我解決了這個問題。我正在研究使用代表,我無法弄清楚。現在它工作得很好。 – Cinghio

0

你有一些代碼填充你的選擇器視圖,但沒有顯示任何代碼從選取器視圖中獲取選定的值。

處理該問題的最簡單方法是在saveInfo方法中添加代碼,該代碼在您的選擇器視圖上調用selectedRowInComponent以獲取當前值。

或者,您可以實施pickerView:didSelectRow:inComponent委託方法,並在每次更改時讀取新選定的選取器值。