2011-08-17 39 views
-1

我是iPhone的新手,我正在创建一个iPhone应用程序,我在其中使用简单的文本文件作为我的数据库。
在firstView我有四个UIButtons。我有四个不同的文本文件。
我想要的是当我选择button1时,然后从file1.txt中的数据将加载相同,因为当我选择button2,button3和button4时,将分别加载来自file2,file3,file4的数据。想要通过点击特定按钮从选定的文件数据

事情是这样的:

if(categoryVC.Quiz1btn isSelected == YES){ 
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; 
} 

,但是这是行不通的。

任何帮助将不胜感激。

谢谢。

回答

0

你可以做的就是在你的第二个的viewController如下在最前一页视图控制器加一个(的NSString *)文件名attribut所选文件按钮,通过名称:

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

}

等一个...

而且在您添加以下代码的第二视图控制器的viewDidLoad功能:

- (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; 
} 
+0

谢谢Lokus001!我会尝试这个..希望它的作品.. !! :))) – iUser 2011-08-17 11:41:22

0

在我看来最好是在控制器(IBAction)上创建四个函数,然后在界面生成器中与视图链接。 的功能可以是这样的:

  • (IBAction为)selectFile1Action:(ID)发送方{ 一个NSBundle *束= [一个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];(IBAction)selectFile2Action:(id)sender { } 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];(IBAction);}}}}} 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”]]; 自我。theQuiz = quizArray; (IBAction)selectFile4Action:(id)sender {Bundle = [NSBundle mainBundle];(id)发件人{0} {0} {0}}} 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

但按钮是在第一个视图,我想在第二个视图中的数据。 那么有可能? – iUser 2011-08-17 11:01:48

相关问题