簡體   English   中英

字符串Xcode Objective-C

[英]Strings Xcode Objective-C

我是Objective-C和Xcode的新手。

我做了第一個程序“ Hello Word”,現在我想將“ Hello Word”消息更改為另一條消息。 這是我的代碼中的示例:

。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 ([label.text isEqualToString:@"hello World"]) {
     label.text = @"here is the second string";
} else {
   label.text = @"hello World";
}

請注意,測試NSString是否等效於isEqualToString: 更通用的形式是isEqual:也可以,但是如果您知道要處理NSString對象,則認為前者效率更高。

如果那不符合您的要求,那么您可以使用邏輯-例如

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;
}

或使用@HotLicks建議的整數標志。.有很多方法可以使用該邏輯,但沒有一種方法是特定於Objective-C的。

您可以通過創建NSMutableArray來做到這一點。 更好的方法是有一個按鈕和一個標簽。 當您按下按鈕時,標簽會顯示您想要的文本。

這是最好的方法:

.h文件

@interface ViewController : UIViewController

{

int currentTextIndex;

// The model objects

NSMutableArray *text;

// The view objects

IBOutlet UILabel *labelName;

}

- (IBAction)showText:(id)sender;

.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];


}

讓我知道這個是否奏效? 可能會有點過

這個答案是解決您問題的另一種方法。 下一步是插入文本字段,標簽和按鈕,因此當您按下按鈕時,標簽將隨文本字段的內容而變化。

將這些項目添加到視圖中,在視圖的頭文件(iE ViewController.h)中將“文本字段和標簽”聲明為“出口”,將“按鈕”聲明為“動作”。 然后在您的實現文件(ViewController.m)中:

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

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

請注意,一旦鍵盤顯示出來,您可能會長大“問題列表”。 我的意思是,一旦顯示,您需要隱藏它(也許)並在按下鍵盤上的“返回”鍵時執行操作,因為起初它什么都不做。

以下是實現“ Return”鍵的一些代碼:

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

在此答案中,您應該已經學會:-如何設置標簽的文本值-如何調用函數-如何為“返回”鍵設置操作

而已。 編碼愉快!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM