iCarousel buttonsDemo 'UIImage' may not respond to 'objectAtIndex' - ios

Can someone help me, please? I am trying to make a carousel of button - every button has a different background image, but I failed to do it.
Finally it functions, but I donĀ“t know, why I got always a warning (on
[image objectAtIndex:0], [image objectAtIndex:1], [image objectAtIndex:2]
):
'UIImage' may not respond to 'objectAtIndex'.
Thank you very much!
My source code of iCarouselExampleViewController.m:
#import "iCarouselExampleViewController.h"
#implementation iCarouselExampleViewController
#synthesize carousel;
- (void)dealloc
{
carousel.delegate = nil;
carousel.dataSource = nil;
[carousel release];
[super dealloc];
}
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
//configure carousel
carousel.type = iCarouselTypeCoverFlow2;
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.carousel = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#pragma mark -
#pragma mark iCarousel methods
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
//generate 100 buttons
//normally we'd use a backing array
//as shown in the basic iOS example
//but for this example we haven't bothered
return 5;
}
- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
//limit the number of items views loaded concurrently (for performance reasons)
return 5;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = (UIButton *)view;
if (button == nil)
{
UIImage *image=[NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],nil];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200.0f, 200.0f);
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
switch (index) {
case 0:
{
[button setImage:(UIImage*)[image objectAtIndex:0] forState:UIControlStateNormal];
}
break;
case 1:
{
[button setImage:(UIImage*)[image objectAtIndex:1] forState:UIControlStateNormal];
break;
}
case 2:
{
[button setImage:(UIImage*)[image objectAtIndex:2] forState:UIControlStateNormal];
}
break;
}
}
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;
}
#pragma mark -
#pragma mark Button tap event
- (void)buttonTapped:(UIButton *)sender
{
//get item index for button
NSInteger index = [carousel indexOfItemViewOrSubview:sender];
[[[[UIAlertView alloc] initWithTitle:#"Button Tapped"
message:[NSString stringWithFormat:#"You tapped button number %i", index]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease] show];
}
#end
#end

because a UIImage isnt an NSArray
but you only DECLARED it a UIImage hence no crash
UIImage *image=[NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],nil];
change it so an array is declared
NSArray *image=[NSArray arrayWithObjects:...
and while at it, rename image to images and use the new syntax
NSArray *images = #[img1,img2,img3];

Related

iCarousel Sticks during screen transition

I am using an iCarousel. The carousel contains about 10 images. While screen transitions the images get stuck to the next screen. I am using popToRootview while going back to the original screen. Does it have to do with the UIImage alloc ?
My Code:
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return ([animals count]);
}
- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
//limit the number of items views loaded concurrently (for performance reasons)
return 7;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
NSString *filePath1 = [animals objectAtIndex:index];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath1];
//create a numbered view
UIView *view = [[UIImageView alloc] initWithImage:image];
view.frame = CGRectMake(0.0f, 0.0f, 230, 150);
return view;
}
- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
//note: placeholder views are only displayed on some carousels if wrapping is disabled
return 0;
}
- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
//usually this should be slightly wider than the item views
return 240;
}
- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
//wrap all carousels
return wrap;
}
- (void)carouselDidEndScrollingAnimation:(iCarousel *)aCarousel
{
label.text = [descriptions objectAtIndex:aCarousel.currentItemIndex];
[_label1 setText:[NSString stringWithFormat:#"%ld/%lu", (long)aCarousel.currentItemIndex+1,(unsigned long)[animals count]]];
}
- (IBAction)btnBackAct:(UIButton *)sender
{
[carousel removeFromSuperview];
//[carousel scrollToItemBoundary];
[carousel scrollToItemAtIndex:0 animated:NO];
[self.navigationController popViewControllerAnimated:YES];
}
try:
- (IBAction)btnBackAct:(UIButton *)sender
{
[carousel scrollToItemAtIndex:0 duration:0.01];
[self performSelector:#selector(popMyView) withObject:nil afterDelay:0.02];
}
-(void)popMyView
{
[self.navigationController popViewControllerAnimated:YES];
}

IBAction addTarget:action:forControlEvents: not working when touched for the first time, otherwise fine

I am developing a calculator app and I am trying to highlight touched buttons by changing the background color. My problem is that when I touch the button for the first time, it displays the numbers but the background color does not change. But later on it works fine, just the first touch does not work at all. I appreciate any help!
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <AudioToolbox/AudioToolbox.h>
#interface ViewController ()
#end
#implementation ViewController
#synthesize resultLabel;
BOOL isDecimal;
NSArray *array;
NSNumber *number;
NSNumberFormatter *formatter;
- (void)viewDidLoad
{
[super viewDidLoad];
array = [[NSArray alloc]initWithObjects:ACbuttonOutlet, deleteButtonOutlet, plusMinusOutlet, divideOutlet, number7Outlet, number8Outlet, number9Outlet, multiplicationOutlet, number4Outlet, number5Outlet, number6Outlet, plusOutlet, number1Outlet, number2Outlet, number3Outlet, minusOutlet, number0Outlet, decimalOutlet, resultOutlet, nil];
for(UIButton *button in array)
{
[button.layer setBorderWidth:0.25];
[button.layer setBorderColor:[UIColor blackColor].CGColor];
}
resultLabel.adjustsFontSizeToFitWidth = YES;
/*formatter = [[NSNumberFormatter alloc]init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setGroupingSize:3];
[formatter setGroupingSeparator:#" "];
number = [NSNumber numberWithInt:[resultLabel.text intValue]];
resultLabel.text = [formatter stringFromNumber:number];*/
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)highlightButton:(UIButton *)button
{
AudioServicesPlaySystemSound(0x450);
[button setBackgroundColor:[UIColor colorWithRed:239/255.0 green:232/255.0 blue:232/255.0 alpha:1]];
NSLog(#"1");
}
- (void)resetButtonBackGroundColor: (UIButton *)sender{
[sender setBackgroundColor:[UIColor whiteColor]];
NSLog(#"2");
}
- (IBAction)ACbutton:(id)sender {
[sender addTarget:self action:#selector(highlightButton:) forControlEvents:UIControlEventTouchDown];
[sender addTarget:self action:#selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];
isDecimal = NO;
Method = 0;
SelectNumber = 0;
RunningTotal = 0;
resultLabel.text = [NSString stringWithFormat:#"0"];
}
Try to add this line in viewDidLoad method into your cycle
for(UIButton *button in array)
{
[button.layer setBorderWidth:0.25];
[button.layer setBorderColor:[UIColor blackColor].CGColor];
[button addTarget:self action:#selector(highlightButton:) forControlEvents:UIControlEventTouchDown];
}
I would suggest you not to work upon background color of the button . Instead toggle/change the background image of the button.

How to create a segue to another view from a button in iCarousel

I'm using an iCarousel where i display some buttons. But how do I get to another view when the button is tapped?
I have this code already:
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return 10;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = (UIButton *)view;
if (button == nil)
{
UIImage *image = [UIImage imageNamed:#"page.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
return button;
}
- (void)buttonTapped:(UIButton *)sender
{
NSInteger index = [carousel indexOfItemViewOrSubview:sender];
//Action
}
How do I get to another view when the button is tapped? Can I use the prepareForSegue method here as well?
Thanks for your help!!
Sure, you just have to drag and create a segue from the first viewController to the second, and name it "segue0" for instance.
-(void)buttonTapped:(UIButton*)sender
{
NSInteger *index = [carousel indexOfItemViewOrSubview:sender];
if(index = 0)
{
[self performSegueWithIdentifier:#"segue0", sender:self];
}
//else if etc.., or better yet, ditch the if, do the checking in 'prepare' as explained below
}
-(void)prepareForSegue:(UIStoryboardSegue*) segue sender:(id)sender
{
YourNextViewController *nextView = [segue destinationViewController];
//Do stuff to your next view with "nextView"
}
If they are all going to the same viewController, but by sending different data in prepareForSegue, then you can ditch the if-statement, and send sender instead of self as the sender in [self performSegue..], and check in prepareForSegue.. which button is the sender(or moving your NSInteger *index... line down here)

UIScrollView programmatically add buttons and scroll

I'm new to ios and Objective-C. Please be a little bit patient with me.
THe functionality in my app, which I'm trying to develop, is a small scrolling toolbar, which contains several buttons, state toggling buttons. Also there is another button above the toolbar which slides in and out the toolbar. The second functionality I achieved successfully, using a view for the whole setup and when the slide button touch up event animates the whole view up and down.
For the scrollview, I wanted to add buttons programmatically, hence I drew an outlet to the class and tried adding a self created button as a subview. However I cannot see the button. Nor can I observe the scrollview sliding.
THe following is the way I'm adding the button:
Also, my total view for the above functionality is very less in size than the total view controller
UIButton *button = [[UIButton alloc] init];
button.titleLabel.text = #"hello";
[_scrollTop setContentSize:self.view.frame.size];
[_scrollTop addSubview:button];
The two images down, I hope, should help you understand the functionality I'm trying to develop. If anybody is aware of existence of similar functionality, please do direct me.
Edit: Code totally is
//
// HCILocationViewController.m
// app2
//
// Created by Ravi Vooda on 13/03/13.
// Copyright (c) 2013 housing.co.in. All rights reserved.
//
#import "HCILocationViewController.h"
#import "HCIAnnotationViewController.h"
#interface HCILocationViewController ()
#property int widMap;
#property BOOL showExploreOptions;
#end
#implementation HCILocationViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_mapLocationView.delegate = self;
MKPointAnnotation *annotation = [[HCIAnnotationViewController alloc]
initwithHouse:[self singleHome]];
[_mapLocationView addAnnotation:annotation];
_widMap = 10000;
_showExploreOptions = NO;
/*
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);//give values whatever you want.
[_scrollTop addSubview:button];
*/
[self addButtonsToScrollView];
}
- (void)addButtonsToScrollView
{
NSInteger buttonCount = 4;
CGRect buttonFrame = CGRectMake(5.0f, 5.0f, self.scrollTop.frame.size.width-10.0f, 40.0f);
for (int index = 0; index <buttonCount; index++) {
UIButton *button = [[UIButton alloc]initWithFrame:buttonFrame];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:index+1];
NSString *title = [NSString stringWithFormat:#"Button %d",index+1];
[button setTitle:title forState:UIControlStateNormal];
[self.scrollTop addSubview:button];
buttonFrame.origin.y+=buttonFrame.size.height+5.0f;
}
CGSize contentSize = self.scrollTop.frame.size;
contentSize.height = buttonFrame.origin.y;
[self.scrollTop setContentSize:contentSize];
}
- (void)buttonPressed:(UIButton *)button
{
NSLog(#"button pressed");
switch (button.tag) {
case 1:
//Do something
break;
default:
break;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
NSString *identifier = #"currDetailsIdentifier";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[_mapLocationView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
return annotationView;
}
-(void) mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in mapView.annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(
annotationPoint.x - (_widMap / 2),
annotationPoint.y - (_widMap/2),
_widMap,
_widMap);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
} else {
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
[mapView setVisibleMapRect:zoomRect animated:YES];
}
-(void) setMyHouse:(NSDictionary *)singleHouse {
self.singleHome = singleHouse;
}
- (IBAction)toggleExploreOptionsView:(UIButton *)sender {
sender.selected = !sender.selected;
if (_showExploreOptions){
NSLog(#"Close Options");
[UIView animateWithDuration:0.2 animations:^{
_exploreView.frame = CGRectMake(_exploreView.frame.origin.x, _exploreView.frame.origin.y + _exploreOptionsView.frame.size.height, _exploreView.frame.size.width, _exploreView.frame.size.height + _exploreOptionsView.frame.size.height);
}];
}
else {
NSLog(#"Open Options");
[UIView animateWithDuration:0.2 animations:^{
_exploreView.frame = CGRectMake(_exploreView.frame.origin.x, _exploreView.frame.origin.y - _exploreOptionsView.frame.size.height, _exploreView.frame.size.width, _exploreView.frame.size.height - _exploreOptionsView.frame.size.height);
}];
}
_showExploreOptions = !_showExploreOptions;
}
#end
If I understood your question correctly you need to have view which should show up and collapse on a button click event. When the view is shown, it should have a scrollable list of buttons. And you are asking help to show many buttons to scrollView.
Demo Source Code
- (void)addButtonsToScrollView
{
NSInteger buttonCount = 5;
CGRect buttonFrame = CGRectMake(5.0f, 5.0f, self.scrollTop.frame.size.width-10.0f, 40.0f);
for (int index = 0; index <buttonCount; index++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:buttonFrame];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:index+1];
NSString *title = [NSString stringWithFormat:#"Button %d",index+1];
[button setTitle:title forState:UIControlStateNormal];
[self.scrollTop addSubview:button];
buttonFrame.origin.y+=buttonFrame.size.height+5.0f;
}
CGSize contentSize = self.scrollTop.frame.size;
contentSize.height = buttonFrame.origin.y;
[self.scrollTop setContentSize:contentSize];
}
- (void)buttonPressed:(UIButton *)button
{
switch (button.tag) {
case 1:
//Do something
break;
default:
break;
}
}

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