1

UIButton に画像を表示させようとしていますが、タイトル エラーが発生しています。

また、XCode によって作成されたので、なぜ()必要なのか知りたいですか?BSViewController ()

//
//  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
4

1 に答える 1

12

この行:

    self.window.rootViewController = testCard;

imageView オブジェクト ポインターを既存の viewController オブジェクト ポインターに割り当てようとしています。それについてコンパイラの警告が表示されているはずです。次に、次の行で、サブビューとしてそれ自体に効果的に追加しようとしますが、おそらく警告も発生します。

() は、クラスのカテゴリ拡張を示します。これは、クラスに対してプライベートである必要があるエンティティを宣言できるようにする、パブリック インターフェイスの拡張です。インターフェイスのほとんどをここに配置する必要があります。パブリックにする必要があるものは .h @interface に保持してください。

BSViewController クラスには と呼ばれるプロパティwindowがないため、 として参照することはできませんself.window。ただし、通常の状況では、次のようにウィンドウへの参照を取得できるはずです。

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

ただし、testCard を BSViewController のインスタンスに入れたいだけの場合は、その必要はありません。現在のインスタンスのビューへの参照が必要なだけです。

    [self.view addSubview:testCard];
于 2013-01-20T01:22:17.620 に答える