简体   繁体   English

字符串Xcode Objective-C

[英]Strings Xcode Objective-C

I'm new in Objective-C and Xcode. 我是Objective-C和Xcode的新手。

I did the first program, "Hello Word", and now I want to change "hello word" message for another message. 我做了第一个程序“ Hello Word”,现在我想将“ Hello Word”消息更改为另一条消息。 Here is the example in my code: 这是我的代码中的示例:

.h 。H

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

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

.m .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: . 请注意,测试NSString是否等效于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. 更通用的形式是isEqual:也可以,但是如果您知道要处理NSString对象,则认为前者效率更高。

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. 或使用@HotLicks建议的整数标志。.有很多方法可以使用该逻辑,但没有一种方法是特定于Objective-C的。

A way you could do this is by creating an NSMutableArray. 您可以通过创建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 .h文件

@interface ViewController : UIViewController

{

int currentTextIndex;

// The model objects

NSMutableArray *text;

// The view objects

IBOutlet UILabel *labelName;

}

- (IBAction)showText:(id)sender;

.m file .m文件

@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). 将这些项目添加到视图中,在视图的头文件(iE ViewController.h)中将“文本字段和标签”声明为“出口”,将“按钮”声明为“动作”。 Then in your implementation file (ViewController.m): 然后在您的实现文件(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”键的一些代码:

//"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! 编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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