0

I'm new in Objective-C and Xcode.

I did the first program, "Hello Word", and now I want to change "hello word" message for another message. Here is the example in my code:

.h

#import <UIKit/UIKit.h>    
@interface ViewController : UIViewController {

 IBOutlet UILabel * label;
 IBOutlet UIButton * boton;
 }
 -(IBAction)click:(id)sender;
 @end

.m

 #import "ViewController.h"

 @interface ViewController ()

 @end

 @implementation ViewController

 -(IBAction)click:(id)sender{
     label.text = @"hello Word" ;
     label.text = @"here is the second string"; 
      // i would like when i touch again the button to change to this string
 }

3 Answers 3

1

If you want to toggle between the two you could try a conditional

if ([label.text isEqualToString:@"hello World"]) {
     label.text = @"here is the second string";
} else {
   label.text = @"hello World";
}

Note the test for NSString equivalence isEqualToString:. The more general form isEqual: would also work, but the former is considered to be more efficient if you know you are dealing with NSString objects.

If that is not quite what you are after, then you can play with the logic - for example

NSString* firstString = @"hello world";
NSString* secondString = @"here is the second string";

if ([label.text isEqualToString:firstString] 
{
     label.text = secondString;
} else if ([label.text isEqualToString:secondString] {
   return;
} else {
  label.text = firstString;
}

Or use an integer flag as @HotLicks suggests.. there are many ways to play with the logic, none of which is specific to objective-C.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer, but what i want to do is : First a press the bottom and the label change to hello world, then i press again the same botton and it changes to here is the second string.. I only want to pass sentences
@juanCu, your comment is unfinished.
You can add an integer property that you increment with each button push, then use that integer to select which message to display. (And if any of what I just said doesn't make sense you need to study up a bit before proceeding.)
0

A way you could do this is by creating an NSMutableArray. A better way to do this would be to have a button and a label. When you press the button the label shows the text you want it to.

Here is the best way you could do it:

.h file

@interface ViewController : UIViewController

{

int currentTextIndex;

// The model objects

NSMutableArray *text;

// The view objects

IBOutlet UILabel *labelName;

}

- (IBAction)showText:(id)sender;

.m file

@interface ViewController ()

@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    // Call the init method implemented by the superclass
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
     text = [[NSMutableArray alloc] init];

// Add text to the arrays
    [text addObject:@"Whatever text you want in here];

    [text addObject:@"other text you want in here"];

}

return self;

- (IBAction)showText:(id)sender
{
    //Step to the next statement
    currentQuestionsIndex++;

    //Am I past the last statement?
    if (currentTextIndex == [text count])
    {

        //Go back to the first statement
        currentTextIndex = 0;
    }
//Get the string at that index in the text array
    NSString *statement = [text objectAtIndex:currentTextIndex];

    //Log the string to the console
    NSLog(@"displaying text: %@", text);

    //Display the string in the question field
    [labelName setText:question];


}

let me know if this works? might be a little off

Comments

0

This answer is a different approach to your question. The next step would be inserting a Text Field, a label and a button, so when you press the button the label will change with the Text Field contents.

Add those items to your view, declare Text Field and Label as Outlets, the Button as Action in the view's header file (i.E. ViewController.h). Then in your implementation file (ViewController.m):

 -(IBAction)myButton:(id)sender{
   [self updateLabel];
 }

-(void)updateLabel{
   _myLabel.text = _myTextField.text;
}

Note once the keyboard shows up, you probably will grow up your "questions list". I mean, once it shows, you need to hide it (maybe) and perform and action when pressing the "return" key from the keyboard as at first it does nothing.

Here is a bit of code to implement the "Return" key:

//"Return" key
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
   [_myTextField resignFirstResponder]; //This takes out the "focus" from the textfield
   [self updateLabel];
   return YES;
}

Within this answer you should have learned: - How to set the text value for a label - How to call a function - How to set an action for the "return" key

That's it. Happy coding!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.