change UIButton image with array - ios

novice need help! idea: when you press the button you need to change the image of the array. Yes, I'm stupid.
from *.h:
{
NSMutableArray *_cat;
NSInteger _catindex;
}
#property (weak, nonatomic) IBOutlet UIButton *catbtn;
- (IBAction)catbutton:(id)sender;
from *.m:`
- (void)viewDidLoad
{
[super viewDidLoad];
_cat = [NSMutableArray array];
UIImage *cat1 = [UIImage imageNamed:#"cat1.png"];
UIImage *cat2 = [UIImage imageNamed:#"cat2.png"];
UIImage *cat3 = [UIImage imageNamed:#"cat3.png"];
UIImage *cat4 = [UIImage imageNamed:#"cat4.png"];
UIImage *cat5 = [UIImage imageNamed:#"cat5.png"];
[_cat addObject:cat1];
[_cat addObject:cat2];
[_cat addObject:cat3];
[_cat addObject:cat4];
[_cat addObject:cat5];
self.catbtn.image = [_cat objectAtIndex:_catindex];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)catbutton:(id)sender
{
_catindex++;
if (_catindex == _cat.count)
{
_catindex = 0;
}
self.catbtn.image = [_cat objectAtIndex:_catindex];
}
I do not know whether it is possible to do that, but I hope for your help.

self.catbtn.image = [_cat objectAtIndex:_catindex];
won't work, you need to use:
[self.catbtn setImage: [_cat objectAtIndex:_catindex] forState:UIControlStateNormal];
forState can be used to set the image for when the button is selected / highlight etc.

use this to set image for UIButton
[self.catbtn setImage:[_cat objectAtIndex:_catindex] forState:UIControlStateNormal];

Instead of this:
self.catbtn.image = [_cat objectAtIndex:_catindex];
write this:
[self.catbtn setImage:[_cat objectAtIndex:_catindex] forState:UIControlStateNormal];
in both places.

Related

Getting frame of subview enumeration

So, I have repeating views containing thumbnails, once a thumbnail is pressed the thumbnail ID is sent as the tag.
With this information I want to get the frame of the subview of that view;
- (IBAction)thumbPressed:(id)sender {
NSLog(#"Thumb %i pressed", (int)[sender tag]);
for (int i = 0; i < _thumbCounter; i++) {
if (i == [sender tag]) {
NSLog(#"thumb view %i", i);
//Up To HERE
break;
}
}
// [self moveToVideoControllerWithInfoID:[NSString stringWithFormat:#"%li", (long)[sender tag]]];
}
For the thumbnails to be drawn they're drawn in a random order retrieved by JSON.
The Button
UIButton *thumbButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[thumbButton addTarget:self
action:#selector(thumbPressed:)
forControlEvents:UIControlEventTouchUpInside];
thumbButton.frame = CGRectMake(0, 0, _thumbView.frame.size.width, _thumbView.frame.size.height);
thumbButton.tag = _thumbCounter;
[_thumbView addSubview:thumbButton];
The subview I want to get the frame of
NSString *string = [NSString stringWithFormat:#"***.jpg",Link];
NSURL * imageURL = [NSURL URLWithString:string];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(thumbGap, thumbGap, thumbHeight - 5, thumbHeight - 5)];
imageView.image = image;
[_thumbView addSubview:imageView];
Where the thumbnail shell is drawn
_thumbView = [[ThumbView alloc] initWithFrame:CGRectMake(0, margin, _scrollView.frame.size.width, thumbHeight)];
_thumbView.backgroundColor = [UIColor whiteColor];
_thumbView.thumbId = _thumbCounter;
[_scrollView addSubview:_thumbView];
_thumbview is a class of UIVIEW with the added thumbId
How once that button is pressed can I locate the imageView frame inside of _thumbview ( bare in mind there are multiple ).
First off, let's make life easier for you:
- (IBAction)thumbPressed:(UIButton*)sender {
NSLog(#"Thumb %i pressed", (int)[sender tag]);
ThumbView *thumbView = (ThumbView*)[sender superView];
for(UIView* subview in thumbView.subViews) {
if([subView iKindOfClass:[UIImageView class]]) {
//you got it here
}
}
}
You could shortcut the whole thing by making ThumbView have an imageView property as well.
I've tried to determine that this is what you are doing:
_scrollView
->_thumbView[0]
--->thumbButton
--->imageView
->_thumbView[1]
--->thumbButton
--->imageView
...
->_thumbView[N]
--->thumbButton
--->imageView
Assumed that you want the image view who is in the same thumbView as the button who is pressed.
Best Possible Solution (IMHO):
#Interface ThumbView()
#property (nonatomic,weak) UIImageView *imageView;
#end
AND:
(After making sure to first add, and THEN SET .imageView)
- (IBAction)thumbPressed:(UIButton*)sender {
NSLog(#"Thumb %i pressed", (int)[sender tag]);
UIImageView *imageView = ((ThumbView*)[sender superView]).imageView
}
for (UIView *i in _scrollView.subviews) {
if ([i isKindOfClass:[ThumbView class]]) {
ThumbView *tempThumb = (ThumbView *)i;
if (tempThumb.thumbId == (int)[sender tag]) {
for (UIView *y in tempThumb.subviews) {
if ([y isKindOfClass:[UIImageView class]]) {
UIImageView *tempIMG = (UIImageView *)y;
[self playVideo:[loadedInput objectForKey:#"link"] frame:tempIMG.frame andView:tempThumb];
}
}
}
}
}

iOS: How to check what the button background image is?

I am trying the following method ,but it isn't work. Is there a way I can check what my current background image is for my UIButton and compare it?
UIImage *buttonCheckImage = checkButton.currentBackgroundImage;
UIImage *checkImage = [UIImage imageNamed:#"checkBox.png"];
if (buttonCheckImage == checkImage) {
}
this works using
[ UIImagePNGRepresentation( uiImage1 ) isEqualToData:
UIImagePNGRepresentation( uiImage2 ) ];
UIImage *buttonCheckImage = [self.btn_Checkbox imageForState:UIControlStateNormal];
UIImage *checkImage = [UIImage imageNamed:#"checkbox"];
if([UIImagePNGRepresentation(buttonCheckImage) isEqualToData:UIImagePNGRepresentation(checkImage)])
{
self.btnImage = [UIImage imageNamed:#"checkbox1"];
}
else
{
self.btnImage = [UIImage imageNamed:#"checkbox"];
}
[self.btn_Checkbox setImage:self.btnImage forState:UIControlStateNormal];
using : http://stackoverflow.com/questions/14078283/comparing-two-images-whether-same-or-not-ios
Hope its working well try this....
.h
#property (weak, nonatomic) IBOutlet UIButton *checkButtonObj;
-(IBAction)buttonClick:(id)sender;
.m
-(void)viewDidLoad {
[super viewDidLoad];
[self.checkButtonObj setImage:[UIImage imageNamed:#"correct.png"] forState:UIControlStateNormal];
}
-(IBAction)buttonClick:(id)sender {
if ([self.checkButtonObj.imageView.image isEqual:[UIImage imageNamed:#"correct.png"]])
[self.checkButtonObj setImage:[UIImage imageNamed:#"wrong.png"] forState:UIControlStateNormal];
else
[self.checkButtonObj setImage:[UIImage imageNamed:#"correct.png"] forState:UIControlStateNormal];
}
Try the following code,
UIImage *buttonCheckImage = [checkButton imageForState:UIControlStateNormal];
UIImage *checkImage = [UIImage imageNamed:#"checkBox.png"];
if (buttonCheckImage == checkImage) {
}

UIimage will not move on btn press, what am i doing wrong?

I am trying to move a UIImage, first button press creates the image and second press moves it.
The image only needs to exist upon pressing the button.
In the simulator it creates the button and places it, the second time it click just doesn't do anything.
This is my Code
- (IBAction) btn:(id)sender {
UIImageView *myImage = [[UIImageView alloc] init];
myImage.image = [UIImage imageNamed:#"keyframe"];
if (startUp == 1){
//Create Image and add to view
myImage.frame = CGRectMake(200, 300, 10, 10);
myImage.image = [UIImage imageNamed:#"keyframe"];
[self.view addSubview:myImage];
//Set startUp to 0 and output rect value
startUp = 0;
NSLog(#"currentFrame %#", NSStringFromCGRect(myImage.frame));
}else if (startUp == 0){
//Change position, size and log to debug
myImage.frame = CGRectMake(500,100 ,20, 20);
NSLog(#"newFrame %#", NSStringFromCGRect(myImage.frame));
}
}
How do you programmatically move a programmatically added UIimage?
I tried changing the center value but that doesn't work either.
Try something like this – tested and working sample:
#import "ViewController.h"
#interface ViewController () {
BOOL startUp;
UIImageView *myImage;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
startUp = YES;
}
- (IBAction)doWork:(id)sender {
if (startUp) {
UIImage *img = [UIImage imageNamed: #"keyframe"];
myImage = [[UIImageView alloc] initWithImage: img];
[myImage sizeToFit];
[myImage setCenter: CGPointMake(200, 300)];
[self.view addSubview: myImage];
startUp = NO;
} else {
[myImage setCenter: CGPointMake(400, 500)];
}
}
#end

Rotating Background Images

I have three images that I want to rotate in the background. Below is what I have so far. I want to have a class where I can hold all these UIImageViews and display them randomly in the background. I read about UIView and the frame method but I have no idea how to add them since it only takes in one frame.
Thus, I used NSArray to hold all the objects instead. The only problem now is when a new background appears, the old background doesn't disappear. Now do I remove the old background?
It would be great if someone can point me in the right direction.
Thanks!
#interface ViewController : UIViewController
#property (strong, nonatomic) NSArray *imageArray;
#property (strong, nonatomic) UIImageView *imageView;
- (IBAction)buttonPressed:(UIButton *)sender;
#end
// .m file
#implementation ViewController
#synthesize imageArray;
#synthesize imageView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
imageView = [[UIImageView alloc] init];
UIImage *image1 = [UIImage imageNamed:#"image1.png"];
UIImage *image2 = [UIImage imageNamed:#"image2.png"];
UIImage *image3 = [UIImage imageNamed:#"image3.png"];
imageArray = [[NSArray alloc] initWithObjects:image1, image2, image3, nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonPressed:(UIButton *)sender {
NSUInteger index = arc4random_uniform(self.imageArray.count);
[imageView setImage:[imageArray objectAtIndex:index]];
}
#end
This:
[self.view insertSubview:current atIndex:0];
Is adding another view to your view hierarchy, but you are not removing the one you previously placed. So you have an ever-growing stack of UIVews. You are also placing the new view at the bottom of the stack.
try
[[[self.view subviews] objectAtIndex:[[self.view subviews] count]-1] removeFromSuperview];
[self.view addSubview:current];
Or better - as #Kaan suggests - have a single UIImageView and simply change it's UIImage property.
To do this, your array will contain your UIImages not your UIImageviews.
Your UIView can be an UIImageview, or it can contain a UIImageview.
The UIImageview has an image property which you can set.
Your code would look something like this...
self.imageArray = [[NSArray alloc] initWithObjects:image1, image2, image3, nil];
NSUInteger index = arc4random_uniform(self.imageArray.count);
UIImage *current;
current = [self.imageArray objectAtIndex:index];
self.imageview.image = current;
In your approach, you have created 6 objects - 3 UIImages and 3 UIImageViews.
What you want can be accomplished using 4 objects instead of 6, using less memory, making your app run more quickly and answering your question at the same time (also, i'm assuming you have background images of all the same size, the size of the device's screen).
I would suggest creating first, only one UIImageView:
//backgroundImage is an IVAR
backgroundImage = [[UIImageView alloc] init];
followed by three UIImages and place them in an NSArray:
UIImage *image1 = [UIImage imageNamed:#"image1.png"];
UIImage *image2 = [UIImage imageNamed:#"image2.png"];
UIImage *image3 = [UIImage imageNamed:#"image3.png"];
//imagesArray is an IVAR
imagesArray = [[NSArray alloc]] initWithObjects: image1, image2, image3, nil];
Finally, to change the background, call the random function and update the UIImageView image property instead of popping a different view on top of the view stack:
NSUInteger index = arc4random() % [imagesArray count];
[backgroundImage setImage:[imagesArray objectAtIndex:index]];

Toggle the button Image

After i implemented a code for give an check image for a button but i want to give check image for no of buttons.
note: if i click on one button check image can be displayed and the same time i click on another button check image displayed on that particular button and previous button comes normal position.
i implement the code for single button here like this.
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
UIImage* img = [UIImage imageNamed:#"btn_check_on.png"];
[self setImage:img forState:UIControlStateNormal];
}
else
{
UIImage* img = [UIImage imageNamed:#"bread_Wheat_rectangle.png"];
[self setImage:img forState:UIControlStateNormal];
}
}
The above code is executed successfully but how to use this code for no of buttons.
please suggest any tutorial regarding my problem
This is how I have implemented it for one button. You can use it for more buttons too.
-(IBAction)ButtonAction
{
if (Flag==0)
{
Flag=1;
[myButton setImage:[UIImage imageNamed:#"checkbox-filled.png"] forState:UIControlStateNormal];
}
else
{
[myButton setImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
Flag=0;
}
}
Note : If you want only one button to get checked Just set all other buttons image as checkbox.png and the selected one's checkbox-filled.png.
EDIT
You can make a class for checkbox and then use it. Here is the code...
CheckButton.h
#import <Foundation/Foundation.h>
#interface CheckButton : UIButton {
BOOL _checked;
int chkButtonClickVal;
}
#property (nonatomic, setter=setChecked:) BOOL checked;
-(void) setChecked:(BOOL) check;
-(int)chkButtonClickVal;
#end
CheckButton.m
#import "CheckButton.h"
#implementation CheckButton
#synthesize checked = _checked;
-(id) init
{
if( self=[super init] )
{
chkButtonClickVal=0;
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void) awakeFromNib
{
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
-(void) dealloc
{
chkButtonClickVal=0;
[super dealloc];
}
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
UIImage* img = [UIImage imageNamed:#"checkbox-checked.png"];
[self setImage:img forState:UIControlStateNormal];
chkButtonClickVal=1;
}
else
{
UIImage* img = [UIImage imageNamed:#"checkbox.png"];
[self setImage:img forState:UIControlStateNormal];
chkButtonClickVal=2;
}
//NSLog(#"%d",chkButtonClickVal);
}
-(int)chkButtonClickVal
{
return chkButtonClickVal;
}
-(void) OnCheck:(id) sender
{
self.checked = !_checked;
}
#end
I have done it in same way. Try you'll be able to achieve it.
Good Luck :)
After long practicing this problem we can use switch cases it can be done very easily
switch (currenttagvalue) {
case 1:
[level1 setImage:[UIImage imageNamed:#"one_time_selected.png"] forState:UIControlStateNormal];
[level2 setImage:[UIImage imageNamed:#"ic_launcher.png"] forState:UIControlStateNormal];
[level3 setImage:[UIImage imageNamed:#"bread_sourdough_rectangle.png"] forState:UIControlStateNormal];
}
in this level is "IBOutlet UIButton level1;"
And then i implement so more buttons

Resources