xcode 在“BSViewController *”类型的对象上找不到属性“window”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14420693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Property 'window' not found on object of type 'BSViewController *'
提问by zerowords
I am trying to make a UIButton display an image and am getting the Title error.
我正在尝试让 UIButton 显示图像,但出现标题错误。
Also I would like to know why ()
is needed on BSViewController ()
as it was created by XCode?
另外我想知道为什么()
需要BSViewController ()
它,因为它是由 XCode 创建的?
//
// BSViewController.m
#import "BSViewController.h"
@interface BSViewController () // Why the "()"?
@end
@implementation BSViewController
- (IBAction) chooseImage:(id) sender{
UIImageView* testCard = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ipad 7D.JPG"]];
//Property 'window' not found on object of type 'BSViewController *'
self.window.rootViewController = testCard;
[self.window.rootViewController addSubview: testCard];
testCard.center = self.window.rootViewController.center;
NSLog(@"chooseImage");
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// BSViewController.h
#import <UIKit/UIKit.h>
@class BSViewController;
@interface BSViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
IBOutlet UIButton* chooseImage;
}
- (IBAction) chooseImage:(id) sender;
@end
回答by foundry
This line:
这一行:
self.window.rootViewController = testCard;
Is an attempt to assign an imageView object pointer to an existing viewController object pointer. You should have got a compiler warning about that. Then on the next line you effectively try to add it to itself as a subview, that probably kicks up a warning as well.
尝试将 imageView 对象指针分配给现有的 viewController 对象指针。您应该收到有关此的编译器警告。然后在下一行,您有效地尝试将其作为子视图添加到自身中,这也可能会引发警告。
The () indicate a category extensionto a class. It's an extension to your public interface which allows you to declare entities that should be private to the class. You should put most of your interface here, just keep things in your .h @interface that need to be public.
() 表示类的类别扩展。它是公共接口的扩展,它允许您声明应该是类私有的实体。您应该将大部分界面放在此处,只需将需要公开的内容保存在 .h @interface 中。
Your BSViewController class does not have a property called window
so you cannot refer to it as self.window
. But under normal circumstances you should be able to get a reference to your window like this:
您的 BSViewController 类没有被调用的属性,window
因此您不能将其称为self.window
. 但在正常情况下,您应该能够像这样获得对您的窗口的引用:
UIWindow* window = [[UIApplication sharedApplication] keyWindow];
[window.rootViewController.view addSubview: testCard];
However, if you just want to put testCard into your instance of BSViewController you don't need to do that. You just need a reference to your current instance's view:
但是,如果您只想将 testCard 放入您的 BSViewController 实例中,则不需要这样做。您只需要引用当前实例的视图:
[self.view addSubview:testCard];