iPhone – iOS get location

I have many buttons in UIScrollView, and I am trying to get their position on button click. This is my code:

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(2, 0, 262, 748)];
scrollView.contentSize=CGSizeMake(262, 816);
scrollView.bounces=NO ;
scrollView.delaysContentTouches=YES;

buton1 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
buton1.frame = CGRectMake(2, 0, 262, 102);
buton1.bounds = CGRectMake(2, 0, 262.0, 102.0);
[buton1 setTag:1];
[buton1 setImage:[UIImage imageNamed:@"left_gride.png"] forState :UIControlStateNormal];
[buton1 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:buton1];

and

< p>

-(void) buttonClick:(UIButton*)button{
if ([button tag]==1)
{
NSLog(@"position: %d",buton1.frame.origin.x);
}
else
{
NSLog(@"position:%d",buton2.frame.origin.y) ;
}

}

What’s wrong with my code. Thank you.

I also tried:

-(void) buttonClick:(UIButton*)button{
if ([button tag]==1)
{
NSLog(@"position:%d",buton1.frame.origin.x);
}
else
{
NSLog(@"position:%d",buton2.frame.origin.y);
}-(void) buttonClick:( UIButton*)button{if ([button tag]==1){ NSLog(@"position:%d",button.frame.origin.x);}else{ NSLog(@"position:%d",button2. frame.origin.y);}

But I get the same. Position 0;

Your code looks good. The only thing-you used the wrong format specifier in NSLog. Since the coordinates are floating point numbers, you should use %f instead of %d:

NSLog(@"position:%f",buton1.frame.origin.x);

I have many in UIScrollView Button, I tried to get their position on button click. This is my code:

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(2 , 0, 262, 748)];
scrollView.contentSize=CGSizeMake(262, 816);
scrollView.bounces=NO;
scrollView.delaysContentTouches=YES;

buton1 = [[UIButton buttonWithType:U IButtonTypeCustom] retain];
buton1.frame = CGRectMake(2, 0, 262, 102);
buton1.bounds = CGRectMake(2, 0, 262.0, 102.0);
[buton1 setTag :1];
[buton1 setImage:[UIImage imageNamed:@"left_gride.png"] forState:UIControlStateNormal];
[buton1 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:buton1];

and

-(void) buttonClick:(UIButton*)button{
if ([button tag]==1)
{
NSLog(@"position:%d",buton1.frame.origin.x);
}
else
{
NSLog(@"position:%d",buton2.frame.origin.y);
}

}

What’s in my code Question. Thank you.

I also tried:

-(void) buttonClick:(UIButton*)button{
if ([button tag]==1)
{
NSLog(@"position:%d",buton1.frame.origin.x);
}
else
{< br /> NSLog(@"position:%d",buton2.frame.origin.y);
}-(void) buttonClick:(UIButton*)button{if ([button tag]==1){ NSLog(@"position:%d",button. frame.origin.x);}else{ NSLog(@"position:%d",button2.frame.origin.y);}

But I get the same. Position 0;

Your code looks fine. The only thing-you are using the wrong format specifier in NSLog. Since the coordinates are floating point numbers, you should use %f instead %D:

NSLog(@"position:%f",buton1.frame.origin.x);

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1033 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.