在"BSViewController*"类型的对象上找不到属性"窗口"

zer*_*rds 1 xcode ios

我试图使UIButton显示图像,并得到标题错误.

另外我想知道为什么()需要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
Run Code Online (Sandbox Code Playgroud)

fou*_*dry 11

这一行:

    self.window.rootViewController = testCard;
Run Code Online (Sandbox Code Playgroud)

是尝试将imageView对象指针指定给现有的viewController对象指针.你应该有一个编译器警告.然后在下一行,您有效地尝试将其添加到自身作为子视图,这可能也会引发警告.

()表示类的类别扩展.它是您的公共接口的扩展,允许您声明应该属于该类的私有实体.你应该把你的大部分界面都放在这里,只需把你的.h @interface中的内容保存在公共场所.

您的BSViewController类没有调用的属性,window因此您无法将其称为self.window.但在正常情况下,您应该能够像这样获得对窗口的引用:

    UIWindow* window = [[UIApplication sharedApplication] keyWindow];
    [window.rootViewController.view addSubview: testCard];
Run Code Online (Sandbox Code Playgroud)

但是,如果您只想将testCard放入BSViewController的实例中,则无需执行此操作.您只需要引用当前实例的视图:

    [self.view addSubview:testCard];
Run Code Online (Sandbox Code Playgroud)