简体   繁体   English

如何使用标签解决UIscrollview中的问题

[英]How to solve the problem in UIscrollview with label

i am doing a small concept on UIscrollView . 我在UIscrollView上做一个小概念。 i attached 10 labels to the scrollview(languages)and i want to retrieve the label value from the label what the user have selected.this code is executing and running .but some time it is terminating with out any exception/error. 我在滚动视图(语言)上附加了10个标签,并且我想从标签中检索用户选择的标签值。此代码正在执行和运行。但是有时它会终止而没有任何异常/错误。

Note:i am having doubt one that is when the first time the screen appears .if i press the scrollview up button .The label in the scroll view is moving to last label.if i press the down button than it is moving first label.it is happening only for first time after that it is moving for one up/down 注意:我怀疑是第一次出现屏幕时。如果我按下向上滚动按钮。滚动视图中的标签移动到最后一个标签。如果我按下向下按钮而不是移动第一标签。它仅在第一次之后才向上/向下移动

can any one help in solving this problem... 谁能帮助解决这个问题...

    -(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];


    }
}

You should better use UITableView for this, with its delegate methods you can easily display your language array. 为此,您最好使用UITableView,它的委托方法可以轻松显示语言数组。 and for geting the selected language,this delegate method will be used. 为了获得所选语言,将使用此委托方法。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM