0

I'm new to iOS.

I'm adding views to the controller using the code below.

@interface ViewController : UIViewController <CollapseClickDelegate,UITextFieldDelegate> {

    IBOutlet UIView *test1view;
    IBOutlet UIView *test2view;

    __weak IBOutlet CollapseClick *myCollapseClick;
}

I need to create 50 different instances of the view located below. How can I do this? I looked into subviews but as I said at the top I'm a newbie and couldn't figure out what was going on.

-Name:
-Amount:
-Percent:

2
  • If you're using a view 50 times are you sure you want to be using IBOutlets for each one? You can create a custom UIView that you insert on the page programmatically. Commented Jul 8, 2014 at 17:12
  • I actually wanted to make 50 programmatically. How would I do that? I'm currently using a plugin called CollapseClick and I only know how to add views through the code located above. I'll research custom classes now. Thanks for the input. Commented Jul 8, 2014 at 17:22

2 Answers 2

1

To create your views programatically, use viewDidLoad: of your UIViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    for(int i=0;i<50;i++){
        //provide some initial frame, set the correct frames in viewWillLayoutSubviews: 
        CGRect frame = CGRectMake(0,i*10;100;5);
        //create a new UIView, use your own UIView subclass here if you have one
        UIView *view = [[UIView alloc] initWithFrame:frame];
        //set it's backgroundColor in case you are copy&pasting this code to try it out, so you see that there are actually views added ;)
        view.backgroundColor = [UIColor blueColor];
        //add it to the viewController's view
        [self.view addSubview:view];

        //you might consider creating an NSArray to store references to the views for easier access in other parts of the code
    }
}

To create a view that you designed in a storyboard use something like described here: https://stackoverflow.com/a/13390131/3659846

MyViewController *myViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyScene"]; 
[self.view addSubView:myViewController.theViewToAdd];  

To create a view from a nib file, use the approach described here: https://stackoverflow.com/a/11836614/3659846

NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"yourNib" owner:nil options:nil];
UIView *view = [nibContents lastObject]; //assuming the nib contains only one view
[self.view addSubview:view];
Sign up to request clarification or add additional context in comments.

Comments

0
for (int i = 0; i < 50; i++) {
    UIView *someView = [UIView new];
    // this positions each view one under another with height 40px and width 320px like a table
    [someView setFrame:CGRectMake(0,40*i,320,40)];
    // set other view properties 
    // put a UILabel on the view
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10,200,20)];
    [view addSubview:label];
    // set label properties
    [self.view addSubview:someView]
}

Note the frame is relative to its parent view coordinate system and you need to add the view to its parent view by calling addSubview: method. -good luck

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.