[英]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.