如何使用next&amp ;;在UITextView中显示不同的文本片段以前在ios的UIButton

时间:2015-02-23 07:15:36

标签: ios objective-c iphone xcode

我们说我有3条文字。我想使用UITextView来显示它。我有一个下一个和上一个按钮,以帮助我循环它。什么是解决这个问题的最佳方法?我正在尝试一些代码..但它不适合我.. 实际上我需要显示如下: 如果我点击下一个按钮我需要显示如下。第1页和第3页第3页和第3页第3页。如果我点击后退按钮我需要显示如下。第3页,共3页第2页,共3页和第3页1 of 3。

-(void)btnClicked:(UIButton*)btn

{

int totalcount=3;

if(slokaText.hidden==NO)

{

if (btn.tag==1)

{

NSLog(@"%lu", (unsigned long)[textStr count]);

if (counter<[textStr count]-1) {

counter++;

NSLog(@"%i", counter);

descriptionLbl1.text= [NSString stringWithFormat:@"%@", [textStr objectAtIndex:counter]];

NSLog(@"counter is :%d",counter);

countLabel.text=[NSString stringWithFormat:@"page %d of %d",counter,totalcount];

}

}

else

{

if (counter>1)

{

counter--;

descriptionLbl1.text= [NSString stringWithFormat:@"%@", [textStr objectAtIndex:counter]];

NSLog(@"counter is :%d",counter);

countLabel.text=[NSString stringWithFormat:@"page %d of %d",counter,totalcount];

}

}        

}

else if (slokaMeaning.hidden==NO)

{

if (btn.tag==1)

{

NSLog(@"%lu", (unsigned long)[textStr1 count]);

if (counter<[textStr1 count]-1) {

counter++;

NSLog(@"%i", counter);

descriptionLbl2.text= [NSString stringWithFormat:@"%@", [textStr1 objectAtIndex:counter]];

NSLog(@"counter is :%d",counter);

countLabel.text=[NSString stringWithFormat:@"page %d of %d",counter,totalcount];

}

}

else

{

if (counter>1)

{

counter--;

descriptionLbl2.text= [NSString stringWithFormat:@"%@", [textStr1 objectAtIndex:counter]];

NSLog(@"counter is :%d",counter);

countLabel.text=[NSString stringWithFormat:@"page %d of %d",counter,totalcount];

}

}

}

else if (meaning.hidden==NO)

{

if (btn.tag==1) 

{

NSLog(@"%lu", (unsigned long)[textStr2 count]);

if (counter<[textStr2 count]-1)

{

counter++;

NSLog(@"%i", counter);

descriptionLbl3.text= [NSString stringWithFormat:@"%@", [textStr2 objectAtIndex:counter]];

NSLog(@"counter is :%d",counter);

countLabel.text=[NSString stringWithFormat:@"page %d of %d",counter,totalcount];

}

}

else

{

if (counter>1)

{            

counter--;

descriptionLbl3.text= [NSString stringWithFormat:@"%@", [textStr2 objectAtIndex:counter]];

NSLog(@"counter is :%d",counter);

countLabel.text=[NSString stringWithFormat:@"page %d of %d",counter,totalcount];

}

}


}

}

1 个答案:

答案 0 :(得分:0)

你可以这样做..

假设currentPageNumtotalPageNumyourTextView被声明为全局变量

- (IBAction)yourButtonAction:(UIButton *)sender
{
    if (sender.tag == 1) // assumed as back action
    {
        if (currentPageNum > 0)
        {
            currentPageNum -- ;
        }
        else
        {
            currentPageNum = totalPageNum; // add this line if you want to cycle
        }
    }
    else // assumed as next action
    {
        if (currentPageNum < totalPageNum)
        {
            currentPageNum ++;
        }
        else
        {
            currentPageNum = 1; // add this line if you want to cycle

        }
    }

    yourTextView.text = [NSString stringWithFormat:@"Page %li of %li",(long)currentPageNum,(long)totalPageNum];
}