简体   繁体   English

如何在另一个类中添加类子视图作为iPhone中的子视图?

[英]How to add a class subview, in another class as a subview in iPhone?

我有两个UIViewController类,在第一个类中,我有一个UIScrollView作为子视图,并且我想将此UIScrollView添加到另一个类中作为子视图

You can add [view1 addSubView:view2]; 您可以添加[view1 addSubView:view2]; .if you added the UIScrollView on UIViewController add that controller view to other view on which you want to add. .if如果在UIViewController添加了UIScrollView ,则将该控制器视图添加到要添加的其他视图。

I don't think this will work properly, once you added a UIView to one UIView I don't think it's proper to add it to another subview. 我认为一旦将UIView添加到一个UIView ,这将无法正常工作,我认为将其添加到另一个子视图中是不合适的。 You might need to explicitly call removeFromSuperview before adding it to another view. 您可能需要先显式调用removeFromSuperview然后再将其添加到另一个视图中。 Ensure though that it's retained enough to be able to do that. 确保已保留足够的空间来执行此操作。

Make the UIScrollview as a property of viewController A .ie by assigning it @property(nonatomic,retain) in the .h file of class A and synthesizing it in .m file of class A.. 通过在类A的.h文件中为其分配viewController (nonatomic @property(nonatomic,retain)属性并将其合成在类A的.m文件中,使UIScrollview成为viewController A .ie的属性。

Further in class B create a instance of class A in B .h file eg: ViewController *VC1; 此外,在类B中,在B .h文件中创建类A的实例,例如: ViewController *VC1; and synthesize it in B's .m file 并将其合成到B的.m文件中

Now..in ViewController A when you are calling ViewController B (Typically when you are pushing) Eg : 现在..在ViewController A当您调用ViewController B (通常在按下时)Eg:

ViewControllerB *VC2 = [[ViewControllerB alloc]initWithNibName:@"ViewControllerB" bundle:nil];
VC2.VC1 = self;

[self navigationController pushViewCOntroller:VC2 animated:YES];

Now in Class B where you want the scrollView of Class A to be added , 现在在B类中,您要添加A类的scrollView

write the following in Class B 在B类中写下以下内容

   [ self.view addSubView:VC1.scrollView];

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

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