Programmatically creating Views in IOS (how does it work)?

68,011

Solution 1

To be even more specific to your question, the syntax would be

UIWindow* window = [UIApplication sharedApplication].keyWindow;

UIView *polygonView = [[UIView alloc] initWithFrame: CGRectMake ( 0, 0, 200, 150)];
//add code to customize, e.g. polygonView.backgroundColor = [UIColor blackColor];

[window addSubview:polygonView];
[polygonView release];

This is a pattern you will use for not only this but subviews afterwards. Also, another note is with many of the templates, the viewController is already set up with it's own view. When you want to make a custom view, you create it like above but instead of the method above you set the viewControllers view to the newly created view like so:

viewController.view = polygonView;

Good luck!

Solution 2

This is an old question, but I'm not sure he got the answer he expected - it sounds like he ran into the same problem I did where I created a UIViewController subclass, whacked a UIViewController in an interface builder nib but then couldn't figure out how to get the two working with one another.

If you made the nib seperately (so it wasn't made for you alongside your class) then you have to remember to click the UIViewController you placed in the interface builder, and then from the properties panel click the third tab button ('Custom Class'). The Class will be the default base class UIViewController etc - just open up this dropdown list and you'll see a list of all your custom subclasses, select it and volla - you've linked your custom element to the interface.

Hope that saves somebody the days it took me to figure out, I found some stupidly wonderful ways of getting around it until I realized the simple solution.

Solution 3

hi just add this code,

[window bringSubviewToFront:polygonView];

right after,
 [window addSubview:polygonView];

Solution 4

You need to have a view (or window) to add a subview to. The normal syntax is like this:

UIView *newView = [[UIView alloc] initWithFrame:mainView.bounds];
[mainView addSubview:newView];
[newView release];

Of course, if you have a custom object that inherits from UIView, you would use that instead of UIView. When you start a new project, create a "View based application", that will start you off with a view controller with an associated view (which can be accessed with "CustomViewController.view", which would replace "mainView" in the code snippet above).

If you want to create the view programmatically when the app starts, put the code in the "- (void)viewDidLoad" method of your view controller.

Share:
68,011
subject_x
Author by

subject_x

Updated on April 17, 2020

Comments

  • subject_x
    subject_x about 4 years

    A little background: I'm going through the CS193P iTune videos and I was stuck on the assignment 3 for the longest time. Basically, the assignment asks you to programmatically create a custom view to display a shape on the screen. I'm not using any view controllers by the way.

    I could not get my view to display until I finally dragged a View object in Interface Builder and change the object name to my custom view class. So my question is when people say to programmatically create a view, are they just saying manually create the class but when you need to display it use IB? I can't help feeling like I misunderstood something?

    edit: let me be more clear. My custom view has been initialized with a frame of 0, 0, 200, 150 and drawRect is overriden to draw a square in it. My view doesn't even show up if try adding it to the main window within my controller:

        UIWindow* window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:polygonView];
    

    However, if use drag a view in IB and change the class to my view class, it shows up fine.

    Edit: Added some code. This is my controller's awakeFromNib method where the view should be drawn.

        - (void)awakeFromNib {
        shape = [[PolygonShape alloc] initWithNumberOfSides:numberOfSidesLable.text.integerValue minimumNumberOfSides:3 maximumNumberOfSides:12];
        polygonView = [[PolygonView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        polygonView.backgroundColor = [UIColor blackColor];
        [window addSubview:polygonView];
        [self updateInterface];  
    }
    

    Part of my controller's updateInterface method:

    - (void)updateInterface {
        [polygonView setPolygon:shape];
        [polygonView setNeedsDisplay];
    ...
    }
    

    PolygonView.h

    #import <UIKit/UIKit.h>
    #import "PolygonShape.h"
    
    @interface PolygonView : UIView {
        IBOutlet PolygonShape *polygon; 
    }
    
    @property (readwrite, assign) PolygonShape *polygon;
    
    - (void)drawRect:(CGRect)rect;
    @end
    

    PolygonView.m

    #import "PolygonView.h"
    
    @implementation PolygonView
    @synthesize polygon;
    
    - (id)initWithFrame:(CGRect)frame {
    
        self = [super initWithFrame:frame];
        if (self) {
            nslog(@"initialized");
        }
        return self;
    }
    
    - (void)drawRect:(CGRect)rect {
           CGRect bounds = [self bounds];
    
        [[UIColor grayColor] set];
        UIRectFill(bounds);
    
        CGRect square = CGRectMake(10, 10, 10, 100);
        [[UIColor blackColor] set];
        UIRectFill(square);
    
        [[UIColor redColor] set];
        UIRectFill(square);
        NSLog(@"drawRect called");
    }
    @end
    

    The polygonView is being initialized but the drawRect isn't being called.

  • subject_x
    subject_x over 13 years
    I know a View controller would be the way to go, but in this particular assignment there's only one model class, one controller class, and the custom view class. Are you saying I would have to add my view object to the main window? How and where would I do that?
  • subject_x
    subject_x over 13 years
    My current topview is the main window. The main window is not an object in my controller class, so how would I set my view as a subview ([window addSubView:...] doesn't work).
  • subject_x
    subject_x over 13 years
    I must be doing something wrong then. I have it exactly as you've wrote and my view isn't displaying.
  • subject_x
    subject_x over 13 years
    Yes, I tried setting my initial background color to black just to see if I can see a white or gray view show up, which didn't. I also put an NSLog message in drawRect and iniWithFrame which also didn't show in the console. I don't think my view is being initialized even though I have polygonView = [polygonView initWithFrame:CGRectMake(0, 10, 320, 480)]; in the awakeFromNib method of my controller.
  • subject_x
    subject_x over 13 years
    I take that back about the view not initializing. It is and the NSLog message in iniWithFrame is showing up. It's just the NSLog message in drawRect that isn't showing up. I can see that my data in my model is getting passed to my view correctly. I just can't figure why the window won't display my view.
  • fzwo
    fzwo over 13 years
    Maybe a little more code would help us analyze the situation then.
  • SushiGrass Jacob
    SushiGrass Jacob over 13 years
    Hmmm… What happens if you comment out drawRect or specifically call it in initWithFrame with [self drawRect];?
  • subject_x
    subject_x over 13 years
    Nothing different happens if I don't override drawRect. If I call drawRect from the initWithFrame method I get an exception error: "reason: '-[PolygonView drawRect]: unrecognized selector sent to instance 0x4b58c70'"