Button changing of state programmatically - iphone

Im really having a problem with my image with button frame,
What I want is:
1. When it is selected it will have a checkmark
2. When the view is dismissed then returned back to the previous screen it will still have the check in the image selected
3. When I tap the selected image or another image it will remove the checkmark.
I was able to do it but I'm having problem with the setting of image:
What I want is this:
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
[myButton setEnabled:NO];
But this is the function I needed:
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
[myButton setImage:bgImage forState:UIControlStateSelected];
[myButton setSelected:YES];
Here is the code below(If there is something wrong with what I'm doing please tell me, Thanks):
- (void)viewDidLoad {
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
// Create view
UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];
int row = 0;
int column = 0;
for(int i = 0; i < _thumbs.count; ++i) {
UIImage *thumb = [_thumbs objectAtIndex:i];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
[myButton setImage:thumb forState:UIControlStateNormal];
[myButton addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
myButton.tag = i;
NSLog(#"%i",i);
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger selectedImageSlot1 = [prefs integerForKey:#"selected1"];
if ( selectedImageSlot1 == i){
[self turnButtonToSelected];
}
[view addSubview:myButton];
if (column == 6) {
column = 0;
row++;
} else {
column++;
}
}
[view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];
}
- (void)turnButtonToSelected
{ UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
[myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
[myButton setEnabled:NO];
}
- (void)highlightButton:(UIButton *)a {
//[self turnButtonToSelected];
}
- (IBAction)buttonClicked:(id)sender {
myButton = (UIButton *)sender;
self.selectedImage = [_images objectAtIndex:myButton.tag];
if (myButton.selected) {
[sender setSelected:NO];
}else {
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
[sender setImage:bgImage forState:UIControlStateSelected];
[sender setSelected:YES];
}
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:myButton.tag forKey:#"slot1"];
[[NSUserDefaults standardUserDefaults] setInteger:myButton.tag forKey:#"selected1"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self performSelector:#selector(highlightButton:) withObject:sender afterDelay:0.0];
[self dismissModalViewControllerAnimated:NO];
}
Or is there a way to forcefully make a button enabled whenever it is disabled when tapped also?

I have just tried this on a different approach.
For me it works just fine and achieves your effect.
Let me know if you need further explanation or the entire Xcode project.
- (void)viewDidLoad
{
[super viewDidLoad];
//button 1
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton.frame = CGRectMake(30, 50, 100, 80);
UIImage *btnImage = [UIImage imageNamed:#"DSC_5359.JPG"];
[imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
//button 2
UIButton *imageButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton2.frame = CGRectMake(180, 50, 100, 80);
UIImage *btnImage2 = [UIImage imageNamed:#"DSC_5355.JPG"];
[imageButton2 setBackgroundImage:btnImage2 forState:UIControlStateNormal];
[imageButton2 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:imageButton];
[self.view addSubview:imageButton2];
}
- (void)buttonClicked:(UIButton*)button
{
//NSLog(#"Button clicked! %#", button);
UIImage *checkmarkImage = [UIImage imageNamed:#"check.png"];
UIImage *empty = nil;
//NSLog(#"Imageview: %d", button.imageView.tag);
if (button.imageView.tag == 0)
{
[button setImage:checkmarkImage forState:UIControlStateNormal];
button.imageView.tag = 1;
}
else
{
[button setImage:empty forState:UIControlStateNormal];
button.imageView.tag = 0;
}
}
Best, Chris
Update: I have updated the code. Here is the entire ViewController. Note: I would solve this using coreData to store the current index. The following code is not the best to solve this issue but is based on your initial code!
//
// SOViewController.m
// SOImageOverlay
//
// Created by Chris on 24.07.12.
//
#import "SOViewController.h"
#interface SOViewController ()
#end
#define CHECKMARK_IMAGE #"check.png"
#implementation SOViewController
#synthesize buttons = _buttons;
#pragma mark - Custom getter
- (NSMutableArray*)buttons
{
if (_buttons == nil)
_buttons = [[NSMutableArray alloc] init];
return _buttons;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
// UIButton *myButton =
// [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
// [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
// Create view
// UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];
int row = 0;
int column = 0;
for(int i = 0; i < 2; ++i) {
/*UIImage *thumb = [_thumbs objectAtIndex:i];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
[myButton setImage:thumb forState:UIControlStateNormal];
[myButton addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
myButton.tag = i;
NSLog(#"%i",i);*/
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
if (i == 0)
{
UIImage *btnImage = [UIImage imageNamed:#"DSC_5359.JPG"];
imageButton.frame = CGRectMake(30, 50, 100, 80);
[imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
}
else if (i == 1)
{
UIImage *btnImage = [UIImage imageNamed:#"DSC_5355.JPG"];
imageButton.frame = CGRectMake(180, 50, 100, 80);
[imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
}
[imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger selectedImageSlot1 = [prefs integerForKey:#"selected1"];
if ( selectedImageSlot1 == i){
[self turnButtonToSelected:imageButton];
}
[self.view addSubview:imageButton];
//adding current button into buttons array
[self.buttons addObject:imageButton];
if (column == 6) {
column = 0;
row++;
} else {
column++;
}
}
//[view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];
// //button 1
// UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
// imageButton.frame = CGRectMake(30, 50, 100, 80);
// UIImage *btnImage = [UIImage imageNamed:#"DSC_5359.JPG"];
// [imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
// [imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
//
// //button 2
// UIButton *imageButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
// imageButton2.frame = CGRectMake(180, 50, 100, 80);
// UIImage *btnImage2 = [UIImage imageNamed:#"DSC_5355.JPG"];
// [imageButton2 setBackgroundImage:btnImage2 forState:UIControlStateNormal];
// [imageButton2 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
//
// [self.view addSubview:imageButton];
// [self.view addSubview:imageButton2];
}
//- (void)buttonClicked:(UIButton*)button
//{
// //NSLog(#"Button clicked! %#", button);
//
// UIImage *checkmarkImage = [UIImage imageNamed:#"check.png"];
// UIImage *empty = nil;
//
// //NSLog(#"Imageview: %d", button.imageView.tag);
// if (button.imageView.tag == 0)
// {
// [button setImage:checkmarkImage forState:UIControlStateNormal];
// button.imageView.tag = 1;
// }
// else
// {
// [button setImage:empty forState:UIControlStateNormal];
// button.imageView.tag = 0;
// }
//}
// code from stackoverflow
// ------------------------------------------------------------
- (void)turnButtonToSelected:(UIButton*)button
{
UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
//UIButton *myButton = button; //just to use the copied code
button.selected = YES;
[button setImage:bgImage forState:UIControlStateSelected];
// [myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
// [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
// [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
// [myButton setEnabled:NO];
}
- (void)highlightButton:(UIButton *)a {
//[self turnButtonToSelected];
}
- (IBAction)buttonClicked:(id)sender
{
UIButton *myButton = (UIButton *)sender;
//I have removed this line... due to the fact that I don't want to
//implement your arrays.
//self.selectedImage = [_images objectAtIndex:myButton.tag];
//deselecting all
for (UIButton *button in self.buttons)
{
button.selected = NO;
//[button setImage:nil forState:UIControlStateNormal];
}
if (myButton.selected) {
[sender setSelected:NO];
}else {
UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
[sender setImage:bgImage forState:UIControlStateSelected];
[sender setSelected:YES];
}
// NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// [prefs setInteger:myButton.tag forKey:#"slot1"];
//getting index of button in array
NSInteger index = 0;
NSInteger foundIndex = -1;
for (UIButton *button in self.buttons)
{
if (button == myButton)
{
foundIndex = index;
}
index++;
}
if (foundIndex >= 0)
{
[[NSUserDefaults standardUserDefaults] setInteger:foundIndex forKey:#"selected1"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[self performSelector:#selector(highlightButton:) withObject:sender afterDelay:0.0];
// [self dismissModalViewControllerAnimated:NO];
}
// ------------------------------------------------------------
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)closeButtonClicked:(UIBarButtonItem *)sender
{
[self dismissModalViewControllerAnimated:YES];
}
#end

Try doing this.
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
**[myButton setSelected:NO];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];**
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
**[myButton setSelected:YES];
[myButton setImage:bgImage forState:UIControlStateSelected];**
In the later part of code, you are setting the state of the button to "selected" but in the upper code, you haven't. Rather, you have disabled the button. Try the above code which I have provided. Thanks.

Just add you chekedImage as a subview on top of the button when user clicks the button
UIImage *button = [UIImage imageNamed:#"cat.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
//add target button clicked
-(void)buttonClicked : (id)sender{
if(toogleSwitch){
[button addSubview:checkedimageView]; // Just the checked image icon
}
else{
[imageview removeFromSuperview];
}
}
What you are trying to do will only work when you have
resources of two types for all your images.
1) The image itself
2) Image with checked box as transparent layer.

Related

iPhone Button Selected State

I am created two buttons that are right next to each other to mimic a segmented control. I am doing this to customize the appearance beyond what the UIKit allows. I decided to use the selected property to keep a button pressed. I have two images that one for each state normal and selected.
The problem is that when I select a button, the button highlights and turns dark, because of the hightlight state. I decided to use the selected image for the highlight state too, but it flashes, any ideas or suggestions.
- (void)leftSegmentPressed:(id)sender
{
if ([sender isSelected]) {
[sender setSelected:NO];
}
else {
[sender setSelected:YES];
}
}
For the "selected" button, disable it and manually switch the image for the state.
- (void) viewDidLoad
{
[rightSegmentButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[rightSegmentButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateDisabled];
[leftSegmentButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[leftSegmentButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateDisabled];
}
- (void)leftSegmentPressed:(id)sender
{
sender.enabled = NO;
rightSegmentButton.enabled = YES;
}
- (void)rightSegmentPressed:(id)sender
{
sender.enabled = NO;
leftSegmentButton.enabled = YES;
}
Check whether the image you given is in your Bundle or check image name you given is in lower case or not. Then write like
[button1 setImage:[UIImage imageNamed:#"normal1.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"selected1.png"] forState:UIControlStateSelected];
[button2 setImage:[UIImage imageNamed:#"normal2.png"] forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:#"selected2.png"] forState:UIControlStateSelected];
button1.tag = 1;
button2.tag = 2;
[button1 addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]
[button2 addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]
in your button event method
-(void)buttonSelected:(id)sender {
if([sender tag] == 1) {
button1.selected = YES;
button2.selected = NO;
} else {
button1.selected = NO;
button2.selected = YES;
}
}
[button setAdjustsImageWhenHighlighted:NO];
This will prevent the flicker.
UIButton *yourButton1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
yourButton1.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[yourButton1 setTitle:#"Left" forState:UIControlStateNormal];
yourButton.backgroundColor = [UIColor clearColor];
yourButton1.tag = 1;
[yourButton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"yourNormalImage.png"];// set normal image
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[yourButton1 setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"yourSelectedImage.png"];// set selected image
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[yourButton1 setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[yourButton1 addTarget:self action:#selector(leftSegmentPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourButton1];
do same for another second button.
and in action method set bellow code..
- (void)leftSegmentPressed:(id)sender
{
UIButton *btnTemp = (UIBUtton *)sender;
if (btnTemp.tag == 1) {
[yourButton1 setSelected:YES];
[yourButton2 setSelected:NO];
}
else {
[yourButton1 setSelected:NO];
[yourButton2 setSelected:YES];
}
}

UIButton state changing

I have a custom ImagePicker. A UIScrollView with images from my NSDocumentDirectory then I added the button as my images' frame, Like a thumbnail View. What I added is when It is selected then dismissed, the next time the view loads/appears/shows it will have an image checkmark already. It was okay. But what I need is, when I tap an image it will be selected already or the image "checkmark" will be shown, then when I tap the same image the checkmark will be gone.
Here is an explanation of the codes I have:
// The UISCrollView
- (void)viewDidLoad {
// Create view
UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];
int row = 0;
int column = 0;
for(int i = 0; i < _thumbs.count; ++i) {
UIImage *thumb = [_thumbs objectAtIndex:i];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
[myButton setImage:thumb forState:UIControlStateNormal];
[myButton addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
myButton.tag = i;
NSLog(#"%i",i);
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger selectedImageSlot1 = [prefs integerForKey:#"selected1"];
if ( selectedImageSlot1 == i){
[self turnButtonToSelected];
}
}
// When button is tapped
- (IBAction)buttonClicked:(id)sender {
myButton = (UIButton *)sender;
self.selectedImage = [_images objectAtIndex:myButton.tag];
[[NSUserDefaults standardUserDefaults] setInteger:myButton.tag forKey:#"selected1"];
[self dismissModalViewControllerAnimated:NO];
}
// method when button is selected
- (void)turnButtonToSelected
{
UIImage *bgImage = [UIImage imageNamed:#"Checkmark.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
[myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
[myButton setEnabled:NO];
}
First u need to keep the selected image index around, so create a selectedImageIndex instance variable.
Then in viewDidLoad there is no need to call
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger selectedImageSlot1 = [prefs integerForKey:#"selected1"];
in the for loop...
Put it before the for loop and store the selected image index in selectedImageIndex.
- (void)viewDidLoad {
// Create view
UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
self.selectedImageIndex = [prefs integerForKey:#"selected1"];
int row = 0;
int column = 0;
for(int i = 0; i < _thumbs.count+1; ++i) {
UIImage *thumb = [_thumbs objectAtIndex:i];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
[myButton setImage:thumb forState:UIControlStateNormal];
[myButton addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
myButton.tag = i+1;
NSLog(#"%i",i);
if (self.selectedImageIndex == myButton.tag){
[self turnButtonToSelected:myButton];
}
}
Remove the line [myButton setEnabled:NO]; from turnButtonToSelected so that the user can re-tap on the button to deselect it. change turnButtonToSelected to:
- (void)turnButtonToSelected:(UIButton *)button
{
UIImage *bgImage = [UIImage imageNamed:#"Checkmark.png"];
[button setBackgroundImage:bgImage forState:UIControlStateNormal];
[button setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[button setBackgroundImage:bgImage forState:UIControlStateDisabled];
}
- (void)turnButtonToDeselected:(UIButton *)button
{
UIImage *bgImage = [UIImage imageNamed:#"Not-Checkmark.png"];
[button setBackgroundImage:bgImage forState:UIControlStateNormal];
[button setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[button setBackgroundImage:bgImage forState:UIControlStateDisabled];
}
Then when user tap on an image, u check if this image is currently selected.
If yes - u call turnButtonToDeselected.
If no - u call turnButtonToSelected
- (IBAction)buttonClicked:(id)sender {
myButton = (UIButton *)sender;
self.selectedImage = [_images objectAtIndex:myButton.tag - 1];
[[NSUserDefaults standardUserDefaults] setInteger:myButton.tag forKey:#"selected1"];
if (myButton.tag == self.selectedImageIndex) {
// user tap on a button that was selected, so we need to deselect it
[self turnButtonToDeselected:myButton];
}
else {
// user tap on a button that wasn't selected
// first we check if there is a selected button, if yes we need to deselect it
if (self.selectedImageIndex > 0) {
// get the selected button
UIButton *selectedButton = (UIButton *)[self.view viewWithTag:self.selectedImageIndex];
// deselect it
[self turnButtonToDeselected:selectedButton];
}
// now we can select the button the user tapped on
[self turnButtonToSelected:myButton];
// save the selected index
self.selectedImageIndex = myButton.tag;
}
}
One more thing, in viewDidLoad set the button tag to i+1
Keep a Boolean value for if the button is selected or not. In your interface keep a boolean variable named BOOL selected; Keep track of the value, and manage to save it. Then in your turnButtonToSelected method add this:
- (void)turnButtonToSelected
{
selected = !selected;
if (selected)
{
UIImage *bgImage = [UIImage imageNamed:#"Checkmark.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
[myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
}
else
{
// set button image without the check mark
}
}
This will make sure, if the button was selected previously and now tapped again the checkmark
will be gone.
you need to create button array.And when button click event you need to check button have set checkmark image or not.
-(IBAction)buttonclickevent:(id)sender{
UIButton *btn=sender;
UIButton *currentbutton=[ButtonArray objectAtIndex:btn.tag];
if([currentbutton.currentImage isEqual:[UIImage imageNamed:#"Checkmark.png"]])
[currentbutton setImage:[UIImage imageNamed:#"normal.png"]forState:UIControlStateNormal];
}
else {
[currentbutton setImage:[UIImage imageNamed:#"Checkmark.png"]forState:UIControlStateNormal];
}
}

How to implement delete functionality for images in different positions in iCarousel iphone

I am using iCarousel as shownh here with carousel type Liner Carousel and implemented delete functionalty .I am able to delete the image in carousel and when I attempt to delete any other iomage in the visible screen It is moved to carousel frame and deleted.
I need to delete the image from its original position.
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor blackColor];
carousel = [[iCarousel alloc] initWithFrame:CGRectMake(-130,300, 320, 100)];
carousel.dataSource = self;
carousel.delegate=self;
carousel.type = iCarouselTypeLinear;
carousel.scrollEnabled=YES;
imageView=[[UIImageView alloc]initWithFrame:CGRectMake(60, 50, 200, 200)];
[self.view addSubview:imageView];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImage *image = [imagesArray objectAtIndex:index];
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0, 60, 60);
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.tag=index;
NSLog(#"tag is %d",button.tag);
UIButton *deleteButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
deleteButton.frame= CGRectMake(50, -5, 20 ,20);
UIImage *img = [UIImage imageNamed:#"close.png"];
[deleteButton setImage:img forState:UIControlStateNormal];
[deleteButton addTarget:self action:#selector(deleteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button addSubview:deleteButton];
return button;
}
-(void)deleteButtonClicked:(int )sender
{
NSInteger currentIndex = carousel.currentItemIndex;
[carousel removeItemAtIndex:currentIndex animated:YES];
}
Please help me out.
You shouldn't delete the item at the carousel.currentItemIndex because that is not the item corresponding to the button you clicked, that's just the currently centred item in the carousel.
To get the correct item index for the button you clicked, do this:
-(void)deleteButtonClicked:(id)sender
{
//get the index of the item view containing the button that was clicked
NSInteger index = [carousel indexOfItemViewOrSubview:sender];
//update the data model (always do this first)
[imagesArray removeObjectAtIndex:index];
//remove item from carousel
[carousel removeItemAtIndex:index animated:YES];
}
Try this:
NSInteger index = carousel.currentItemIndex;
[carousel removeItemAtIndex:index animated:YES];
[imagesArray removeObjectAtIndex:index];
add this also:
- (void)awakeFromNib
{
if (self) {
//set up carousel data
wrap = NO;
}
}
or make your
carousel.type = iCarouselTypeCustom;
to
carousel.type = iCarouselTypeLinear;
then implement this: [carousel reloadData];

adding reflection to iCarousel

I tried to add reflection to my icarousel. Firstly my code was like this and it was working wright.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton* button = (UIButton *)view;
if (button == nil)
{
//no button available to recycle, so create new one
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
return button;
}
After changing, my code became like this:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIButton* button = nil;
if (button == nil)
{
view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 200.0f)] autorelease];
//no button available to recycle, so create new one
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
[view update];
return view;
}
Reflection appears. But the problem is when i move the carousel view, images not flowing correctly. Sometimes another carousel view appears.
Do you have any idea what causes that?
Code should be:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIButton* button = nil;
if (view == nil)
{
//no view available to recycle, so create new one
view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 200.0f)] autorelease];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = view.bounds;
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 99;
[view addSubview:button];
}
else
{
button = (UIButton *)[view viewWithTag:99];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
[button setBackgroundImage:image forState:UIControlStateNormal];
[view update];
return view;
}
I use images with reflection built in them... don't need to do anything in code that way.. :p
I found my mistake. I am loading images here:
-(void) getImagesWithThread{
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
//for(int i=0;i<bookCount;i++){
for(int i=0;i<bookCount;i++){
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [arrBookImages objectAtIndex:i]]];
UIImage *image = [[UIImage imageWithData: imageData] retain];
image = [self scaleToSize:CGSizeMake(140, 200) withImage:image];
if(image!=nil) [arrBookImages replaceObjectAtIndex:i withObject:image];
[image release]; }
[carousel reloadData];
[pool drain];
}
When i performed this in background it didn't reload correctly. İf i perform normally, it works. But i want to load them in a thread. I mean first i want to set images and replace them images from url. but i want them to appear one by one. Do you have any idea?

change image on click of button in objective c

i draged 3 button in my .xib file (btn1,btn2,btn3 respectively) and initially i given default image to them, first.png
now when user clicks on btn1, image of btn1 should change from first.png to second.png..
and when user selects on btn2, image of btn2 should change from first.png to second.png, and also change image of btn1 to default again first.png, so that user came to know he has clicked 2nd button.
how should i do this ?
Thanks In Advance !!
You have to set button action method code
-(IBAction)btnClked:(id)sender
{
[btn1 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
[btn2 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
[btn3 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
UIButton *btn=(UIButton *)sender;
[btn setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateNormal];
}
#import <Foundation/Foundation.h>
#interface CustomRadioButton : UIButton {
}
#end
#import "CustomRadioButton.h"
#implementation CustomRadioButton
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Set the refresh icon as the button's image
[self setImage:[UIImage imageNamed:#"off.png"] forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:#"on.png"] forState:UIControlStateSelected];
// When the button is pressed, draw to button with less opacity.
self.adjustsImageWhenHighlighted = YES;
}
return self;
}
#end
inside the Implement file of the viewController
- (void)viewDidLoad {
[super viewDidLoad];
for (int i=1;i<=2;i++){
CustomRadioButton *Radiobutton = [CustomRadioButton buttonWithType:UIButtonTypeCustom];
Radiobutton = [[CustomRadioButton alloc] initWithFrame:CGRectMake(200,50*i, 30, 30)];
[Radiobutton addTarget:self action:#selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
Radiobutton.tag=i;
[self.view addSubview:Radiobutton];
}
}
- (IBAction)checkboxButton:(UIButton *)button{
for (UIButton *Radiobutton in [self.view subviews]) {
if ([Radiobutton isKindOfClass:[UIButton class]] && ![Radiobutton isEqual:button]) {
[Radiobutton setSelected:NO];
}
}
if (!button.selected) {
button.selected = !button.selected;
}
}
Set the images in your button call method like:
BOOL first;
-(void)firstBtnPressed
{
if(first == YES){
[btn1 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
[btn2 setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateNormal];
first = NO;
}
else
{
first = YES;
[btn1 setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateNormal];
[btn2 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
}
}