iphone - Add a UIImageView to a UIView -


ive been stuck on while appreciated.

i have made class called shape uiview , want add uiimageview uiview.

here .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 .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 lot!

hey again sorry if confusing im trying port c sharp code objective, code above first attempt , no need finished can see i'm trying acheive, sorry im new objective c , different other languages use :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 = 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, you're doing in c# code isn't different in objective-c (this code untested!):

in 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)     {         (int = 0; < blocksx; i++)         {             (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 adds uiimageviews subviews. note blockview allocated , configured, added subview of self. calling -addsubview: retains new subview , adds view hierarchy. therefore, blockview released right away. when whole shapeview object deallocated, uiview's -dealloc implementation takes care of removing , releasing subviews.

hope helps!


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -