简体   繁体   中英

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

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

You can add [view1 addSubView:view2]; .if you added the UIScrollView on UIViewController add that controller view to other view on which you want to add.

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. You might need to explicitly call removeFromSuperview before adding it to another view. 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..

Further in class B create a instance of class A in B .h file eg: ViewController *VC1; and synthesize it in B's .m file

Now..in ViewController A when you are calling ViewController B (Typically when you are pushing) 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 ,

write the following in Class B

   [ self.view addSubView:VC1.scrollView];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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