2012-01-08 10 views
0

私はそれがUIImageへのポインタである変数強いid変数をUIImage変数に転送するにはどうすればよいですか?

@property (strong, nonatomic) id dataObject; 

を持っています。私は新しい変数にそれを送りたい。私は私の新しい変数にそれを送信するにはどうすればよい unrecognized selector sent to instance 0x68423b0

@property (nonatomic, retain) UIImage *theImage; 

私はこの、このエラーでアプリがクラッシュをしようか?前もって感謝します。

contentViewController.h:

#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 
#import "pageAppViewController.h" 

@class pageAppViewController; 

@interface ViewController : UIViewController 

{ 
    NSArray *content; 
    UIImageView *theImageView; 
    UIImage *theImage; 
} 

@property (strong, nonatomic) IBOutlet UIImageView *theImageView; 
@property (strong, nonatomic) id dataObject; 
@property (nonatomic, retain) UIImage *theImage; 

@end 

contentViewController.m:あなたはidとして定義されたプロパティを持っている場合は

#import "ViewController.h" 
#import "pageAppViewController.h" 

@implementation ViewController 

@synthesize theImageView, dataObject; 
@synthesize theImage; 


- (void) viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    //this is the line that is causing the problems. 
    theImage = dataObject; 
    theImageView = [[UIImageView alloc] initWithImage: theImage]; 

    [self.view addSubview:theImageView]; 
} 
@end 
+0

これを試してください theImage =(UIImage *)dataObject; – IPaPa

+0

ありがとうございますが、動作しませんでした。 – ctw

答えて

0

理由だけではなく、あなたのインターフェイスで代わりにIDをUIImageに設定していない、なぜ(ID)?

@property (strong, nonatomic) UIImage *dataObject; 

NSData型の画像はありますか?

+0

私はそれを試したが、それでも動作しませんでした。 dataObjectは次のコードから導出されます。ブックと呼ばれるUIImagesの配列からのobjectAtIndexです。 - (ViewController *)viewControllerAtIndex:(NSUInteger)index { ViewController * dataViewController = [[ViewController alloc] initWithNibName:@ "ViewController"バンドル:nil]; dataViewController.dataObject = [self.book objectAtIndex:index]; return dataViewController; } – ctw

0

それは任意の種類のオブジェクトことができ、コンパイラに指示します。このオブジェクトを既知の型に割り当てたい場合は、idの背後にあるオブジェクトが同じ型であることを確信しているだけです。

id someObject = [UIImage imageNamed:@"x.png"]; 

UIImage *myImage = (UIImage*) someObject; 
+0

ありがとうございます。私はまだ1つの問題someObjectは、画像の配列から取られた画像へのポインタであり、ユーザが画像を変更すると変更されます。だから私はそれを一つの画像にすることはできない。 id someObjectをUIImageに設定する別の方法はありますか? – ctw

+0

これを知っているかわかりませんが、UIImageをNSArrayに保存することはできません。 NSDataとして保存する必要があります。 UIImageには、imageWithDataというメソッドがあります。これを使用してNSDataオブジェクトからイメージを取得できます。 –

+0

@HubertKunnemeyerそれは真実ではありません。 UIImagesをNSArrayに追加し、後で問題なく検索することができます。 NSArray内にUIImageを格納することが無効になると思いますか? – Till

関連する問題