2

I have a Scroll view in My Viewcontroller. Also I had created a view and connected it to an IBOutlet variable. I need to add many views to the Scroll view.

Code for adding view to ScrollView :

In .h File

UIView *customView_;

@property (nonatomic, retain) IBOutlet UIView *customView;

In .m File :

@synthesize customView = customView_;

    for (int i = 0; i < 2; i++) {

        UIView *tempView = customView;
        tempView.frame = CGRectMake(i * 187, 0, 187, 133);
        [scrollView addSubview:tempView];
    }

But using the above code, I am only able to see one in the position : 187,0,187,133. If I change the code to for (int i = 0; i < 3; i++) { I am able to see the view only in 374,0,187,133. Can anyone please help me to Resolve this error?

jay
  • 3,517
  • 5
  • 26
  • 44

1 Answers1

5

Why is this happening?

Documentation for addSubview:

Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

With this code

UIView *tempView = customView;
tempView.frame = CGRectMake(i * 187, 0, 187, 133);
[scrollView addSubview:tempView];

You are trying to add the same view twice/three times. With the comment from the documentation, this will remove the previous one, and add it to the new position. (Whilst the superview is the receiver in your case, it still does not mean it'll copy your view and add it again).

Solution

Unfortunately, UIView does not follow the NSCopying protocol. As such, you can't create a direct copy of it using copy. You will need to create a factory method that will create your view for you, as so:

-(UIView)createCustomView {
    ...
}

Then call that in your for loop:

UIView *tempView = [self createCustomView];
tempView.frame = CGRectMake(i * 187, 0, 187, 133);
[scrollView addSubview:tempView];

This may outgrow your scope of the .xib file. Refer to this answer on how to get this to play nice with Interface Builder.

Community
  • 1
  • 1
WDUK
  • 18,870
  • 3
  • 64
  • 72
  • Now I am getting an exception : [UIView copyWithZone:]: unrecognized selector sent to instance – jay Nov 20 '12 at 10:53
  • Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView copyWithZone:]: unrecognized selector sent to instance – jay Nov 20 '12 at 10:53
  • Answer updated. UIView can't copy, this makes it a bit trickier I'm afraid. – WDUK Nov 20 '12 at 11:00
  • It will be helpful, if you can post the content in createCustomView. Is there any other solution to solve this issue while using XIB file? – jay Nov 20 '12 at 11:03
  • I don't know how you're creating `customView` in the first place. However you're creating it, you need to be able to have it produce multiple copies of it. Because of the `IBOutlet` keyword in the property declaration, I believe you're creating it via Interface Builder. As such, I provided a link to an answer on how to duplicate UIViews from .xibs. – WDUK Nov 20 '12 at 11:06