user2301717
user2301717

Reputation: 1169

Change Button Label

I am new to iOS app development. currently I am working on a very simple application that displays two words on the screen and the user selects one and the app just outputs you have selected x.

I have created two plists and i have loaded them onto an array and randomised them in the array. On my view I have two buttons that i want the words to display on them. However, I am having two problems.. 1 . I want the label on the buttons to change when the app starts. So the user doesn't have to tap on the button or anything. 2. I want one random word from array_1 (loaded from a plist) to appear on a random button. either button_1 or button_2 and the same for list array_2.

Here is what i have done so far. (with some help from forums (: )

- (IBAction)buttonpressed:(id)sender {

    // Load contents of plist onto array

    NSString *path = [[NSBundle mainBundle] pathForResource:@"wordsOne" ofType:@"plist"];
    NSMutableArray *words = [NSMutableArray arrayWithContentsOfFile:path];

    //shuffle them using Fisher–Yates shuffle algorithm

    for (int i = words.count-1; i>=0; i--) {
        int r = arc4random_uniform(words.count);
        [words exchangeObjectAtIndex:i withObjectAtIndex:r];
    }

    // assign word to the button
    for (int j =0; j<_WordButton.count; j++) {
        UIButton *button = [_WordButton objectAtIndex:j];
        button.titleLabel.text = [words objectAtIndex:j];
    }

}

There are obviously flaws in the above code, for example it uses only one plist and it doesn't display words on both buttons randomly.

Upvotes: 0

Views: 2537

Answers (3)

Matt
Matt

Reputation: 879

To achieve what you are looking to do you have to create an IBOutlet to the buttons on your .xib file. You can do this by using an easy Xcode shortcut that will create the IBOutlet code and make the connection from the .xib file to the code simultaneously for you:

  1. Open up the .xib file in Xcode which will show a graphic file using Interface Builder.
  2. In the toolbar at the top-right of Xcode is an icon that allows you to toggle through different modes, select the 'Assistant Editor'. The Interface Builder will slide over to share the window with a code file, this should automatically select your ViewController.h
    The toolbar should look like this:
    enter image description here

  3. Insert some curly-brace in your code like so:

`

@interface CustomViewController : UIViewController {
// This is where you will control-drag to from the Interface Builder to have it create an IBOutlet to the button for you
}

// Your already set-up IBAction Method
- (IBAction)buttonpressed:(id)sender
@end

`

  1. Control-drag from the button you wish your label to change to the area between the curly-braces in the .h file:
    enter image description here

  2. A new dialog box will appear. Make sure that the settings are as follow:
    a. Connection = IBOutlet
    b. Name = (Whatever you want to call the button, you've used '_wordButton')
    c. Type = UIButton
    d. Storage = WEAK

enter image description here

  1. Now in your action method - (IBAction)buttonpressed:(id)sender and in your application:didFinishLaunchingWithOptions (to automatically load a word into the label when the application is launched) you can set the button title with the following:

`

// Create a string from the word that you pull out of the plist file
NSString *labelWord = ???;

// Set the label of the button
[_wordLabel setTitle:word forState:UIControlStateNormal];

`

Upvotes: 2

Maurice
Maurice

Reputation: 1464

Do it like this:

-(void)viewDidLoad{
[self setnewtitle];

}

-(void)setnewtitle{
NSString *pathone = [[NSBundle mainBundle] pathForResource:@"wordsOne" ofType:@"plist"];
    NSMutableArray *wordsOne = [NSMutableArray arrayWithContentsOfFile:pathone];
NSString *pathtwo = [[NSBundle mainBundle] pathForResource:@"wordsOne" ofType:@"plist"];
    NSMutableArray *wordsTwo = [NSMutableArray arrayWithContentsOfFile:pathtwo];
int words = "the number of objects(words) in your plist";
int randombutton = arc4random() % 2;
int randomplist = arc4random() % 2;
int randomWord = arc4random() % words;

if (randombutton==0){
if (randomplist==0){
[[_WordButton objectatIndex:randombutton] setTitle:[wordsOne objectatIndex:randomWord]];
} else {
[[_WordButton objectatIndex:randombutton] setTitle:[wordsTwo objectatIndex:randomWord]];

}
}else {
if (randomplist==0){
[[_WordButton objectatIndex:randombutton] setTitle:[wordsOne objectatIndex:randomWord]];
} else {
[[_WordButton objectatIndex:randombutton] setTitle:[wordsTwo objectatIndex:randomWord]];

}
}



}

Please make sure that the in the Array contained Buttons are initialized and allocated, also if you have any questions please be free to ask

Upvotes: 0

user1525369
user1525369

Reputation:

You can set UIButton Title by using following code:

[button setTitle:titleString forState:UIControlStateNormal];

Upvotes: 4

Related Questions