2013-01-20 57 views
1

我想让UIButton显示一个图像,并且正在获取标题错误。在'BSViewController *'类型的对象上找不到属性“窗口”

另外我想知道为什么()需要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 
+0

当我进入这个'@财产(非原子,保留)的UIWindow *窗口;'我得到的错误'不兼容的指针类型从的UIImageView分配到的UIViewController * __strong '。 – zerowords

回答

10

这条线:

self.window.rootViewController = testCard; 

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

该()表示类别扩展到一个类。它是您的公共接口的扩展,它允许您声明应该对该类是私有的实体。你应该把你的大部分界面放在这里,只要把你的界面放在需要公开的界面中即可。

您的BSViewController类没有名为window的属性,因此您不能将其称为self.window。但在正常情况下,你应该能够得到这样你的窗口的引用:

UIWindow* window = [[UIApplication sharedApplication] keyWindow]; 
    [window.rootViewController.view addSubview: testCard]; 

不过,如果你只想把testCard到您BSViewController的情况下,你不需要做。你只需要你当前实例的观点的引用:

[self.view addSubview:testCard]; 
+0

看起来我想知道“为什么我会得到这个结果”,但我也需要知道如何解决它。 – zerowords

+0

首先删除我提到的那一行...然后 - 您是否打算将图像添加为您的BSViewController实例或另一个控制器的子视图?控制器之间的关系是什么? – foundry

+0

现在我想让图像在同一个viewController中。最终我想用UIImagePickerController来拍照,以便我可以识别52张卡片中的每张卡片。 – zerowords

相关问题