Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView with multiple UIViewControllers

Tags:

iphone

sdk

i have one scrollview and 4 UIviewcontrollers with xib files now i want to add 4 viewcontrollers to scrollview

and also the scroll is enabled for four viewcontrollers

any one know this plz answer this problem

like image 459
user564963 Avatar asked Jun 14 '26 19:06

user564963


2 Answers

Just add them. What's the problem?

// this loads a view controller from a nib
controller = [[UIViewController alloc] initWithNibName:@"YourNibsName" bundle:nil];

// this adds the viewcontroller's view to the scrollview
[scrollView addSubview:controller.view];

// place the subview somewhere in the scrollview
CGRect frame = controller.view.frame;
frame.origin.x = 600;
controller.view.frame = frame;

// don't forget to release the viewcontroller somewhere in your dealloc
[controller release];

Do this for all your four controllers.

like image 162
ySgPjx Avatar answered Jun 16 '26 07:06

ySgPjx


yes, you can just add the views of the viewcontrollers to your scrolling view, but remember that you are rolling your own equivalent to a UITabBarController or UINavigationController and so you have some responsibilities:

When you alloc init your vc from a nib the vc will get its viewDidLoad method called.

But when you put the vc.view into your scrollview YOU need to call [vc viewDidAppear:YES] (and also call viewWillAppear just before if your vc uses it).

Be careful with things like presenting modal view controllers from your vc as this may not work as you expect.

Peter

like image 29
Peter Avatar answered Jun 16 '26 08:06

Peter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!