Objective C : Adding Action On Array of Buttons - objective-c

i'm a newbie in Objective-C can someone help me out here? I am trying to make a carousel of button but i failed to do it.
What i got it just displaying all the buttons on load and that's it. I can't add action on each button, I mean tagging them.
It's my first to do declare button in this way.
The buttons are not working.
Here's my code:
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor grayColor];
mainHolder = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"feat.png"]];
[mainHolder setUserInteractionEnabled:YES];
[mainHolder setFrame: CGRectMake(0, 0, 1024, 768)];
[self.view addSubview: mainHolder];
header = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"header.png"]];
[header setFrame:CGRectMake(0, 0, 1024, 50)];
[header setUserInteractionEnabled:YES];
[mainHolder addSubview:header];
// The items to be displayed in the carousel
items = [NSArray arrayWithObjects:
[UIImage imageNamed:#"picOne.png"],
[UIImage imageNamed:#"picTwo.png"],
[UIImage imageNamed:#"picThree.png"],
[UIImage imageNamed:#"picFour.png"],
[UIImage imageNamed:#"picFive.png"],
[UIImage imageNamed:#"picSix.png"],
[UIImage imageNamed:#"picSeven.png"],
[UIImage imageNamed:#"picEight.png"],
[UIImage imageNamed:#"picNine.png"],
[UIImage imageNamed:#"picTen.png"],
[UIImage imageNamed:#"picEleven.png"],
nil];
// Initialize and configure the carousel
carousel = [[iCarousel alloc] initWithFrame:self.view.bounds];
carousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
carousel.type = iCarouselTypeCoverFlow2;
carousel.dataSource = self;
[self.view addSubview:carousel];
}
#pragma mark -
#pragma mark iCarousel methods
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [items count];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
UIImage *image = [items objectAtIndex:index];
imageChoices = [[[UIButton alloc] initWithFrame:CGRectMake(253, 150, 518, 389)] autorelease];
[imageChoices setBackgroundImage:image forState:UIControlStateNormal];
[imageChoices setUserInteractionEnabled:YES];
[imageChoices setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
imageChoices.titleLabel.font = [imageChoices.titleLabel.font fontWithSize:50];
[imageChoices addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
imageChoices.tag=index;
return imageChoices;
}
- (void)buttonTapped:(UIButton *)sender
{
//I don't know how to tag each button here.
switch(sender.tag){
case 1:{
NSLog(#"Photo1");
}
break;
case 2:{
NSLog(#"Photo2");
}
case 3:{
NSLog(#"Photo3");
}
break;
case 4:{
NSLog(#"Photo4");
}
break;
case 5:{
NSLog(#"Photo5");
}
break;
}
}

Implement the target method this way:
- (IBAction)buttonTapped:(id)sender
{
switch(sender.tag){
case 1:{
NSLog(#"Photo1");
break;
}
case 2:{
NSLog(#"Photo2");
break;
}
case 3:{
NSLog(#"Photo3");
break;
}
case 4:{
NSLog(#"Photo4");
break;
}
case 5:{
NSLog(#"Photo5");
break;
}
default:{
NSLog(#"Default");
break;
}
}
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
UIImage *image = [items objectAtIndex:index];
//Instead of adding single UIButton, add separate for each carousel.
UIButton *imageChoices = [[[UIButton alloc] initWithFrame:CGRectMake(253, 150, 518, 389)] autorelease];
[imageChoices setBackgroundImage:image forState:UIControlStateNormal];
[imageChoices setUserInteractionEnabled:YES];
[imageChoices setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
imageChoices.titleLabel.font = [imageChoices.titleLabel.font fontWithSize:50];
[imageChoices addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
imageChoices.tag=index; //print index, that it should be unique or not?
return imageChoices;
}

Try this
- (void)buttonTapped:(UIButton *)sender
{
UIButton *sendButton = (UIButton *)sender; //No need of this! We already have sender
switch(sendButton.tag)
{
//.......
}
}

add carousel.delegate = self; In your loadview method and then implement
- (void)carousel:(iCarousel *)crsl didSelectItemAtIndex:(NSInteger)index { }

Related

remove objects from array based on the index

I have array of images here i am doing check and uncheck the images finally i have delete button which are the images i check i need to remove the images from array based on the button selection
here is my code
- (IBAction)delete_btn_touch:(id)sender {
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
for(int i=0; i<_GalleryimageArry.count;i++){
_imageV = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) * i/2*0.6, 0, 100, 100)];
self.Crossbtn =[[UIButton alloc]initWithFrame:CGRectMake(_imageV.image.size.width/2+80,0,20,20)];
UIImage *btnImage = [UIImage imageNamed:#"unselectimag"];
[self.Crossbtn setImage:btnImage forState:UIControlStateNormal];
_imageV.contentMode = UIViewContentModeScaleAspectFill;
_imageV.image = [_GalleryimageArry objectAtIndex:i];
[_imageV setUserInteractionEnabled:YES];
_imageV.tag = i;
self.Crossbtn.tag =i;
[self.Crossbtn addTarget:self action:#selector(deleteImage:) forControlEvents:UIControlEventTouchUpInside];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapMethod:)];
[_imageV addGestureRecognizer:tap];
[self.imageV addSubview:self.Crossbtn];
[self.imageGalleryscroll addSubview:_imageV];
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}
self.imageGalleryscroll.delegate = self;
index=0;
self.imageGalleryscroll.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame) * _GalleryimageArry.count/2*0.7, CGRectGetHeight(self.imageGalleryscroll.frame));
[self.imageGalleryscroll setPagingEnabled:YES];
[self.imageGalleryscroll setShowsHorizontalScrollIndicator:NO];
self.imageGalleryscroll.alwaysBounceVertical = NO;
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
-(void)deleteImage:(UIButton *)sender{
btn = (UIButton *)sender;
self.add_delete_view.hidden =YES;
self.deleteview.hidden=NO;
if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"unselectimag"]])
{
[btn setImage:[UIImage imageNamed:#"Crossimg"] forState:UIControlStateNormal];
[btn setSelected:YES];
[btn setTag:index];
}
else
{
[btn setImage:[UIImage imageNamed:#"unselectimag"] forState:UIControlStateNormal];
// other statements
self.selectedimages =#"No";
image = [self.GalleryimageArry objectAtIndex:index];
[self.GalleryimageArry addObject:image];
}
NSLog(#"%#", self.GalleryimageArry);
}
- (IBAction)delete_all_img_btn_touch:(id)sender {
// self.view (change it with your button superview)
if (btn.selected) {
[self.GalleryimageArry removeObjectAtIndex:index];
}
//here i need to removeObjectAtIndex:index based on the how many button are selected
}
any one help me to solve this issues
thanks in advance
Use the following code, where you want to clear the back stack of controllers.
-(void)clearStack
{
NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
[navigationArray removeAllObjects];
//[navigationArray removeObjectAtIndex: 2];
self.navigationController.viewControllers = navigationArray;
}

Button's Tag are same in TableView

I add view for header, here is my code:
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] init];
view.layer.borderWidth = 0.5;
view.layer.borderColor = [UIColor colorWithRed:127.0/255.0 green:229.0/255.0 blue:232.0/255.0 alpha:1.0].CGColor;
view.backgroundColor = [UIColor clearColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 150, 50)];
label.text = sectionArray[section];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
button = [[UIButton alloc] initWithFrame:CGRectMake(self.myTableView.frame.size.width - 50, 15, 20, 20)];
[button setImage:[UIImage imageNamed:#"down"] forState:UIControlStateNormal];
button.tag = section;
[button addTarget:self action:#selector(extentFunction) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:label];
[view addSubview:button];
return view;
}
-(void)extentFunction {
switch (button.tag) {
case 0:
NSLog(#"0");
break;
case 1:
NSLog(#"1");
break;
case 2:
NSLog(#"2");
break;
case 3:
NSLog(#"3");
break;
default:
break;
}
}
The question is: the button's tag is same of each header, not the section integer (ex: first header button's tag is 0; second header button's tag is 1)that I want.
What should I do?
As first look its clear issue of local & global variable.
You need to define your button as local variable and use button action sender as button object and get tag from that button.
Look at below code I think it will help you to understand your issue:
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] init];
view.layer.borderWidth = 0.5;
view.layer.borderColor = [UIColor colorWithRed:127.0/255.0 green:229.0/255.0 blue:232.0/255.0 alpha:1.0].CGColor;
view.backgroundColor = [UIColor clearColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 150, 50)];
label.text = sectionArray[section];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
//Make Your Button local object not global
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.myTableView.frame.size.width - 50, 15, 20, 20)];
[button setImage:[UIImage imageNamed:#"down"] forState:UIControlStateNormal];
button.tag = section;
[button addTarget:self action:#selector(extentFunction) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:label];
[view addSubview:button];
return view;
}
-(IBAction)btnClick:(UIButton*)sender{
switch (sender.tag) {
case 0:
NSLog(#"0");
break;
case 1:
NSLog(#"1");
break;
case 2:
NSLog(#"2");
break;
case 3:
NSLog(#"3");
break;
default:
break;
}
}
Let me know if its working fine.
This is your custom button code which is correct.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.myTableView.frame.size.width - 50, 15, 20, 20)];
[button setImage:[UIImage imageNamed:#"down"] forState:UIControlStateNormal];
button.tag = section;
[button addTarget:self action:#selector(extentFunction) forControlEvents:UIControlEventTouchUpInside];
But you also need to add its sender and event to get the touch location of your button like below code.
[button addTarget:self action:#selector(extentFunction:event:) forControlEvents:UIControlEventTouchUpInside];
Now when you will click on button, extentFunction selector will get called. Now all you have to do is implement your button selector like below code.
-(void)extentFunction:(id)sender event:(id)event{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:_yourTableViewName];
NSIndexPath *indexPath = [_yourTableViewName indexPathForRowAtPoint: currentTouchPosition];
NSLog(#"%ld",indexPath.section);
}
Hope this will help you out.

NavigationBar alpha value changed automatically after dismissing view controller

Weird thing
navigationbar'alpha value changes after dismissing view controller
Step
Click button that presents a new viewcontroller
Set navigation bar alpha to 0.0 when the button is clicked
Dismiss viewcontroller which I presented right before.
Result [NG]
navigationbar'alpha value is set to 1.0
Expected result
navigationbar's alpha should be 0.0 as expected
Below sample is pretty much easy
ViewController class
- (void)viewWillAppear:(BOOL)animated {
NSLog(#"%s : %f", __func__, self.navigationController.navigationBar.alpha);
}
- (void)viewDidAppear:(BOOL)animated {
NSLog(#"%s : %f", __func__, self.navigationController.navigationBar.alpha);
}
- (void)viewWillDisappear:(BOOL)animated {
NSLog(#"%s : %f", __func__, self.navigationController.navigationBar.alpha);
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.backgroundColor = [UIColor orangeColor];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *clickMe = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
[clickMe setTitle:#"clickme" forState:UIControlStateNormal];
[clickMe setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clickMe addTarget:self action:#selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clickMe];
}
- (void)btnClicked {
self.navigationController.navigationBar.alpha = 0.0f;
second *sec = [[second alloc] init];
[self presentViewController:sec animated:YES completion:nil];
}
second class
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationController.navigationBar.backgroundColor = [UIColor orangeColor];
self.view.backgroundColor = [UIColor yellowColor];
UIButton *getBack = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
[getBack setTitle:#"getBack" forState:UIControlStateNormal];
[getBack setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[getBack addTarget:self action:#selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:getBack];
}
- (void)btnClicked {
[self dismissViewControllerAnimated:YES completion:nil];
}
I really would like to know what happened in this..
If someone knows this, let me know.. Thank you in advance.

Implementing toggle feature in a UIButton

I want to know how to add the toggling feature to a UIButton, something like the user taps a unselected button the button becomes selected and stays selected till the user taps it again there by making unselected like it was before.
I was thinking of making an IBAction which changes it from unselected to selected, how can I do that?
Heres what I tried:
-(IBAction)toggle {
//Toggle on implementation.
button.selected = YES;
button.highlighted = NO;
button.enabled = YES;
//Toggle off implementation.
if (button.highlighted == YES) {
button.selected = NO;
button.highlighted = YES;
button.enabled = NO;
}
}
Problem...
-(IBAction)toggleFav {
if (favButton == nil) {
UIImage *unselectedImage = [UIImage imageNamed:#"favUntapped.png"];
UIImage *selectedImage = [UIImage imageNamed:#"favTapped.png"];
[favButton setImage:unselectedImage forState:UIControlStateNormal];
[favButton setImage:selectedImage forState:UIControlStateSelected];
[favButton setFrame:CGRectMake(0, 0, 40, 40)];
}
if([favButton isSelected]){
//Add to menu.
[favButton setSelected:NO];
} else {
//Remove from menu.
[favButton setSelected:YES];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIImage *unselectedImage = [UIImage imageNamed:#"unselected.png"];
UIImage *selectedImage = [UIImage imageNamed:#"selected.png"];
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setBackgroundImage:unselectedImage forState:UIControlStateNormal];
[b setBackgroundImage:selectedImage forState:UIControlStateSelected];
[b addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[b setFrame:CGRectMake(0, 0, 40, 40)];
[cell.contentView addSubview:b];
}
return cell;
}
-(void) buttonPressed:(UIButton *)sender
{
if([sender isSelected]){
//...
[sender setSelected:NO];
} else {
//...
[sender setSelected:YES];
}
}
Your toggleFav code doesn't make much sense.
if (favButton == nil) { checks, if favButton is present. But if you are wiring it up with IB, it should always been there at that point. And if it wasn't how could the button call this method? So do it like this:
-(void)viewDidLoad
{
//....
UIImage *unselectedImage = [UIImage imageNamed:#"favUntapped.png"];
UIImage *selectedImage = [UIImage imageNamed:#"favTapped.png"];
[favButton setImage:unselectedImage forState:UIControlStateNormal];
[favButton setImage:selectedImage forState:UIControlStateSelected];
[favButton setFrame:CGRectMake(0, 0, 40, 40)];
//....
}
-(IBAction)toggleFav:(UIButton *)sender {
if([sender isSelected]){
//...
[sender setSelected:NO];
} else {
//...
[sender setSelected:YES];
}
}
Here you'll find an example project, with a DetaiView, that holds a Button with the 2 states.
Note: I am saving the information of what button was selected in the NSUserDefaults. You should not do that. Instead you'll want to save it in the model. But as I dont have informations on your model, I am just using NSUserDefaults.
-(void)hitButton:(UIButton*)button
{
buttonOnFlag = !buttonOnFlag;
if( buttonFlag )
[self performSelector:#selector(setHighlight:) withObject:button afterDelay:0];
}
- (void)setHighlight:(UIButton*)button
{
button.highlighted = true;
}
Use button.highlighted property
You should code like this:
-(IBAction)toggle:(id)sender {
//Toggle on implementation.
if (sender.highlighted == NO)
{
sender.selected = YES;
sender.highlighted = NO;
sender.enabled = YES;
}
//Toggle off implementation.
else{
sender.selected = NO;
sender.highlighted = YES;
sender.enabled = NO;
}
}

want to go to next view from rightbarbutton item

I am using this code to get the three button on the right side of the navigationbar ,button are visible but next to this I want to go to the next view from this three button image, text ,vedio.
NSArray *segmentTextContent = [NSArray arrayWithObjects:#"Image",#"Text",#"Video",nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
segmentedControl.frame = CGRectMake(13, 20, 150, kCustomButtonHeight);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
defaultTintColor = [segmentedControl.tintColor retain]; // keep track of this for later
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
self.navigationItem.rightBarButtonItem = segmentBarItem;
if(segmentedControl.selectedSegmentIndex=0)
{
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
}
else if(segmentedControl.selectedSegmentIndex==1)
{
[segmentedControl addTarget:self action:#selector(segmentAction1:) forControlEvents:UIControlEventValueChanged];
}
else if(segmentedControl.selectedSegmentIndex==2)
{
[segmentedControl addTarget:self action:#selector(segmentAction2:) forControlEvents:UIControlEventValueChanged];
}
[segmentBarItem release];
//[modalBarButtonItem release];
}
return self;
}
I think you have a fundamental misunderstanding of how actions and targets work. You can't check on the selectedSegmentIndex at the time of creation. It will always be 0. You have to add the target exactly once and inside the responder method, check the selectedSegmentIndex and act from there:
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvent:UIControlEventTouchUpInside];
//...
- (void) segmentAction:(UISegmentedControl *)source {
switch (source.selectedSegmentIndex) {
case 0:
[self segmentAction0:source];
break;
case 1:
[self segmentAction1:source];
break;
case 2:
[self segmentAction2:source];
break;
}
}