I have this in my config.h
#define SomeImage [UIImage imageNamed:#"some_pic.png"]
I have a class named Something subclass of UIImageView, imported config.h in Something.h
In Something.m initWithFrame method:
self.image = SomeImage;
The problem is when i create a new Something and add it to superview , I can't see anything.
I think I can handle the problem with initWithImage but couldn't figure out why my code isn't working.
Did you mentioned frame for your Something.m ?
It should work fine, example
Something *something = [[Something alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
[self.view addSubview:something];
Something.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
self.image = SomeImage;
return self;
}
Related
I have a custom cell in a UITableView using an xib file. I programatically created a UILabel with a height of 200 and a width of 50. When I do an NSLog in the customCell.m of the label's width and height, it gives me w: 50 and h: 200. But when I do an NSLog in mainViewController.m it gives me 0 for the height and width.
Not sure why it does that. I need to get the real height of the label in the mainViewController.m
Here's my code:
customCell.m
- (void)awakeFromNib
{
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
[self.label setText:#"This is a label"];
[self.myView addSubview:self.label];
NSLog(#"%f", self.label.frame.size.height); // Results: 200.0000
}
mainViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
customCell *cellVC = [[cutsomCell alloc] init];
NSLog(#"%f, %f", cellVC.label.frame.size.height); // Results: 0.0000
}
Isn't awakeFromNib supposed to get called the mainViewController.m at viewDidLoad if I use an xib file? If not, how do I call it in viewDidLoad?
awakeFromNib is an initializer called when you load from nib..ie you add a view and change its class to your custom class in storyboard/nib and this porcess will call awakeFromNib method..not programatically
When done programatically use init method for the purpose or a custom initializer
-(id)init
{
//class super init calls
//then method calls
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
[self.label setText:#"This is a label"];
[self.myView addSubview:self.label];
//return self
}
Example
- (id) init {
// Call superclass's initializer
self = [super init];
if(self) {
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
[self.label setText:#"This is a label"];
[self.myView addSubview:self.label];
}
return self;
}
My idea is to add generated integer value to the name of UIImageView. Here is an example: I have three UIImageViews named imageView1, imageView2, imageView3. When a button is pressed I have there this simple code:
self.value = value + 1;
(and of course I have this "value" declared) Now I want add some code which says something like:
UIImage *img = [UIImage imageNamed:#"image.png"];
[imageView(value) setImage:img];
So this I want this part of code ...[imageView(value)... to use the value I defined above. How could I do this?
You can set a tag for your image views:
// set tag
[imageView1 setTag:1];
[imageView2 setTag:2];
//...
// get the image view with the tag
[(UIImageView *)[self.view viewWithTag:value] setImage:img];
//...
You could use a tag, or you can put the UIImageViews into an NSArray. Then you can do something like
[((UIImageView *)[imageViewArray objectAtIndex:self.value]) setImage:img];
Here I tried to did according to your requirement.
my ViewController Class
- (void)viewDidLoad
{
[super viewDidLoad];
ExtendedUIImageView *eimage=[[ExtendedUIImageView alloc]initExtendedUIImageViewWithFrame:CGRectMake(0, 0, 768, 1024)];
UIImage *img1=[UIImage imageNamed:#"mac.png"];
UIImage *img2=[UIImage imageNamed:#"images.jpeg"];
UIImage *img3=[UIImage imageNamed:#"mac.png"];
[eimage addUIImage:img1 ToExtendedImageViewWithRect:CGRectMake(100, 100, 100, 100) withTag:1];
[eimage addUIImage:img2 ToExtendedImageViewWithRect:CGRectMake(210, 100, 100, 100) withTag:2];
[eimage addUIImage:img3 ToExtendedImageViewWithRect:CGRectMake(320, 100, 100, 100) withTag:3];
[self.view addSubview:eimage];
}
- (IBAction)chageImage:(UIButton *)sender {
NSLog(#"#####");
UIImage *img2=[UIImage imageNamed:#"images.jpeg"];
[eimage replaceImage:img2 forTag:3];
}
I Extended the UIView Class and created new Class ExtendedImageView class
ExtendedImageView.h
#interface ExtendedUIImageView : UIView
-(id)initExtendedUIImageViewWithFrame:(CGRect)rect;
-(void)addUIImage:(UIImage *)img ToExtendedImageViewWithRect:(CGRect)rect withTag:(int)n;
-(void)replaceImage:(UIImage *)newImg forTag:(int)n;
#end
ExtendedImageView.m
#implementation ExtendedUIImageView
-(id)initExtendedUIImageViewWithFrame:(CGRect)rect{
if([super initWithFrame:rect]){
return self;
}
return nil;
}
-(void)addUIImage:(UIImage *)img ToExtendedImageViewWithRect:(CGRect)rect withTag:(int)n{
UIImageView *imgView=[[UIImageView alloc]initWithImage:img];
//[imgView setBackgroundColor:[UIColor grayColor]];
imgView.frame=rect;
[imgView setTag:n];
[self addSubview:imgView];
}
-(void)replaceImage:(UIImage *)newImg forTag:(int)n{
for(UIView * img in [self subviews]){
if([img isKindOfClass:[UIImageView class]]&&[img tag]==n){
((UIImageView *)img).image=newImg;
}
}
}
#end
I created view-based application.
I have got a sampleGameViewContrioller.xib, which contains main View, and child View, which connected with class Behavior.
SampleViewController.xib:
View
View <- Behavior
In sampleViewContoller.m I create instance of the class Behavior:
Behavior *b = [[Behavior alloc] initilizeWithType:#"type" position:rect mapArray:arrMap];
Behavior.m:
-(id) initilizeWithType:(NSString *)type position:(CGRect) pos mapArray:(NSMutableArray *) mapArr {
self = [super initWithFrame:CGRectMake(0, 0, 1024, 570)];
if (self != nil) {
if ([type isEqualToString:#"type"]) {
arrMap = mapArr;
self.rectForDraw = pos;
self.file = [[NSString alloc]initWithString:#"girl.png"];
UIImageView *imgg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"girl.png"]];
imgg.frame = rect;
[self addSubview:imgg];
timer = [NSTimer scheduledTimerWithTimeInterval:0.015 target:self selector:#selector(moving:) userInfo:nil repeats:YES];
}
}
return self;}
But it doesn't work. Image doesn't add to my View.
if I add imageView in -(void)drawRect:(CGRect) rect, it's working:
- (void)drawRect:(CGRect)rect {
self.img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"girl.png"]];
self.img.frame = CGRectMake(100, 100, 100, 100);
[self addSubview:self.img]; }
But i should send parameters of drawing in class Behavior through constructor. How to add imageView without method drawRect, with my constructor?
sorry for my English :)
If u want to add an image view over simple UIView then just do [self.view addSubview:imageview]; If you are getting that Image from Behaviour class method then return UIImageView from that method and the add it to your view
Try this:
Behavior *b = [[Behavior alloc] init];
Then write one method in Behaviour class
-(UIImageView*) initilizeWithType:(NSString *)type position:(CGRect) pos mapArray:(NSMutableArray *) mapArr {
// Your Logic
return imgView; //UIImageView object
}
Then call this method in your viewcontroller as
UIImageView *imgView=[b initilizeWithType:#"YOUR STRING" position:YOUR RECT pos mapArray:YOUR ARRAY ];
imgg.frame = rect;
This line assigns your imageView frame to... nothing as far as I can see. Should this be pos or rectForDraw? When adding to your view in drawRect you are explicitly setting a real frame so this is probably where the difference lies.
I have a UIView class, to create the structure, I do:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//creo la view
CGRectMake(0, 0, 100, 100); //I would just like the frame size
self.clipsToBounds = YES;
}
return self;
}
so I make the location and size of the frame.
I call this class in the main UIViewController
is there a way to just create the size of frame in the class, and in the UIViewController set the position?
MyClass *myClass1 = [[Item alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; //I would just like the frame position
[self.view addSubview:myClass1];
thanks a lot!
I think you are looking at something like initWithOrigin:.
- (id)initWithOrigin:(CGPoint)origin {
CGSize size = CGSizeMake(100,100);
CGRect frame;
frame.origin = origin;
frame.size = size;
return [self initWithFrame:frame];
}
Now you can use it as,
MyClass *myClass1 = [[MyClass alloc] initWithOrigin:CGPointMake(0,0)];
[self.view addSubview:myClass1];
I'm a beginner. I just want to draw a simple line without using IB .
code I used is
lineViewController.m
-(id)init
{
if(self = [super init])
{
myView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,440)];
myView.backgroundColor = [UIColor grayColor];
mView = [[UIView alloc]initWithFrame:CGRectMake(0,0,200,200)];
self.view = myView;
[myView addSubView:mView];
}
return self;
}
-(void)viewWillAppear:(BOOL)animated
{
line = [[MyView alloc]init];
[line setNeedsDisplay];
}
MyView.m
#import "MyView.h"
#implementation MyView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetStrokeColor(c, red);
CGContextBeginPath(c);
CGContextMoveToPoint(c, 150.0f, 200.0f);
CGContextSetLineWidth(c, 5.0f);
CGContextAddLineToPoint(c, 50,300);
CGContextAddLineToPoint(c, 260,300);
CGContextClosePath(c);
}
But I'm not able to draw. Can you please help me.
Thanks in advance.
The code in viewWillAppear doesn't do anything because line is never added to the view hierarchy. MyView drawRect is never called because of the above and "myView" is allocated as a UIView instead of a MyView. Try changing the alloc in the view controller init.
myView = [[MyView alloc]initWithFrame:CGRectMake(0,0,320,440)];
Also the code will leak as written. Look into retain & release memory management.