gpt4 book ai didi

ios - 使用下一个和上一个UIButton切换UITextView/UILabel中的不同文本

转载 作者:行者123 更新时间:2023-12-01 17:55:31 25 4
gpt4 key购买 nike

假设我有5条文字。我想使用UITextView或UILabel来显示它。我有一个下一个和上一个按钮,以帮助我循环浏览。解决此问题的最佳方法是什么?

 NSString *text1 = @"Hello World 1"
NSString *text2 = @"Hello World 2"
NSString *text3 = @"Hello World 3"
NSString *text4 = @"Hello World 4"
NSString *text5 = @"Hello World 5"

最佳答案

这个解决方案可能很好

在.h文件中

UIButton *nextButton;
UIButton *backButton;
UILabel *textLabel;
NSArray *textStr;
int counter;

在.m文件中
- (void)viewDidLoad

{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

counter=0;

textStr = [[NSArray alloc] initWithObjects:@"Today is rainy", @"Today is sunnt", @"Today is bright", @"Today is gloomy",
@"Today is beautifyl", nil];

textLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 30, 200, 200)];
textLabel.text= [NSString stringWithFormat:@"%@", [textStr objectAtIndex:0]];
[self.view addSubview:textLabel];

nextButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[nextButton addTarget:self
action:@selector(btnClicked:)
forControlEvents:UIControlEventTouchDown];
nextButton.tag=1;
[nextButton setTitle:@"Next" forState:UIControlStateNormal];
nextButton.frame = CGRectMake(120.0, 150, 80, 40.0);
[self.view addSubview:nextButton];

backButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[backButton addTarget:self
action:@selector(btnClicked:)
forControlEvents:UIControlEventTouchDown];
backButton.tag=2;
[backButton setTitle:@"Previous" forState:UIControlStateNormal];
backButton.frame = CGRectMake(30.0, 150, 80.0, 40.0);
[self.view addSubview:backButton];

}
-(void)btnClicked:(UIButton*)btn{



if (btn.tag==1) {
NSLog(@"%i", [textStr count]);
if (counter<[textStr count]-1) {
counter++;
NSLog(@"%i", counter);

textLabel.text= [NSString stringWithFormat:@"%@", [textStr objectAtIndex:counter]];
}
}
else{
if (counter>1) {
counter--;
textLabel.text= [NSString stringWithFormat:@"%@", [textStr objectAtIndex:counter]];
}

}

}

关于ios - 使用下一个和上一个UIButton切换UITextView/UILabel中的不同文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18452996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com