3

I'm trying to understand how to make a duplicate of a uiview that has a set of uibuttons inside it.

Been trying to follow this question/answer but I'm really confused atm:

Make a deep copy of a UIView and all its subviews

Basically trying to make a vc that displays two sets of uiviews with buttons. This is what the regular view will look like:

Points of team 1:
   +  +  +  +
   1  2  3  P
   -  -  -  


Points of team 2:
   +  +  +  +
   1  2  3  P
   -  -  -  

And I need to make a copy of it. I can probably just drag objects onto the viewcontroller but it'll have way too many IBactions for it if I create another copy.

Thoughts on how to deal with this?

EDIT: This is how I solved adding multiple buttons

Add a multiple buttons to a view programmatically, call the same method, determine which button it was

Community
  • 1
  • 1
gdubs
  • 2,724
  • 9
  • 55
  • 102

1 Answers1

3

First I would create a UIView subclass called PointsView or something.

This will look like this...

Points of [name label]:
   +  +  +  +
   1  2  3  P
   -  -  -  

It will have properties like NSString *teamName and set those properties up against the relevant labels.

It may also have properties for NSUInteger score so you can set the score value of the PointView object.

This is all completely separate from your UIViewController.

Now, in your UIViewController subclass you can do something like...

PointsView *view1 = [[PointsView alloc] initWithFrame:view1Frame];
view1.teamName = @"Team 1";
view1.score1 = 1;
view1.score2 = 2;
view1.score3 = 3;
[self.view addSubView:view1];

PointsView *view2 = [[PointsView alloc] initWithFrame:view2Frame];
view2.teamName = @"Team 2";
view2.score1 = 1;
view2.score2 = 2;
view2.score3 = 3;
[self.view addSubView:view2];

Now there is not copying involved. You just create two instances of an object.

EDIT

Creating your view subclass...

The easiest way to create your view subclass is to do the following...

Create the files... PointsView.m and PointsView.h

The .h file will look something like this...

#import <UIKit/UIKit.h>

@interface PointsView : UIView

@property (nonatomic, strong) UILabel *teamNameLabel;
// other properties go here...

@end

The .m will look like this...

#import "PointsView.h"

@implementation PointsView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.teamNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 21)];
        self.teamNameLabel.backgroundColor = [UIColor clearColor];
        [self addSubView:self.teamNameLabel];

        // set up other UI elements here...
    }
    return self;
}

@end

Then in your view controller you add the PointsView to it IN CODE (i.e. not with Interface builder) like this...

- (void)viewDidLoad
{
    [super viewDidLoad];

    PointsView *pointsView1 = [[PointsView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
    pointsView1.teamNameLabel.text = @"Team 1";
    [self.view addSubView:pointsView1];

    // add the second one here...
}

You can also create and add these views in Interface Builder but it's a lot harder to explain on here.

If you set it up this way then you can use IB for setting up the rest of your UIViewController. Just don't use IB to set up the PointsViews. It won't work with the way I've shown here.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • I see, I think that was what I was missing. So do I have to drag an object into the VC and assign it to the subclass I created? Or do I just create an instance and assign a position to it? – gdubs May 16 '13 at 15:52
  • awesome, ill try these out as soon as i get home in a bit. Also, just so you know I'm using storyboards. Not sure if that's the same as IB. lol, pretty new with this. – gdubs May 16 '13 at 17:20
  • It worked! I have an issue tho. Those symbols "+" and "-" , those are supposed to be buttons where the user taps and adds scores "score 1", "score 2", etc. I'm not really sure how to handle those buttons, that was one of the main problems I have when thinking about implementing this. Thoughts? Also, thank you for the help! – gdubs May 17 '13 at 03:51
  • I was thinking, I should create a UiButton subclass as I tried adding a UIButton property, it seems like I have to multiply the button properties by 8 and assign them methods. – gdubs May 17 '13 at 03:57
  • You should create subclassed of uibutton. They are really complicated as it is a composite class. Stay away from uibutton subclasses. – Fogmeister May 17 '13 at 06:54
  • Yes, storyboards are made using Interface Builder. What is it about the + - buttons you're not site about? – Fogmeister May 17 '13 at 06:56
  • It's working now! I basically created instance of buttons, (+ and -). And just dynamically added it and have it connected to a selector that checks the tag of the button. Thanks a lot for the help! – gdubs May 17 '13 at 13:29
  • No worries. I see what you were trying to do now. You could do this a different way by having the buttons call a method INSIDE the PointsView class and then having a delegate method in the controller or something but whatever works is great :D – Fogmeister May 17 '13 at 13:32
  • Well, I was trying to do a scoring for teams and those are the points I'm going to use, makes it easier to add scores and there's a label that adds all of them. Now my problem is how do I get the value of the total score from the viewcontroller. the value is stored at the PointsView. Is that when delegate comes in? – gdubs May 17 '13 at 14:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30136/discussion-between-fogmeister-and-gdubs) – Fogmeister May 17 '13 at 14:36