UIButtons from different views add images to same view? - iphone

So, I am building an application with different views. Each of them has a number of buttons. When pressing a button in a view, it should add an image to a addedImages view (the main one).
How do I make a loop that will add the images dynamically according to the position of previously added images?
Im quite new in this, so now I have only one button adding an image like this:
- (IBAction)addItemToShoppingList
{
UIImage *image = [UIImage imageNamed:#"button1.jpg"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = CGRectMake(120, 100, 80, 80);
[shoppingList.view addSubview:imageView];
[imageView release];
}
So I wanna make a function addItemToShoppingList that will be associated to all buttons in different views but to put different images dynamically to the same one?

int shoppingListX;
- (IBAction)addItemToShoppingList
{
UIImage *image = [UIImage imageNamed:#"button1.jpg"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = CGRectMake(shoppingListX, 100, 80, 80);
shoppingListX += 80;
[shoppingList.view addSubview:imageView];
[imageView release];
}

Related

How to navigate into other screen while clicking a image in iPhone

I am very to new phone i want to display four images in one line like tabbar and also if the user clicks any of the image i want to show another screen in iPhone.
can any one tell me how can i do that and also if possible please tell me in which files i have to write the code to achieve this requirement.
Try This
In ViewDidLoad
CGRect myImageRect = CGRectMake(10.0f, 5.0f, 80.0f, 95.0f);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect];
[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
imageView.opaque = YES; // explicitly opaque for performance
[self.view addSubview:imageView];
UIButton *newbutton1 = [[UIButton alloc] init];
[newbutton1 setFrame:myImageRect];
[newbutton1 addTarget:self action:#selector(myMethod:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:newbutton1];
//Method on Button Click
-(void)myMethod:(id)sender {
//Button is pressed
}
Make some xib file and build your UI.
You need to set other screens as IBOutlets and after click on button perform action to switch view.
You can set image background for UIButton.
Try, it isn't so big problem.
UIButton with images tutorial
Is a great tutorial for creating clickable images.
arrange 4 images manually in ur screen
eg
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
myImageView.image = [UIImage imageNamedWith:#"ur image.png"];
UIGestureRecognizer *recognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(showtheUI:)];
[myImageView addGestureRecognizer:recognizer1];
recognizer1.delegate = self;
[recognizer1 release];
[self.view addSubView:myImageView ];
}
//this method will trigger when u tapped the 1st image
-(void)showtheUI(UIImageViee *)sender{
}
create 2nd imageView like
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 10, 100, 100)]; etc
or use a for loop to create 4 image Views. set frame according to the i value
you need to create the custom table or more things to use for the creating a grid type of image
for that you need to use following things to use
TableView
Custom UITableViewCell
Array of images
HJCache for asynchronous image loading.
You can add buttons on your images and handle click events of buttons to Navigate to other screens

UIImageView animation is not displayed on view

In my Iphone App, I used the following code for the animation of those 3 images in viewDidLoad() method and I use left transform to load this ViewController. But these imageView is not displayed on the view.
NSArray *newArray = [[NSArray alloc]initWithObjects:[UIImage imageNamed:#"search1.png"],
[UIImage imageNamed:#"search2.png"],
[UIImage imageNamed:#"seach3.png"],
nil];
UIImageView *imageView = [UIImageView alloc];
[imageView initWithFrame:CGRectMake(240, 60, 60, 30)];
imageView.animationImages = newArray;
imageView.animationDuration = 0.25;
imageView.animationRepeatCount = 3;
[self.view addSubview:imageView];
Animation won't start until you tell it to start. You will need to add a command to startAnimating. I also cleaned up your alloc/init and I added a command to make the image view still keep showing when it is not animating. Setting an animation will not make the image appear. The image property is for the still image and the animationImages property is for the animating images of a UIImageView.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 60, 60, 30)];
imageView.image = [UIImage imageNamed:#"search3.png"]; //This will be the image that is visible when it is NOT animating.
imageView.animationImages = newArray;
imageView.animationDuration = 0.25;
imageView.animationRepeatCount = 3;
[imageView startAnimating]; //This will make the animation start
[self.view addSubview:imageView];
Finally, your third image is missing an r. I think you want #"search.png" and not #"seach.png"

how to show image on right hand side of table view for iphone

i can show image on left hand side of table view but why my right hand side is not working
CGRect frame;
frame= CGRectMake(310 ,10, 50, 50);
UIImageView * myImageView = [[UIImageView alloc]init];
cell.myImageView.image = [UIImage imageNamed:#"bowl.png"];
myImageView.frame = frame;
[myImageView release];
Try this, it works for me:
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yellow_dot.png"]];
cell.accessoryView = image;
Do this code in willDisplayCell and it should work. Also remember the default cell already has an imageview, you can just move its frame.

how to remove my image sub view from my navigation main view

UIImage *image = [UIImage imageNamed:#"logo_header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 10, 320, 28);
[self.navigationController.view addSubview:imageView];
I have added an image to my mainview using above code, how can I remove it from view so that it will not visible in next view?
I'm not sure that I understand the reason for doing this in the first place.
But you might set a tag value for this image view and then find it by that tag:
..
UIImage *image = [UIImage imageNamed:#"logo_header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 10, 320, 28);
imageView.tag = 100001;
[self.navigationController.view addSubview:imageView];
// Why don't you release the image view?
[imageView release];
..
UIView *imageView = [self.navigationController.view viewWithTag:100001];
[imageView removeFromSuperView];
Not sure that all the code is correct - wrote it without XCode...

Showing an image programmatically in tableview cell

I'm doing this to load an image into a tableview cell:
UIImage *image = [UIImage imageNamed:#"myimage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake( 0, 0, cell.frame.size.width, cell.frame.size.height );
imageView.contentMode = UIViewContentModeScaleToFill;
[cell addSubview:imageView];
[imageView release];
return cell;
However, no image displays as the cell's background. This is in a new navigation app. The above looks just like the steps in Interface Builder when you are using a custom cell. The image is in the app bundle. What am I doing wrong?
Are you trying to set the cell's background, or just add an image? If you're trying to set its background, you should use the cell.background property.
imageView.frame = cell.bounds;
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
cell.backgroundView = imageView;