Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8927565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:14:48+00:00 2026-06-15T08:14:48+00:00

I have a array that reads a .txt file and when you click a

  • 0

I have a array that reads a .txt file and when you click a button the label changes in one of the words of the .txt file, but the label doen’t change.

This is the code:

if(sender == self.button) {
    NSArray *words = [[NSArray alloc] initWithObjects:@"words.txt", nil];
    [randomLabel setText:[words objectAtIndex:random() % [words count]]];
}

What should I do so the label changes when I press the button?
What file do I use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:14:49+00:00Added an answer on June 15, 2026 at 8:14 am

    A few things here:

    Reading in file into an array

    Well, for starters you’re not reading in the contents of the .txt file.

    NSArray *words = [[NSArray alloc] initWithObjects:@"words.txt", nil];
    

    This creates a 1 element array, with that one element being @"words.txt". I don’t know the format of your .txt file, so I can’t say for sure how you have to load it in. See How do I format a text file to be read in using arrayWithContentsOfFile on how to potentially do this.

    Setting button text

    Also, you need to make sure randomLabel actually refers to the label contained within the button, otherwise the button text won’t change. Typically for a button, you’d change the title using the method:

    - (void)setTitle:(NSString *)title forState:(UIControlState)state
    

    So in your instance, it’d be:

    NSString* newTitle = [words objectAtIndex:random() % [words count]];
    [self.button setTitle:newTitle forState:UIControlStateNormal];
    

    Is the code actually being called?

    Double check that sender == self.button evaluates to true (for readability and clarity, I’d use [sender isEqual:self.button]). Use the debugger to step through the code, to see if that particular piece of code is being called. See http://mobile.tutsplus.com/tutorials/iphone/xcode-debugging_iphone-sdk/ on how to achieve this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C program that reads from a txt file. The text file
Hi I have made a program that reads a text file containing words and
I have a php file that reads in information from a txt file and
I have a program that reads from a txt file line by line splits
i have a very simple script that reads out a txt file, puts the
I have a txt file that has a change-log.I'm trying to display the new
I originally have an array that reads $array1 = array(a,a,b,b) I run it through
I have one array that is filled by mysql_query. Values that are in this
So I have a method that reads a file and assigns classes to elements
I have a problem. I created a application which reads a txt file and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.