I've created an app that shows 8 buttons in coverflow using icarousel.i have to assign different action for each button.Here is my piece of code for viewcontroller.m file..
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return NUMBER_OF_ITEMS;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImage *buttonImage=[NSArray arrayWithObjects:[UIImage imageNamed:#"Cover_0.png"],
[UIImage imageNamed:#"Cover_1.png"],
[UIImage imageNamed:#"Cover_2.png"],
[UIImage imageNamed:#"Cover_3.png"],
[UIImage imageNamed:#"Cover_4.png"],
[UIImage imageNamed:#"Cover_5.png"],
[UIImage imageNamed:#"Cover_6.png"],
[UIImage imageNamed:#"Cover_7.png"],nil];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200.0f, 200.0f);
[button setImage:(UIImage*)[buttonImage objectAtIndex:index] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;
}
- (void)buttonTapped:(UIButton *)sender
{
}
Here i've got stuck...now i want to assign different action for each button.but if i declare an action under buttonTapped it gets assigned for all the buttons..Can anyone help?...
additional to my previous question i've added other two buttons and two event to that buttons in xib file and defined method in my .m file...But if i run that on simulator its just displayed as image and cant interact with that button...Any idea pls...
You can ask the carousel which button was pressed like this:
- (void)buttonTapped:(UIButton *)sender
{
NSInteger index = [carousel indexOfItemViewOrSubview:sender];
switch(index) {
case 0:
//do action number 1
break;
case 1:
//do action number 2
break;
etc....
}
}
just you call base on the button tag value.
get button tag value
- (void)buttonTapped:(UIButton *)sender
{
uibutton *btn=(uibutton *) sender;
nslog(#"button tag== %d",btn.tag);
}
Related
I have a UIScrollView full of custom UIButtons that are generated programmatically.
This code executes every iteration through the loop, typically 7 times.
[cardButton
addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[cardButton setTag:i + 100];
[self.scrollView addSubview:cardButton];
Elsewhere I have this function:
- (IBAction) buttonPressed:(id)sender
{
UIButton *button = (UIButton *)sender;
NSLog(#"%d", [button tag]);
}
How do I link the two? My button actually stores all the information I need from it inside its label so I really just need to detect when it is being tapped so I can respond.
You already linked two with this line
[cardButton
addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
Sender is returning the instance of button which you pressed.so
- (IBAction) buttonPressed:(id)sender
{
UIButton *button = (UIButton *)sender;
NSLog(#"%d", [button tag]);
switch (button.tag) {
case 1:
//Action for button with tag 1
break;
case 2:
//Action for button with tag 2
break;
default:
break;
}
}
I am beginner trying to make a menu using iCarousel. What I have tried so far is return a UIButton with a custom image for each view in carousel. The image list has been declared same as told in examples. Problems I am facing are:
button (view returned in carousel) - its image is not adjusting properly as shown in fig.
Also you can see that when I am displaying bear, it is picking description of Tiger!
When I tap any pic, it just scroll forward instead of reacting to button touch down event.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//set up carousel data
wrap = YES;
self.menuItems = [NSMutableArray arrayWithObjects:#"Bear.png",
#"Zebra.png",
#"Tiger.png",
#"Goat.png",
#"Birds.png",
#"Giraffe.png",
nil];
self.descriptions = [NSMutableArray arrayWithObjects:#"Bears Eat: Honey",
#"Zebras Eat: Grass",
#"Tigers Eat: Meat",
#"Goats Eat: Weeds",
#"Birds Eat: Seeds",
#"Giraffes Eat: Trees",
nil];
}
return self;
}
- (void)buttonTapped:(UIButton *)sender
{
NSInteger index = [aCarousel indexOfItemViewOrSubview:sender];
switch (index) {
case 0:
NSLog(#"at index: %d",index);
break;
case 1:
NSLog(#"at index: %d",index);
break;
case 2:
NSLog(#"at index: %d",index);
break;
case 3:
NSLog(#"at index: %d",index);
break;
case 4:
NSLog(#"at index: %d",index);
break;
case 5:
NSLog(#"at index: %d",index);
break;
} }
- (void)viewDidLoad
{
aCarousel.type = iCarouselTypeInvertedRotary;
aCarousel.delegate = self;
aCarousel.dataSource = self;
//aCarousel.frame = CGRectMake(0, 68, 320, 257);
[super viewDidLoad];
}
#pragma - mark iCarousel Delegate
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [self.menuItems count];
}
- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
// limit the number of item views loaded concurrently (for performance)
return 4;
}
- (UIView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0, 0, 240.0f, 200.0f);
[button setImage:[UIImage imageNamed:[menuItems objectAtIndex:index]] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchDown];
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
//button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
return button;
}
- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
return 0;
}
- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
// usually this should be slightly wider than the item views
return 240; //for 220 px image
}
- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
return self.wrap;
}
- (void)carouselDidScroll:(iCarousel *)carousel
{
[label setText:[NSString stringWithFormat:#"%#", [descriptions objectAtIndex:carousel.currentItemIndex]]];
}
I am really fed up trying adjusting carousel for hours. Although I tried aspectfitratio, adjusting frame but don't know why output is wrong. Can please someone figure out where am I doing wrong? Thanks alot :(
Atlast I was able to solve issue by myself. Don't know why my project was behaving so odd, so I decided to use iCarousel in new Project. The mistakes I noted were as follows:
In nib file, when I was making class type to iCarousel for subview, it was showing something like "I Carousel" in objects list - I changed the name in the list manually to my IBOutlet iCarousel object as "aCarousel".
Secondly I wasn't properly assigning UIButton to UIView in -(UIView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view method. I am glad that I found "Button demo" in Examples folder. From there I copied my code and now everything is working awesome! :)
Copying Code here for anybody who wants to see:
- (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 = [UIImage imageNamed:[menuItems objectAtIndex:index]];
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:UIControlEventTouchDown];
}
//set button label
//[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
//Use these commented lines if you are displaying some text on button
return button;
}
I have a custom header view in my iphone app. and it have a custom button over that.
But that custom button action is not fire but header tap event is fired which is in custom class .Please help me .How to fire action on button in header view.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSNumber *num=[NSNumber numberWithInt:section];
UIView * customView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,320,50)]autorelease];
customView.tag = section;
[customView setUserInteractionEnabled:YES];
customView.backgroundColor = [UIColor blackColor];
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(260, 7, 25, 25);
button.tag=section;
[button addTarget:self action:#selector(expandCollapsing:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"add.png"] forState:UIControlEventTouchUpInside];
[button setSelected:YES];
}
You can implement your own callback method to do this.
What i did is I added a TapGesture to the customView. When the event is fired i used a callback method to pass the control to the viewController where my tableView is implemeted
What you can do While loading the header you can assign the tag value to the customView
customHeader.tag = 1;
and you can implement the call back method in your viewController.
In CustomheaderView
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(checkBoxEventHandler:)];
[recognizer setNumberOfTapsRequired:1];
[recognizer setNumberOfTouchesRequired:1];
[self setGestureRecognizers:[NSArray arrayWithObject:recognizer]];
-(IBAction)checkBoxEventHandler:(id)sender
{
if ([self.delegate respondsToSelector:#selector(selectedHeader: atIndex:)])
{
[self.delegate selectedHeader:self atIndex:self.tag];
}
}
In your viewController.m
-(void)selectedHeader:(myCustomHeader *)header atIndex:(int)index
{
//header -> will give you the same instance you created in your viewController while loading this custom header view
// So you can check the tag like this
if(header.tag == 1)
{
//Do Stuff here
}
// index -> we are passing headerViews tag as index ie header.tag is equal to index
// So checking the tag like this is the proper way
if(index == 1)
{
//Do Stuff here
}
}
Just paste this code will solve your problem :
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(50.0, 0.0, 25, 25);
[button setImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
button.tag=section;
[button addTarget:self action:#selector(expandCollapsing:) forControlEvents:UIControlEventTouchUpInside];
[button setSelected:YES];
return button;
}
-(void)expandCollapsing:(id)sender {
UIButton *btn=(UIButton*)sender;
NSLog(#"Tag:%d",[btn tag]);
}
I've successfully added some UIButtons to a custom tableFooterView. The problem is that the button events does not get called. Also, the image relative to the UIControlStateHighlighted doesn't come up. Any ideas?
I've tried all I could think of, but I can't find the trick. I'm reporting here the interesting part of the UITableViewController:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection: (NSInteger)section
{
CGRect footerFrame = CGRectMake(0.0, 0.0, 480, 190);
_footerView = [[UIImageView alloc] initWithFrame: footerFrame];
_footerView.image = [UIImage imageNamed:#"tableviewfooter.png"];
_footerView.contentMode = UIViewContentModeTopLeft;
UIButton *addExam = [self createButtonWithFrame: CGRectMake(10, 30, 460, 58) Target: self Selector: #selector(addNewItem:) Image: #"additem.png" ImagePressed: #"additem_pressed.png"];
[_footerView addSubview:addExam];
return _footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection: (NSInteger) section
{
return 190;
}
- (void) addNewItem:(id)sender
{
// This get's never called
NSLog(#"Pressed");
}
- (UIButton*) createButtonWithFrame: (CGRect) frame Target:(id)target Selector:(SEL)selector Image:(NSString *)image ImagePressed:(NSString *)imagePressed
{
UIButton *button = [[UIButton alloc] initWithFrame:frame];
UIImage *newImage = [UIImage imageNamed: image];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
UIImage *newPressedImage = [UIImage imageNamed: imagePressed];
[button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
return button;
}
Thanks for your help!
I do not think UIImageView was designed to work well as a container for other views, especially UIButtons. For one, UIImageViews, when created, have their userInteractionsEnabled property set to false by default.
I would just create a simple UIView as your footer view and then add both the UIButton and the UIImageView directly to it.
Is there either a way to implement UISwitch with custom graphics for the switch-states?
Or as an alternative the other way round, an UIButton with UISwitch functionality?
UIButton already supports a "switch" functionality.
Just set a different image in Interface Builder for the "Selected State Configuration", and use the selected property of UIButton to toggle its state.
set the image to show on selected state:
[button setImage:[UIImage imageNamed:#"btn_graphics"] forState:UIControlStateSelected];
and then on touch up inside selector, set:
button.selected = YES;
if you want this to cancel another button's selection, set:
otherButton.selected = NO;
To build on what PGB and nurne said above, after you set your states and attach a selector (event method) you would want to put this code in that selector.
- (IBAction)cost:(id)sender
{
//Toggle current state and save
self.buttonTest.selected = !self.buttonTest.selected;
/**
The rest of your method goes here.
*/
}
For programmatically inclined:
-(void) addToggleButton {
CGRect aframe = CGRectMake(0,0,100,100);
UIImage *selectedImage = [UIImage imageNamed:#"selected"];
UIImage *unselectedImage = [UIImage imageNamed:#"unselected"];
self.toggleUIButton = [[UIButton alloc] initWithFrame:aframe];
[self.toggleUIButton setImage:unselectedImage forState:UIControlStateNormal];
[self.toggleUIButton setImage:selectedImage forState:UIControlStateSelected];
[self.toggleUIButton addTarget:self
action:#selector(clickToggle:)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.toggleUIButton];
}
-(void) clickToggle:(id) sender {
BOOL isSelected = [(UIButton *)sender isSelected];
[(UIButton *) sender setSelected:!isSelected];
}