如何使用label解决UIscrollview中的问题

时间:2011-03-18 05:40:40

标签: iphone uiscrollview uilabel

我在UIscrollView上做了一个小概念。我将10个标签附加到scrollview(语言),我想从标签中检索用户所选择的标签值。这段代码正在执行并运行。但有时它会终止而没有任何异常/错误。

注意:我怀疑是第一次出现屏幕时的情况。如果我按下向上滚动查看按钮。滚动视图中的标签将移动到最后一个标签。如果我按下向下按钮而不是它正在移动第一个label.it只是第一次发生之后,它正在向上/向下移动

任何人都可以帮助解决这个问题...

    -(void)printLanguage
    {
    NSLog(@"in print language method");

    //int y=0;
    //NSMutableArray *languageArray=[[NSMutableArray alloc]initWithObjects:@"Chinese",@"Spanish",@"English",@"Arabic",@"Hindi",@"Bengali",@"Portuguese",@"Russian",@"Japanese",@"German",nil];
    //UILabel *languageLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, y ,90,30 )];

    languagValue=0;

    int y=0;
    languageArray=[[NSMutableArray alloc]initWithObjects:@"Chinese",@"Spanish",@"English",@"Arabic",@"Hindi",@"Bengali",@"Portuguese",@"Russian",@"Japanese",@"German",nil]; 


    for(languagValue=0;languagValue<[languageArray count];languagValue++)
    {
        UILabel *languageLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, y ,90,30 )];
        NSLog(@"array count is @%d",[languageArray count]);
        languageLabel.text=[languageArray objectAtIndex:languagValue];
        NSLog(@"array objectat index is @%@",[languageArray objectAtIndex:languagValue]);
        languageLabel.font=[UIFont systemFontOfSize:19.0];
        languageLabel.backgroundColor=[UIColor clearColor];
        [languageScrollView addSubview:languageLabel];
        //  [languageScrollView addSubview:languageLabel];
        //y+=90;        
        y+=languageLabel.frame.size.height;
        [languageLabel release];

    }   
                            [languageScrollView setShowsHorizontalScrollIndicator:NO];
                            [languageScrollView setShowsVerticalScrollIndicator:NO];

                            [languageScrollView setContentSize:CGSizeMake(genderScrollView.frame.size.width, y)];   
         }


    -(IBAction)languageDownButton:(id)sender
    {
    NSLog(@"language scroll view  up button pressed");

    languagValue-=1;

    NSLog(@"value of language value is @%d",languagValue);
    if(!(languagValue>0))
    {
        languagValue=0; 

    }
    else if(!(languagValue<10))
    {
        languagValue=9;
    }
    else {
        NSLog(@"gender value*24 is @%d",(languagValue)*30);

         [languageScrollView setContentOffset:CGPointMake(0, (languagValue)*30) animated:YES];


    }       
}


    -(IBAction)languageUpButton:(id)sender
    {

    NSLog(@"language scroll view down button pressed");
        NSLog(@"value of language value is @%d",languagValue);
    languagValue+=1;

    if(!(languagValue>0))
    {
        languagValue=0; 

    }
    else if(!(languagValue<10))
    {
        languagValue=9;
    }
    else {
        NSLog(@"gender value*24 is @%d",(languagValue)*30);

        [languageScrollView setContentOffset:CGPointMake(0, (languagValue)*30)   animated:YES];


    }
}

1 个答案:

答案 0 :(得分:0)

您最好使用UITableView,使用其委托方法可以轻松显示语言数组。 并且为了选择所选语言,将使用此委托方法。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

请参阅文档。 http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html