开发者

Want data from selected file by tapping particular button

开发者 https://www.devze.com 2023-03-28 12:03 出处:网络
I am new to iPhone and i am creating an iPhone app in which i am using simple text file as my database.

I am new to iPhone and i am creating an iPhone app in which i am using simple text file as my database.

In firstView i have four UIButtons. I have four different text file.

What i want is when i select button1 then data from file1.txt will be load same as when i select button2 , button3 and button4 , data from file2 ,file3 ,file4 will be load respectively.

Something like this :

if(categoryVC.Quiz1btn isSelected == YES){
NSBundle *bundle = [NSBundle mainBundle];
NSString *textFilePath = [bundle pathForResource:@"file1" ofType:@"txt"];
NSString *fileContents = [NSString stringWithContentsOfFile:textFilePath encoding:NSUTF8StringEncoding error:n开发者_运维问答il];
[NSString stringWithContentsOfFile:textFilePath encoding:NSUTF8StringEncoding error:nil];
NSArray *quizArray = [[NSArray alloc] initWithArray:[fileContents componentsSeparatedByString:@"\n"]];
self.theQuiz = quizArray;
}

but this is not working.

Any Help will be appreciated.

Thank you.


What you can do is to add an (NSString*) filename attribut in your second viewController and pass the name of the selected file button as following in the fisrt view controller :

-(IBAction)selectFile1Action:(id)sender {
SecondViewController *scView = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
scView.filename = @"file1";
[[self navigationController] pushViewController:scView animated:YES];
[scView release];

}

and so one ...

And in viewDidLoad function of the second view controller you add the following code :

- (void)viewDidLoad {

[super viewDidLoad];
NSBundle *bundle = [NSBundle mainBundle]; 
NSString *textFilePath = [bundle pathForResource:filename ofType:@"txt"]; 
NSString *fileContents = [NSString stringWithContentsOfFile:textFilePath encoding:NSUTF8StringEncoding error:nil]; 
[NSString stringWithContentsOfFile:textFilePath encoding:NSUTF8StringEncoding error:nil]; 
NSArray *quizArray = [[NSArray alloc] initWithArray:[fileContents componentsSeparatedByString:@"\n"]]; 
self.theQuiz = quizArray;
}


In my opinion it's better to create four functions on your Controller (IBAction) and link then with your view in Interface Builder. The functions can be like that :

  • (IBAction)selectFile1Action:(id)sender { NSBundle *bundle = [NSBundle mainBundle]; NSString *textFilePath = [bundle pathForResource:@"file1" ofType:@"txt"]; NSString *fileContents = [NSString stringWithContentsOfFile:textFilePath encoding:NSUTF8StringEncoding error:nil]; [NSString stringWithContentsOfFile:textFilePath encoding:NSUTF8StringEncoding error:nil]; NSArray *quizArray = [[NSArray alloc] initWithArray:[fileContents componentsSeparatedByString:@"\n"]]; self.theQuiz = quizArray; }

  • (IBAction)selectFile2Action:(id)sender { NSBundle *bundle = [NSBundle mainBundle]; NSString *textFilePath = [bundle pathForResource:@"file2" ofType:@"txt"]; NSString *fileContents = [NSString stringWithContentsOfFile:textFilePath encoding:NSUTF8StringEncoding error:nil]; [NSString stringWithContentsOfFile:textFilePath encoding:NSUTF8StringEncoding error:nil]; NSArray *quizArray = [[NSArray alloc] initWithArray:[fileContents componentsSeparatedByString:@"\n"]]; self.theQuiz = quizArray; }

  • (IBAction)selectFile3Action:(id)sender { NSBundle *bundle = [NSBundle mainBundle]; NSString *textFilePath = [bundle pathForResource:@"file3" ofType:@"txt"]; NSString *fileContents = [NSString stringWithContentsOfFile:textFilePath encoding:NSUTF8StringEncoding error:nil]; [NSString stringWithContentsOfFile:textFilePath encoding:NSUTF8StringEncoding error:nil]; NSArray *quizArray = [[NSArray alloc] initWithArray:[fileContents componentsSeparatedByString:@"\n"]]; self.theQuiz = quizArray; }

  • (IBAction)selectFile4Action:(id)sender { NSBundle *bundle = [NSBundle mainBundle]; NSString *textFilePath = [bundle pathForResource:@"file4" ofType:@"txt"]; NSString *fileContents = [NSString stringWithContentsOfFile:textFilePath encoding:NSUTF8StringEncoding error:nil]; [NSString stringWithContentsOfFile:textFilePath encoding:NSUTF8StringEncoding error:nil]; NSArray *quizArray = [[NSArray alloc] initWithArray:[fileContents componentsSeparatedByString:@"\n"]]; self.theQuiz = quizArray; }

0

精彩评论

暂无评论...
验证码 换一张
取 消