这个解决方案可能很好
在 .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]];
}
}
}