简体   繁体   中英

Strings Xcode Objective-C

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
 }

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.

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

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 (iE 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM