Programmatically creating Views in IOS (how does i

2019-01-21 17:34发布

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.

8条回答
Explosion°爆炸
2楼-- · 2019-01-21 18:03

Did you set the background color of your view to something distinguishable?

Are you sure your view doesn't display, or is it just that you can't see it? Put an NSLog in the drawRect: method of your view (if you use a custom view class).

查看更多
别忘想泡老子
3楼-- · 2019-01-21 18:04

Somewhere in your active controller class, you can add a custom view to the current top level view.

[ myCurrentTopView addSubview: myNewCustomView ];

It helps to allocate and initialize this custom view first.

Then, you can trigger some drawing in this custom view's drawRect by doing a:

[ myNewCustomView setNeedsDisplay ];
查看更多
姐就是有狂的资本
4楼-- · 2019-01-21 18:08

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!

查看更多
贪生不怕死
5楼-- · 2019-01-21 18:10

I had a very similar problem recently and here's what worked for me: It seems that when starting with an 'empty application' in Xcode 4.3.2 you have to set your view as the root view your main window using the setRootViewController method instead of addSubview. So:

// Initialize myView and then
[window setRootViewController:myView];
// instead of [window addSubview:myView];
// the rest as normal...
[window makeKeyAndVisible];

After that you can use the addSubview method to add additional views.

查看更多
smile是对你的礼貌
6楼-- · 2019-01-21 18:19

call

[super drawRect];

before your custom code in drawRect: body.

查看更多
SAY GOODBYE
7楼-- · 2019-01-21 18:24

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.

查看更多
登录 后发表回答