5

in carousel viewForItemAtIndex i am using reuse view something like this-

 -(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view{

if (!view){

   UIViewController * viewController = [self.storyboard  instantiateViewControllerWithIdentifier:@"PopUpView"];
   view = [viewController.view viewWithTag:1];
   CGRect  Frame = CGRectMake(view.frame.origin.x+300, view.frame.origin.y, view.frame.size.width+300, view.frame.size.height-350);
   view.frame = Frame;
}

Is it a good approach?

Saheb Singh
  • 545
  • 4
  • 17
  • 1
    You could also improve the performance by not allocating the viewcontroller everytime. For that you need to cache the view controller in a `NSMutableArray` and reuse it in the next invocation on same index. – Selvin Apr 21 '14 at 06:42

2 Answers2

5

I would say that this is not a good approach. You are creating a view controller here only to immediately throw it away, which is pointless.

If you just need the view, you can load it directly from a nib file without needing a view controller. You can bind it's actions to the main view controller for the carousel (there is an example of this in ControlsExample project included with the library), or create a custom view class and bind the subview outlets to the view itself.

If you want to use a view controller for each carousel item view (which I don't recommend, as this is not the convention used for UITableView or UICollectionView, which iCarousel is modelled on) then you should add the view controller as a child view controller of the main carousel controller, but this is fiddly as there is no obvious place where you can remove the child controller again when its view goes offscreen).

Nick Lockwood
  • 39,931
  • 11
  • 108
  • 100
1

As per approach, there is nothing wrong in using a view controller's view. UIView is where you handle what it looks like, UIViewController is the class where you handle events. If you want to handle any events then using UIViewController's View is a better option.

NKB
  • 649
  • 5
  • 11
  • But it is instantiating a ViewController within a viewController.Really it doesn't matter? – Saheb Singh Apr 21 '14 at 06:19
  • No, There is nothing wrong in instantiating a viewController inside a view controller but ensure that it is instantiated only once and reused there after avoiding a lot of memory usage. – NKB Apr 21 '14 at 06:22