可可-从笔尖加载视图并将其显示在NSView容器中,作为子视图 [英] Cocoa - loading a view from a nib and displaying it in a NSView container , as a subview

查看:40
本文介绍了可可-从笔尖加载视图并将其显示在NSView容器中,作为子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前已经问过这个问题,但是问题本身以及其中的所有信息可能有点令人困惑,而且我想要获得的结果也更加复杂.因此,我启动了一个新的干净测试项目,以处理我目前暂时不希望了解的部分.

I've asked about this earlier but the question itself and all the information in it might have been a little confusing, plus the result i want to get is a little more complicated. So i started a new clean test project to handle just the part that im interested to understand for the moment.

所以我想要的基本上是这样的:我有一个视图容器(继承NSView).在内部,我想放置一些图像,但不仅要放置简单的NSImage或NSImageView,还要放置一些自定义视图(也继承NSView),该视图本身包含一个文本字段和一个NSImageView.我称之为图像持有人"的文件位于单独的nib文件中(之所以使用这种方法,是因为我在Apple SAmple Application,COCOA SLIDES之后进行自我指导).到目前为止,我得到的结果是一些东西,但不是我所期望的.我以为我一定在Interface Builder中做错了事(没有连接适当的东西),但是我希望有更多专业知识的人能够启发我.

So what i want, is basically this: i have a view container (inherits NSView). Inside, i want to place some images, but not just simple NSImage or NSImageView, but some custom view (inherits NSView also), which itself contains a textfield and an NSImageView. This 'image holder' as i called it, is in a separate nib file (im using this approach since i am guiding myself after an Apple SAmple Application, COCOA SLIDES). The results i got so far, is something but not what i am expecting. Im thinking i must be doing something wrong in the Interface Builder (not connecting the proper thingies), but i hope someone with more expertise will be able to enlighten me.

下面,我将尝试放置到目前为止的所有代码:

Below i'll try to put all the code that i have so far:

//ImagesContainer.h
#import <Cocoa/Cocoa.h>


@interface ImagesContainer : NSView {

}
@end
//ImagesContainer.m
#import "ImagesContainer.h"
#import "ImageHolderView.h"
#import "ImageHolderNode.h"
@class ImageHolderView;
@class ImageHolderNode;
@implementation ImagesContainer

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
  //create some subviews
  for(int i=0;i<3;i++){
   ImageHolderNode *node = [[ImageHolderNode alloc] init];
   [self addSubview:[node rootView]];
  }
    }
 NSRunAlertPanel(@"subviews", [NSString stringWithFormat:@"%d",[[self subviews] count]], @"OK", NULL, NULL);
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    // Drawing code here.

 [[NSColor blackColor] set];
 NSRectFill(NSMakeRect(0,0,dirtyRect.size.width,dirtyRect.size.height));
 int i=1;
 for(NSView *subview in [self subviews]){
  [subview setFrameOrigin:NSMakePoint(10*i, 10)];
  i++;
 }
}

@end

//ImageHolderView.h
#import <Cocoa/Cocoa.h>


@interface ImageHolderView : NSView {
 IBOutlet NSImageView *imageView;
}
@end
//ImageHolderVIew.m
#import "ImageHolderView.h"


@implementation ImageHolderView
- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    // Drawing code here.
 [[NSColor blueColor]set];
 NSRectFill(NSMakeRect(10,10, 100, 100));
 //[super drawRect:dirtyRect];
}
@end

//ImageHolderNode.h
#import <Cocoa/Cocoa.h>
@class ImageHolderView;

@interface ImageHolderNode : NSObject {
 IBOutlet ImageHolderView *rootView;
 IBOutlet NSImageView *imageView;
}
-(NSView *)rootView;
-(void)loadUIFromNib;
@end

//ImageHolderNode.m
#import "ImageHolderNode.h"


@implementation ImageHolderNode

-(void)loadUIFromNib {
 [NSBundle loadNibNamed:@"ImageHolder" owner: self];
}
-(NSView *)rootView {
 if( rootView == nil) {
  NSRunAlertPanel(@"Loading nib", @"...", @"OK", NULL, NULL);
  [ self loadUIFromNib];
 }
 return rootView;
}

@end

我的笔尖文件是:

  1. MainMenu.xib
  2. ImageHolder.xib

  1. MainMenu.xib
  2. ImageHolder.xib

MainMenu是我启动新项目时生成的xib.

MainMenu is the xib that is generated when i started the new project.

我将尽力在xib ImageHolder中提及到目前为止的连接:

I'll try to mention the connections so far in the xib ImageHolder :

文件的所有者-具有ImageHolderNode类ImageHolder.xib的主视图具有ImageHolderView类

File's Owner - has class of ImageHolderNode The main view of the ImageHolder.xib , has the class ImageHolderView

因此,要恢复,我得到的结果是视图容器中的3个蓝色矩形,但我似乎无法使其显示从ImageHolder.xib加载的视图.

So to resume, the results im getting are 3 blue rectangles in the view container, but i cant seem to make it display the view loaded from the ImageHolder.xib

如果有人想看看CocoaSlides示例应用程序,请在Apple开发人员页面上查看(不幸的是,我不允许张贴多于1个链接:))

If anyone wants to have a look at the CocoaSlides sample application , its on apple developer page ( im not allowed unfortunately to post more than 1 links :) )

推荐答案

确切地说,这不是答案,因为不清楚您要问什么..

Not an answer, exactly, as it is unclear what you are asking..

您进行查看("ImagesContainer类").让我们称之为 imagesContainerView .

You make a view (class 'ImagesContainer'). Lets call it imagesContainerView.

ImagesContainerView生成3个对象(类"ImageHolderNode").ImagesContainerView向每个imageHolderNode询问其 -rootView (也许是"ImageHolderView"),并将返回值添加到其视图层次结构中.

ImagesContainerView makes 3 Objects (class 'ImageHolderNode'). ImagesContainerView asks each imageHolderNode for it's -rootView (maybe 'ImageHolderView') and adds the return value to it's view-heirarchy.

ImagesContainerView丢弃(但泄漏)每个imageHolderNode.

ImagesContainerView throws away (but leaks) each imageHolderNode.

所以heirachy视图看起来像:-

+ imagesContainerView
    + imageHolderView1 or maybe nil
    + imageHolderView2 or maybe nil
    + imageHolderView3 or maybe nil

这是您所期望的吗?

那么您在哪里调用-(void)loadUIFromNib 并等待笔尖加载?
在某些代码中您没有显示?

So where do you call -(void)loadUIFromNib and wait for the nib to load?
In some code you are not showing?

通常,一次完成一个步骤,使每个步骤都起作用.

In general, progress a step at a time, get each step working.

NSAssert是您的朋友.尝试使用它,而不是将警报面板和日志误用于调试目的.IE.

NSAssert is your friend. Try it instead of mis-using alert panels and logging for debugging purposes. ie.

ImageHolderNode *node = [[[ImageHolderNode alloc] init] autorelease];
NSAssert([node rootView], @"Eek! RootView is nil.");
[self addSubview:[node rootView]];

当然,应该画点东西.TextViews绘制文本,而ImageViews绘制图像.如果您需要绘制可可提供的文字,图像,表格等以外的其他内容,则应该将NSView子类化.

A view of course, should draw something. TextViews draw text and ImageViews draw images. You should subclass NSView if you need to draw something other than text, images, tables, etc. that Cocoa provides.

如果需要从多个笔尖组合视图,则应按照应用程序的要求在笔尖中排列视图,或者使用viewController或windowController排列视图.那就是他们的目的.

You should arrange your views as your app requires in the nib or using a viewController or a windowController if you need to assemble views from multiple nibs. Thats what they are for.

Interface Builder连接

Interface Builder Connections

如果RootView不为零,则似乎您已正确连接了连接,但是您说不清楚..

If RootView isn't nil then it seems like you have hooked up your connections correctly, but you say you are unclear so..

确保将IB窗口设置为列表视图",以便可以清楚地看到笔尖的内容.
文件的所有者"代表将要装载笔尖的对象,对吗?您的情况是ImageHolderNode.
控制单击File的所有者,除其他外,您可以查看其出口.当笔尖由ImageHolderNode加载时,控制从列表中的拖动(在列表视图中)到要设置为实例var的对象.我知道您已经知道了,但是没有别的了.

Make sure the IB window is set to List view so you can see the contents of you nib clearly.
'File's Owner' represents the object that is going to load the nib, right? In your case ImageHolderNode.
Control Click on File's owner and amongst other things you can see it's outlets. Control drag (in the list view) from an outlet to the object you want to be set as the instance var when the nib is loaded by ImageHolderNode. I know you know this already, but there is nothing else to it.

您期望看到什么?空的imageView?好吧,那看起来什么都没有.空的文本框?那也将看起来像什么都没有.将出口连接到textfield和imageView并在其上设置一些内容.

What exactly are you expecting to see ? An empty imageView? Well, that will look like nothing. An empty textfield? That too, will look like nothing. Hook up an outlet to your textfield and imageView and set some content on them.

这篇关于可可-从笔尖加载视图并将其显示在NSView容器中,作为子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆