从ViewController访问View

时间:2013-09-01 20:35:10

标签: objective-c view uiviewcontroller

我在StoryBoard中创建了一个ViewController,并将其与GamePanelViewController类相关联。 此外,我将ViewController的视图与名为GamePanel的第二个类链接起来,后者扩展了UIView。

如何访问由StoryBoard自动创建的视图,以执行我为GamePanel类实现的一些方法,这些方法通过身份检查器链接到视图?

GamePanel.h:

#import <UIKit/UIKit.h>
#import "Plattform.h"

@interface GamePanel : UIView

@property (strong, nonatomic) NSMutableArray *plattforms;

- (void)initGamePanel;
- (void)drawPlattforms:(CGContextRef)gc;

@end

GamePanel.m:

#import "GamePanel.h"

@implementation GamePanel

@synthesize plattforms;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}

- (void)initGamePanel{
   self.backgroundColor = [UIColor redColor];

    self.plattforms = [NSMutableArray new];
    // Initialization code
    for(int i = 0;i<8;i++){
        for(int j = 0;j<6;j++){
            [self.plattforms addObject:[[Plattform alloc] initWithXCord:(14+j*37) andYCoord:(58+14+i*37)]];
            NSLog(@"Init");
        }
    }
}

1 个答案:

答案 0 :(得分:3)

如果您实际上已将viewController的view属性与自定义视图相关联,那么您只需在viewController中使用self.view即可访问它。

如果你刚刚将一个UIView拖到你的storyboard中的viewController上(并没有将它链接到viewController的view属性),你需要为它创建一个IBOutlet(从视图控制拖动到你的界面,并给出它是一个名称),然后你可以用你给它的名称来引用它,例如self.myView

相关问题