简体   繁体   中英

Add a UIImageView to a UIView

Ive been stuck on this for a while now so any help will be greatly appreciated.

I have basically made a class called shape which is a UIView and I want to add an UIImageView to that UIView .

Here is my .h file:

@interface Shape : UIView {

 int blockWidth;
 int blockHeight;
 NSString *colour;

 IBOutlet UIImageView *blockView;

}

@property (nonatomic, retain) IBOutlet UIImageView *blockView;
@property (nonatomic, retain) NSString *colour;

-(void)setColour:(NSString *)colour;
-(void)createShape:(int)blocksX :(int)blocksY;


@end

And here is my .m file:

@implementation Shape

@synthesize blockView;
@synthesize colour;

- (void)setColour:(NSString *)colour{
 NSLog(@"Colour: %@", colour);
}

-(void)createShape:(int)blocksX :(int)blocksY{
 self.frame = CGRectMake(0, 0, 200, 200);
 blockView.frame = CGRectMake(0, 0, 100, 100);

 self.backgroundColor = [UIColor greenColor];
 blockView.backgroundColor = [UIColor redColor];
 [self addSubview:blockView];

}

- (void)dealloc {

 [blockView release];
 [colour release];
 [super dealloc];
}


@end

Thanks a lot!

Hey Again sorry if this is confusing but Im trying to port this c sharp code into objective, the code above is my first attempt and is obviously no where need finished but you can hopefully see what I'm trying to acheive, sorry but Im completely new to Objective C and it is completely different to other languages I am use to :S

public class Shape : UIView
    {
        public UIImageView blockView;

        private int blockWidth = 40;
        private int blockHeight = 40;


        public Shape (int startX, int startY ,string colour, int blocksX, int blocksY)
        {
            Console.WriteLine("Colour: "+colour+" Blocks: "+blocksX+" "+blocksY);

            this.Frame = new RectangleF(startX,startY,blockWidth*blocksX,blockHeight*blocksY);
            this.UserInteractionEnabled = true;


            for(int i = 0; i<blocksX; i++)
            {
                for(int j = 0; j<blocksY; j++)
                {
                    blockView = new UIImageView(UIImage.FromFile("Images/Blocks/"+colour+"block.jpg"));
                    blockView.Frame = new RectangleF(blockWidth*i,blockHeight*j,blockWidth,blockHeight);
                    Console.WriteLine("I: "+i+" J: "+j);
                    this.AddSubview(blockView);
                }
            }
        }   

(deleted old text)

Actually, what you're doing in your C# code isn't that different in Objective-C (this code is untested!):

In the header file:

#import <UIKit/UIKit.h>

@interface ShapeView : UIView {

}

@end

Implementation file:

#import "ShapeView.h"

#define kBlockWidth 40
#define kBlockHeight 40

@implementation ShapeView

- (id)initWithStartX:(int)startX startY:(int)startY colour:(NSString*)colour blocksX:(int)blocksX blocksY:(int)blocksY
{
    CGRect frame = CGRectMake(startX, startY, kBlockWidth * blocksX, kBlockHeight * blocksY);

    if ((self = [super initWithFrame:frame]) != nil)
    {
        for (int i = 0; i < blocksX; i++)
        {
            for (int j = 0; j < blocksY; j++)
            {
                UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"Images/Blocks/%@block.jpg", colour]];
                UIImageView* blockView = [[UIImageView alloc] initWithImage:image];

                blockView.frame = CGRectMake(kBlockWidth * i,
                                             kBlockHeight * j,
                                             kBlockWidth,
                                             kBlockHeight);

                [self addSubview:blockView];
                [blockView release];
            }
        }
    }

    return self;
}

@end

This implementation simply adds the UIImageViews as subviews. Note that the blockView is allocated and configured, then added as a subview of self . Calling -addSubview: retains the new subview and actually adds it to the view hierarchy. Therefore, blockView is released right away. When the whole ShapeView object is deallocated, UIView's -dealloc implementation takes care of removing and releasing the subviews.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM