How do I get text from text file into UIScrollView?

Discussion in 'iOS Programming' started by learningCentres, Feb 25, 2010.

  1. learningCentres macrumors newbie

    Joined:
    Feb 25, 2010
    #1
    How can I get text from my .txt file into my UIScrollView?

    At the moment I have this which does not work, it compiles just fine but will not show any text.

    Code:
    -(void)loadText
    {
    	NSBundle *bundle = [NSBundle mainBundle];
    	NSString *textFilePath = [bundle pathForResource:@"mathsquestions" ofType:@"txt"];
    	NSString *fileContents = [NSString stringWithContentsOfFile:textFilePath];
    	NSArray *mathsText = [[NSArray alloc] initWithArray:[fileContents componentsSeparatedByString:@" "]];
    	self.mathsPracticeTextArray = mathsText;
    	[mathsText release];
    	
    }
    Code:
    - (void)viewDidLoad {
    	
        [super viewDidLoad];
    	
    		
    	myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    	
    	
    	myScrollView.contentSize = CGSizeMake(320, 960);
    	
    		myScrollView.pagingEnabled = FALSE;
    	
    
    	myScrollView.scrollEnabled = TRUE;
    	
    	
    	myScrollView.backgroundColor = [UIColor whiteColor];
    	
    	
    	[self.view addSubview:myScrollView];
    	UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,100,960,40)];
    	myLabel.text = [mathsPracticeTextArray componentsJoinedByString:@" "];
    	[myScrollView addSubview:myLabel];
    	[myLabel release];
    	
    }
    
    Thanks so much in advance
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Which method seems to be the problem: loadText or viewDidLoad? And you're sure loadText is being called before viewDidLoad?
     

Share This Page