這種解決方案可能是在.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]];
}
}
}
嘗試任何事情 –
我嘗試使用多個UIbuttons,隱藏和顯示它們。不是一種非常有效的做事方式。 –