开发者

Create UIImageView in object

I am trying to create a UIImageView programmatically in a method of a object.

-(void) createApple
{
  CGRect myImageRect = CGRectMake(100, 100, 64, 64);
  image = [[UIImageView alloc] initWithFrame:myImageRect];
  [i开发者_开发技巧mage setImage:[UIImage imageNamed:@"Apple.png"]];
  [self.view addSubview:image];
  [image release]; 
}

when I use this code in the ViewController.m it works fine but when I use it in my Apple object I get an error:request for member 'view' in something not a structure or union.

What do I use instead of self.view???

Sorry if I'm repeating posts. I saw a similar question to this but wasn't sure if it was exactly the same situation.


If I got your question correctly you have an interface like this

@interface Apple: NSObject
@end

If you want to create an UIImageView using NSObject methods try this

//first declare a method of UIImageView type
@interface Apple: NSObject
{

}
-(UIImageView *)ImageView;
@end

now come in your Apple.m file

@implementation Apple

-(UIImageView *)ImageView
{
    UIImageView *imgView = [UIImageView alloc]initWithFrame:CGRectMake(100, 100, 64, 64)];
    imgView setImage:[UIImage imageNamed:@"Apple.png"];
    return imgView;
}
@end

now call this method where you want to show your image, in your ViewController.m file you can call it like this- You just have to import your object class first.

#import "ViewController.h"
#import "Apple.h"

@implementation ViewController
-(Void)ViewDidLoad
{
    Apple *appleObject = [Apple alloc]init];
    UIImageView *imageViewOfViewController = [appleObject ImageView];
    [self.view addSubView:imageViewOfViewController];
}
@end

I show you imageview method in viewDidLoad method you can call it any where like this.


The line causing the error would be:

[self.view addSubview:image];

If your Apple object does not extend UIViewController then there will be so self.view property on it, unless you have defined one yourself.

As to what to use instead, that is difficult to say without knowing specifically what you are trying to do.

However, assuming that you want your Apple class to display something on the screen, then probably what you want to do is update Apple so that it extends either UIViewController or (perhaps) UIView. Then in the first case you can use self.view, and in the second case you can just do [self addSubview: image];.


Because "view" is a property of UIViewController and your "apple object" doesn’t have one.


The problem is, you're not calling this method inside UIViewController. Personally I'd do it slightly other way:

- (UIView*) newAppleView
{ 
  CGRect myImageRect = CGRectMake(100, 100, 64, 64);

  UIImageView *imageView;
  imageView = [[UIImageView alloc] initWithFrame:myImageRect];
  [imageView setImage:[UIImage imageNamed:@"Apple.png"]];

  return [imageView autorelease];
}

Assuming the newAppleView method is implemented in your UIViewController, you can easly add new apple:

[self.view addSubview:[self newAppleView]];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜