我试图在另一个类中获取视图控制器的imageview的x,y坐标和高度,宽度..但是我失败了...请告诉我我在哪里弄错了..?
这是我给视图控制器提供的视图控制器和类的代码
查看控制器
- (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..
}
并且此代码属于类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.
}
注意:我给视图控制器的类是视图。
当您分配imageView时,imageView框架的所有点都将为0,dot1
并且dot2
在- (void)viewDidLoad
其中不会在alloc和init上调用。因此,如果要获取imageView的正确框架,则需要在init方法中设置它,这是您从drawRect:
函数中调用的唯一方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句