我是Objective-c的新手所以请耐心等待.我有一个类从网络摄像头返回图片,我试图将其显示在屏幕上.我将子类NSViewController从相机类中获取并将其设置为实例NSImageView,并将其NSViewController视图设置为NSImageView.我在Interface Builder中创建了一个自定义视图,将一个NSViewController对象拖到MainMenu.xib中,将它的类设置为PhotoGrabberController,并从自定义视图中单击拖动控件到PhotoGrabberController,将其出口绑定设置为视图.(我真的不知道幕后是怎么回事,对我来说似乎很神奇).没有任何东西出现在屏幕上,我一直在玩这个.
在PhotoGrabberController.h中
@interface PhotoGrabberController : NSViewController {
PhotoGrabber * grabber;
NSImageView* iView;
}
@property (nonatomic, retain) NSImageView* iView;
Run Code Online (Sandbox Code Playgroud)
在PhotoGrabberController.m中
@implementation PhotoGrabberController
@synthesize iView,grabber;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void) awakeFromNib
{
self = [super init];
if (self) {
NSRect rect = NSMakeRect(10, 10, 200, 100);
iView = [[NSImageView alloc] initWithFrame:rect];
[iView setImageScaling:NSScaleToFit];
[iView setImage:[NSImage imageNamed:@"picture.png"]];
grabber = …Run Code Online (Sandbox Code Playgroud) 我希望有一个子类对象的集合,但我的泛型类型实现,因为它现在给出一个错误,allItems.add(item);因为allItems不保持Item类型.那么如何更改以下代码而不是出错呢?
public class ItemManager {
public static Collection<? extends Item> allItems;
...
public static boolean addItem(Item item){
return allItems.add(item);
}
}
Run Code Online (Sandbox Code Playgroud)
可能会添加一个新项目:
itemManager.add(new Bomb());
Run Code Online (Sandbox Code Playgroud)
有没有办法addItem改为:
public static boolean addItem([all subclasses of Item] item) { ... }
Run Code Online (Sandbox Code Playgroud)
或者可能改变allItems所以它可以接受接收Item和子类Item?