how get values from subview(UILabel) of ScrollView in iphone
i added 10 labels(labels contains some data) to scrollview .i want to get the value of label at particular location .can u please get me know how to get the value at that location.please give suggestion for this is the code what i have written .how can i get the value of the user selected language
-(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;
NSMutableArray *languageArray=[[NSMutableArray alloc]initWithObjects:@"Chi开发者_StackOverflow中文版nese",@"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)];
}
Thank You,
girish
Somebody must have set those values, wasn't it you ? :) Why would you ask labels for their values when you already must know it to set it in the first place.
EDIT : seeing your code, you probably can figure out from the contentOffset
of the scroll view, but it seems that you'd be better off with a UIPicker
or a UITableView
where all this infrastructure is provided for free
UILabel is not interactive component Girish. So you cannot get the event of its selection. Better use UIButtons. I have edited your code and added one method. Just make the languageArray global and the code whould work fine.
-(void)youMethod
{
//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;
NSMutableArray *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 )];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, y ,90,30 );
btn.tag = languagValue;
[btn addTarget:self action:@selector(languageSelected:) forControlEvents:UIControlEventTouchUpInside];
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:btn];
// [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)];
}
-(void)languageSelected:(id)sender
{
UIButton * btn = (UIButton*)sender;
int selectedIndex = btn.tag;
NSString * selectedLanguage = [languageArray objectAtIndex:selectedIndex];
}
精彩评论