1

i am trying to get x,y coordinates and height,width of an imageview of the view controller in another class..but i failed ...please tell me where did i made mistake..?

this is my code of view controller and class that i given to the view controller

view controller.m

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. dot1=[[UIImageView alloc]initWithFrame:CGRectMake(145, 30, 20, 20)]; dot1.backgroundColor=[UIColor blackColor]; dot1.layer.cornerRadius=11.0; dot2=[[UIImageView alloc]initWithFrame:CGRectMake(80, 130, 20, 20)]; dot2.backgroundColor=[UIColor blackColor]; dot2.layer.cornerRadius=11.0; NSLog(@" (x==%f,y==%f,w==%f,h==%f)",dot1.frame.origin.x,dot1.frame.origin.y,dot1.frame.size.width,dot1.frame.size.height);//here i got the correct output.. } 

and this code belongs to the class view.m....

 - (void)drawRect:(CGRect)rect { ViewController*vc=[[ViewController alloc]init]; NSLog(@"(x==%f,y==%f,w==%f,h==%f)",vc.dot1.frame.origin.x,vc.dot1.frame.origin.y,vc.dot1.frame.size.width,vc.dot1.frame.size.height);//but here i got all points 0. } 

note: i gives class to the view controller is view.

2 Answers 2

1

All points of imageView frame will be 0 as you are allocating the imageView dot1 and dot2 inside - (void)viewDidLoad which won't be called on alloc and init. So if you want to get the imageView's proper frame you need to set it in init method which is the only method you are calling from your drawRect: function.

Sign up to request clarification or add additional context in comments.

Comments

0

Have a cgrect and pass the frame

CGRect requiredRect;

-(void)initWithRect:(CGRect) rectLocal

 { requiredRect = rectLocal; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.