iCarousel issue - ios

I'm trying to add a iCarousel to an existing project with the code below, but I'm getting this error: MemberCatalog[14990:1a903] -[MainViewController carousel:viewForItemAtIndex:reusingView:]: unrecognized selector sent to instance 0xa0589b0.
The line in iCarousel.m throwing the error is: view = [_dataSource carousel:self viewForItemAtIndex:index reusingView:[self dequeueItemView]];
Any help it will be really appreciate it.
carouselItems = [NSArray arrayWithObjects:
[UIImage imageNamed:#"iLibrary+Featured_AM-RAH.png"],
[UIImage imageNamed:#"iLibrary+Featured_CCA.png"],
[UIImage imageNamed:#"iLibrary+Featured_GI-PA.png"],
nil];
// Initialize and configure the carousel
carousel = [[iCarousel alloc] initWithFrame:self.view.bounds];
carousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
carousel.type = iCarouselTypeCoverFlow2;
carousel.delegate = self;
carousel.dataSource = self;
[self.view addSubview:carousel];
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
UIImage *image = [carouselItems objectAtIndex:index];
UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)] autorelease];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
button.tag=index;
return button;
}

You're implementing a -carousel:viewForItemAtIndex: method, but the view expects a -carousel:viewForItemAtIndex:reusingView: method.
You need to update your method with the reusingView:(UIView *)view parameter and you'll be fine. You should also use that reusable view instead of allocating more and more views, but that's another story.

Related

How to Add a button into iCarousel View?

I am using iCarousel for showing an image. But I want to add a button into iCarousel. I implemented it but problem is that custom button action does not work, only didSelectItem work when I click on the button. here is my code. Please, any one helps me out.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
NSString *str=[[appController
sharedappController].allLibraryImagesArrM objectAtIndex:index];
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];
((UIImageView *)view).image = [[appController sharedappController].allLibraryImagesArrM objectAtIndex:index];
view.contentMode = UIViewContentModeScaleAspectFit;
//[view addSubview:cross];
UIButton *cross = [[UIButton alloc]init];
cross.frame = CGRectMake(view.frame.size.width/2+30, view.frame.origin.y+30, 40, 40);
[cross setBackgroundImage:[UIImage imageNamed:#"circle"] forState:UIControlStateNormal];
[cross addTarget:self action:#selector(crossMethod) forControlEvents:UIControlEventTouchUpInside];
UIImage *buttonImage = [UIImage imageNamed:#"cross.png"];
[cross setBackgroundImage:buttonImage forState:UIControlStateNormal];
[view addSubview:cross];
return view;
}
Did you enable UserInteraction?
Enable UserInteraction
[view setUserInteractionEnabled:YES];

UIButton image cannot set properly by taking names from the NSArray

Hello I have a problem like this. In my ViewController I have a delegate method like this
`
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
imgArray=[NSArray arrayWithObjects:#"water-dark.png",#"land-dark.png",#"animal-dark.png",#"air-dark.png",#"other-dark.png", nil];
button = (UIButton *)view;
if (button == nil)
{
//no button available to recycle, so create new one
//UIImage *image = [UIImage imageNamed:#"page.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, 50, 50);
button.layer.cornerRadius=button.frame.size.width/2.0;
button.layer.borderWidth=2;
button.clipsToBounds = YES;
button.titleLabel.font = [button.titleLabel.font fontWithSize:15];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
NSString *strIndex=[NSString stringWithFormat:#"%i",index];
[button setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#", [imgArray objectAtIndex:[strIndex intValue]]]] forState:UIControlStateNormal];
NSLog(#" CURRENT INDEX %i",index);
return button;
}
`
Here Im lacing 5 buttons on viewcontroller. Those 5 buttons titles are setting perfectly. I put the index number as the title. And now I want to get the image name from theimgArray` and set each image to the button. But the problem is
it set only 0th button image and the 4th(5th) button image only. But when I try to print the image name by taking from the array. All print on each button properly.
What should be the reason for this? And how can I solve this problem. Please help me
This is the output that I get currently. But I need to fill all 5 buttons with images in the array.
Hey First Of all There is no need to take a Button and its click event instead you can just return view in - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view in this method as I listed beolw.
and ya of course you want its click event instead you can use its one of the delegates - (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
This is your main method viewForItemAtIndex()
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
((UIImageView *)view).image = [UIImage imageNamed:[imgArray objectAtIndex:index]];
view.alpha = 1.0;
[view.layer setCornerRadius:125];
[((UIImageView *)view).layer setMasksToBounds:YES];
[((UIImageView *)view) setContentMode:UIViewContentModeScaleAspectFit];
view.contentMode = UIViewContentModeCenter;
return view;
}
Here the arrImages is declared in .h file of your view controller and assigning it in viewDidLoad() method i.e. imgArray = [NSArray arrayWithObjects:#"water-dark.png",#"land-dark.png",#"animal-dark.png",#"air-dark.png",#"other-dark.png", nil];
and for knowledge it is not necessary to write or assign array of images with .png after every image name like you can also write imgArray = [NSArray arrayWithObjects:#"water-dark",#"land-dark",#"animal-dark",#"air-dark",#"other-dark", nil];

ChildViewController not able to modify its UI after creation

I have a view controller which is being added as a child view controller. The problem is that after the child controller creates its view, I am not able to modify it.
It loads everything properly, but when I want to hide a text field on clicking a button (textfield.hidden = YES), nothing happens!
This is my loadView :
-(void)loadView {
self.waitingPopup = [[WaitingPopupViewController alloc]initWithLabel:#"loading salon"];
self.view=[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
// self.view.frame=CGRectMake(0, -20, self.view.frame.size.width, self.view.frame.size.height);
scrollableTable=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scrollableTable.delegate=self;
scrollableTable.dataSource=self;
[self.view addSubview:scrollableTable];
headerHolder=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-44)];
//creating and adding backgroundImage view container and setting the image
// saloonHomeImage.backgroundColor = [UIColor grayColor];
saloonHomeImage.frame=CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, self.view.frame.size.height);
saloonHomeImage.contentMode = UIViewContentModeScaleAspectFill;
//[saloonHomeImage setBackgroundColor:[UIColor redColor]];
[headerHolder setBackgroundColor:[UIColor grayColor]];
[headerHolder addSubview:saloonHomeImage];
//creating and adding saloonheader label
saloonLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 32, self.view.frame.size.width, 48)];
saloonLabel.text=#"";
saloonLabel.textAlignment=NSTextAlignmentCenter;
[saloonLabel setTextColor:[UIColor whiteColor]];
[saloonLabel setFont:[UIFont fontWithName:#"vevey" size:48]];
[headerHolder addSubview:saloonLabel];
//creating and adding book appointment button
UIButton *bookAppointmentButton=[UIButton buttonWithType:UIButtonTypeSystem];
NSLog(#"%f", self.view.frame.size.height-97.5);
bookAppointmentButton.frame=CGRectMake(self.view.frame.origin.x, self.view.frame.size.height-97.5, 216, 53);
[bookAppointmentButton setBackgroundImage:[UIImage imageNamed:#"bg-left.png"] forState:UIControlStateNormal];
[bookAppointmentButton setTitle:#"BOOK APPOINTMENT" forState:UIControlStateNormal];
[bookAppointmentButton addTarget:self action:#selector(bookAppointmentAction) forControlEvents:UIControlEventTouchUpInside];
[bookAppointmentButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[headerHolder addSubview:bookAppointmentButton];
UIButton *callButton=[UIButton buttonWithType:UIButtonTypeSystem];
callButton.frame=CGRectMake(216, self.view.frame.size.height-97.5, self.view.frame.size.width-216, 53);
[callButton setBackgroundImage:[UIImage imageNamed:#"bg-right.png"] forState:UIControlStateNormal];
[callButton addTarget:self action:#selector(callButtonAction) forControlEvents:UIControlEventTouchUpInside];
[callButton setTitle:#"CALL" forState:UIControlStateNormal];
[callButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[headerHolder addSubview:callButton];
[self getSalonDetails];
loading=[[UIImageView alloc] init];
loading.frame=CGRectMake(130,200,40,36);
[loading setBackgroundColor:[UIColor clearColor]];
loading.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"spinner-1.png"],
[UIImage imageNamed:#"spinner-2.png"],
[UIImage imageNamed:#"spinner-3.png"],
[UIImage imageNamed:#"spinner-4.png"],
[UIImage imageNamed:#"spinner-5.png"],
[UIImage imageNamed:#"spinner-6.png"],
[UIImage imageNamed:#"spinner-7.png"],
[UIImage imageNamed:#"spinner-8.png"],[UIImage imageNamed:#"spinner-9.png"],nil];
loading.animationDuration = 1.0f;
loading.animationRepeatCount = 0;
[loading startAnimating];
[self.view addSubview:loading];
loadingText = [[UILabel alloc]initWithFrame:CGRectMake(90, 240, 200, 40)];
loadingText.text = #"loading salon";
[loadingText setFont:GOTHIC_FONT(22)];
[loadingText setTextColor:[UIColor whiteColor]];
[self.view addSubview:loadingText];
articles=[Model getArticles];
}
- (void) getSalonDetails{
[[MyDataModel sharedDataModel] getSalonDetails];
}
This is the delegate method which is trying to set some fields hidden. This delegate is called when an NSURLSession call completes successfully. The control comes here as per logs but nothing gets hidden. When I add a breakpoint and inspect the fields, every object is shown as nil. If this class is used as the main view controller, I've found everything works. On making it a child view, nothing works.
-(void)salonDetailsRecievedDelegate:(BOOL)success andStatusCode:(int)statusCode{
[loading stopAnimating];
self.loading.hidden = YES;
self.loadingText.hidden = YES;
saloonLabel.text = #"this is texT";
if(success){
//
}
}
What I think here is that salonDetailsRecievedDelegate isn't called from the main thread, try any of the following:
1) call it in the main thread from your model as follows:
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate salonDetailsRecievedDelegate:success andStatusCode:status];
});
2) change its implementation as follows:
-(void)salonDetailsRecievedDelegate:(BOOL)success andStatusCode:(int)statusCode{
dispatch_async(dispatch_get_main_queue(), ^{
[loading stopAnimating];
self.loading.hidden = YES;
self.loadingText.hidden = YES;
saloonLabel.text = #"this is texT";
if(success){
//
}
});
}

How to get index of uiimage subview of uiscrollview

I need to detect selected image of uiimage which is placed inside scrollview.
My code is here:
int newscrollviewY = 40;
for(int i = 0; i<1; i++)
{
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(5, newscrollviewY, 310, 100)];
scrollView.showsVerticalScrollIndicator = YES;
scrollView.scrollEnabled = YES;
scrollView.userInteractionEnabled = YES;
scrollView.backgroundColor = [UIColor clearColor];
scrollView.delegate=self;
int imgx = 1;
for(int img=0; img<[images count]; img++)
{
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(imgx,0,100,100)];
imageView1.backgroundColor = [UIColor whiteColor];
imageView1.image = [images objectAtIndex:img];
imageView1.contentMode = UIViewContentModeScaleAspectFit;
[imageView1 setNeedsDisplay];
[imageView1 setUserInteractionEnabled:YES];
[imageView1 setMultipleTouchEnabled:YES];
[scrollView addSubview:imageView1];
imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton.frame = CGRectMake(imgx,0,100,100);
[imageButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
imageButton.tag = img;
[scrollView addSubview:imageButton];
imgx = imgx + 103;
}
Please help to detect selected image using imageButton or something else.
Replace your selector with buttonClicked:
be Sure that you are adding ":" after selector method. follow or replace the code below:
[imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
then Add the selector definantion in .m file
- (void) buttonClicked: (id) sender
{
// here you can get the tag of button.
NSInteger tag = ((UIButton *)sender).tag;
// do your implementation after this
}
Hope this will solve your issue. Enjoy Coding..!!
They way you have set it up the tag of the 'button' is already the number of the image in your array of images. So all you need to do is get the tag from the 'button'.
Make sure the button clicked function retrieves the tag from the sender(id) that you receive.
For example a bit like this:
- (IBAction)buttonClicked:(id)sender {
UIButton *button = (UIButton *)sender;
UIImage selectedImages = [images objectForKey: [button tag]];
}
As par you question if you are using UIImageview you have to use UITapGestureRecognizer that same like Button. you just need to add UITapGestureRecognizer like:-
for(int img=0; img<[images count]; img++)
{
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(imgx,0,100,100)];
imageView1.backgroundColor = [UIColor whiteColor];
UITapGestureRecognizer *frameTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(frameTapGesture:)];
frameTapGesture.numberOfTapsRequired=1;
imageView1.image = [images objectAtIndex:img];
imageView1.contentMode = UIViewContentModeScaleAspectFit;
[imageView1 setNeedsDisplay];
[imageView1 setUserInteractionEnabled:YES];
[imageView1 setMultipleTouchEnabled:YES];
imageView1.tag=img;
[_imgView addGestureRecognizer:frameTapGesture];
[scrollView addSubview:imageView1];
}.
- (void)frameTapGesture:(UITapGestureRecognizer*)sender
{
UIView *view = sender.view; //cast pointer to the derived class if needed
NSLog(#"%d", view.tag);
}
Reduce your code There are No need to add Button with same Frame of ImageView For getting Click Event or you can also set Image of UIButton for you want to like use UIBUtton then remove UIImageview Code else you simply use UITapGestureRecognizer with getting click event using - (void)frameTapGesture:(UITapGestureRecognizer*)sender
Please try this code
for(int img=0; img<[images count]; img++)
{
imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setImage:[UIImage imageNamed:[images objectAtIndex:i]] forState:UIControlStateNormal];
imageButton.frame = CGRectMake(imgx,0,100,100);
[imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
imageButton.tag = img;
[scrollView addSubview:imageButton];
imgx = imgx + 103;
}
- (void) buttonClicked: (id) sender
{
UIButton *button = (UIButton *)sender;
UIImage selectedImage = button.currentImage;
}
set button target
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
// get Button Action
-(void) buttonAction:(UIButton *)sender
{
NSLog(#"%dl",sender.tag);
// do your implementation after this
}

iCarouselTypeLinear with more than 6 images

for some reason when I set iCarousel.type = iCarouselTypeLinear, I can't use more than 6 images. When I try to scroll I get the following error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0xa47c2b0'
What am I missing?
Any help will be really appreciated.
Controller Code:
journalCarouselItems = [NSMutableArray arrayWithObjects:
[UIImage imageNamed:#"Icon-JCO1.png"],
[UIImage imageNamed:#"Icon-JCO2.png"],
[UIImage imageNamed:#"Icon-JCO3.png"],
[UIImage imageNamed:#"Icon-JCO4.png"],
[UIImage imageNamed:#"Icon-JCO5.png"],
[UIImage imageNamed:#"Icon-JCO6.png"],
[UIImage imageNamed:#"Icon-JCO7.png"],
[UIImage imageNamed:#"Icon-JCO8.png"],
[UIImage imageNamed:#"Icon-JCO9.png"],
[UIImage imageNamed:#"Icon-JCO10.png"],
[UIImage imageNamed:#"Icon-JCO11.png"],
nil];
// Initialize and configure the carousel
carouselId = 2;
journalCarousel = [[iCarousel alloc] initWithFrame: CGRectMake ( 10, 340, 748, 240)];
journalCarousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
journalCarousel.type = iCarouselTypeLinear;
journalCarousel.delegate = self;
journalCarousel.dataSource = self;
[self.view addSubview:journalCarousel];
EDIT: While debugging I notice that when I have 6 images the function below is called every time and the index value goes from 0 to 5, but when I have more than 6 images the same function is not called for each image, the index values are 0,2,8,9.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIImage *image = [journalCarouselItems objectAtIndex:index];
UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)] autorelease];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
button.layer.cornerRadius = 8.0f;
button.tag=index;
[view addSubview:button];
return view;
}
I think the issue may be coming from the initial declaration of the journalCarouselItems array. That error message means that you're sending an objectAtIndex: messages to an object of type NSString (or possibly CFString). You are then expecing your journalCarouselItems to be an array, but it is probably being released (almost certainly autoreleased) too early and that by the time the method above is called the memory that was being pointed at by journalCarouselItems now holds something different.
Try retaining the array at the very end of its declaration to ensure it stays at the correct memory location.
journalCarouselItems = [[NSMutableArray arrayWithObjects:
[UIImage imageNamed:#"Icon-JCO1.png"],
[UIImage imageNamed:#"Icon-JCO2.png"],
[UIImage imageNamed:#"Icon-JCO3.png"],
[UIImage imageNamed:#"Icon-JCO4.png"],
[UIImage imageNamed:#"Icon-JCO5.png"],
[UIImage imageNamed:#"Icon-JCO6.png"],
[UIImage imageNamed:#"Icon-JCO7.png"],
[UIImage imageNamed:#"Icon-JCO8.png"],
[UIImage imageNamed:#"Icon-JCO9.png"],
[UIImage imageNamed:#"Icon-JCO10.png"],
[UIImage imageNamed:#"Icon-JCO11.png"],
nil] retain];

Resources