Button's Tag are same in TableView - objective-c

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.

Related

Programatically created UIDatepicke done and cancle button is not working

This is my UIDatePicker code but in that picker, done and cancle button is not working ...please help me.... i also add here my done and cancle button methods here..Please give me a proper suggessions for how it works properly.
if (textField.tag==Jobdatetxt.tag)
{
[Jobdatetxt resignFirstResponder];
[data.toolbar removeFromSuperview];
UITextField *lagFreeField = [[UITextField alloc] init];
[self.view.window addSubview:lagFreeField];
[lagFreeField becomeFirstResponder];
[lagFreeField resignFirstResponder];
[lagFreeField removeFromSuperview];
//////////////////////////////////////////////// DATE OF JOB////////////////////////////////////////////////
pickerDate = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,result.height - 216, result.width, 216)];
[pickerDate setDatePickerMode:UIDatePickerModeDate];
pickerDate.backgroundColor = [UIColor whiteColor];
[self.view.window addSubview:pickerDate];
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlackOpaque;
toolbar.frame=CGRectMake(0,result.height - 266,result.width,50);
cancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cancel setTitle:#"Cancel" forState:UIControlStateNormal];
cancel.backgroundColor = [UIColor clearColor];
[cancel.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
cancel.titleLabel.textColor = [UIColor whiteColor];
[cancel addTarget:self action:#selector(CancelClicked:) forControlEvents:UIControlEventTouchUpInside];
cancel.tag = textField.tag;
cancel.frame = CGRectMake(10, 10, 70, 30);
[toolbar addSubview:cancel];
doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
doneButton.backgroundColor = [UIColor clearColor];
[doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
doneButton.titleLabel.textColor = [UIColor whiteColor];
[doneButton addTarget:self action:#selector(DoneClicked:) forControlEvents:UIControlEventTouchUpInside];
doneButton.tag = textField.tag;
doneButton.frame = CGRectMake(result.width - 80, 10, 70, 30);
[toolbar addSubview:doneButton];
[self.view.window addSubview:toolbar];
}
// Done Button method
-(IBAction)DoneClicked:(UIButton *)sender
{
[pickerDate removeFromSuperview];
[toolbar removeFromSuperview];
}
//Cancle Button Method
-(IBAction)CancelClicked:(UIButton *)sender
{
[pickerDate removeFromSuperview];
[toolbar removeFromSuperview];
}

Add texfield in runtime in IOS

I want to make an "add" button to add as many textfields as i want in my scroll view.
I did this already
UITextField *text=[[UITextField alloc]initWithFrame:CGRectMake(50,50,200,200)];
[self.scrollView addSubview:text];
But is not working.
Thanks!
If this is your complete button code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
// add this
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
// continue
[self.mainScroll addSubview:button];
Then add this somewhere:
- (void) buttonPressed:(UIButton *)button {
UITextField *text=[[UITextField alloc]initWithFrame:CGRectMake(50,50,200,200)];
text.backgroundColor = [UIColor greenColor]; // just to make sure it's showing up.
[self.scrollView addSubview:text];
}
First create common function like this,
-(void)setTextfieldStyle:(UITextField *)pTmpTextField
{
pTmpTextField.borderStyle = UITextBorderStyleRoundedRect;
pTmpTextField.font = [UIFont systemFontOfSize:15];
pTmpTextField.placeholder = #"First Name";
pTmpTextField.autocorrectionType = UITextAutocorrectionTypeNo;
pTmpTextField.keyboardType = UIKeyboardTypeDefault;
pTmpTextField.returnKeyType = UIReturnKeyDone;
pTmpTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
pTmpTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
pTmpTextField.delegate = self;
}
and use this for creating UITextFiled,
int y = 0;
for(int i=0;i < 5;i++)
{
UITextField *txtTemp=[[UITextField alloc]initWithFrame:CGRectMake(0, y, 300, 31)];
txtTemp.tag = i;
[self setTextfieldStyle:txtTemp];
[self.view addSubview:txtTemp];
[txtTemp release];
y+=36;
}
This is UITextField's delegate method,
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"%#",textField.text);
}
Hope this works for you as this works for me. only keep this in mind that add the text fields to your scroll view.
In the example am adding them to my view!

Button action doesn't respond when switched viewcontroller

I have an app where the viewcontroller is switched if a button is pressed. I switch like this:
NewViewController *nv = [[NewViewController alloc] init];
[nv NewButtonTapped:self];
[self.view addSubview:nv.view];
Then in my NewViewController.m:
- (void)NewButtonTapped:(id)sender
{
backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 748)];
backgroundImage.image = [UIImage imageNamed:#"cubePage.png"];
[self.view addSubview:backgroundImage];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(964, 100, 60, 150);
button.enabled = YES;
button.userInteractionEnabled = YES;
[button setTitle:#"D" forState:UIControlStateNormal];
button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonTapped:(id)sender
{
NSLog(#"Tapped");
}
All images and buttons are loaded and appears fine in the new viewcontroller but the button action buttonTapped: doesn't respond when I'm tapping the button. This code works fine in the first viewcontroller.What's the problem?

how to maintain state of uibutton in table view

I have a table view in that I am using a custom cell. I have two buttons on each table view cell named "edit" and "cancel" both have images in cellForRow. What I want that when user clicks on edit button at same time same rows cancel button should change its image. Code is Working but it not changing same rows cancel button image. Its changing another rows cancel button image. How to maintain state of each button.
Here Is Code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
simpleTableIdentifier = #"MenuNameCell";
MenuNameCell *cell = (MenuNameCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell== nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenuNameCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSLog(#"---------new cell agin");
}
else
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
// Creating Label Menu Name
_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 11, 82, 21)];
_nameLabel.backgroundColor = [UIColor clearColor];
_nameLabel.text = [_hotel._orderedMenus objectAtIndex:indexPath.row];
// Creating Label Menu Cost
_amountMenu = [[UILabel alloc] initWithFrame:CGRectMake(167, 13, 44, 21)];
_amountMenu.backgroundColor = [UIColor clearColor];
_amountMenu.text = [[_hotel._menuPrices objectAtIndex:indexPath.row] stringValue];
// Creating Text Field For Order Quantity
_textFieldQuantity = [[UITextField alloc] initWithFrame:CGRectMake(125,14,42,21)];
_textFieldQuantity.userInteractionEnabled = NO;
_textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:indexPath.row] stringValue];
//Creating Button For CANCEL Order
_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_cancelButton setFrame:CGRectMake(265, 13, 25, 28)];
[_cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel.png"] forState:UIControlStateNormal];
[_cancelButton setTag:indexPath.row];
[_cancelButton addTarget:self action:#selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
// Creating Button For Check Order
_checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
[_checkButton setTag:indexPath.row];
[_checkButton setBackgroundImage:[UIImage imageNamed:#"edit.png"]forState:UIControlStateNormal];
[_checkButton addTarget:self action:#selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
// Adding All To Content View
[cell.contentView addSubview:_nameLabel];
[cell.contentView addSubview:_amountMenu];
[cell.contentView addSubview:_textFieldQuantity];
[cell.contentView addSubview:_checkButton];
[cell.contentView addSubview:_cancelButton];
//objc_setAssociatedObject(_checkButton, iindex, indexPath,OBJC_ASSOCIATION_RETAIN );
return cell;
}
-(void)editQuantity:(id)sender{
NSLog(#"count of the array is----%d",[imageViewArray count]);
button = (UIButton *)sender;
row = button.tag;
NSLog(#"---rowww%d",row);
_textFieldQuantity.userInteractionEnabled = YES;
UIImage *buttonImage = [UIImage imageNamed:#"edit_over.png"];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
UIImage *buttonImageCancel = [UIImage imageNamed:#"check.png"];
[_cancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
_cancelButton.tag = 0;
}
-(void)cancelOreder:(id)sender{
button = (UIButton *)sender;
row = button.tag;
NSLog(#"The Row Selected iS At Cancel Order ISSSS----%d", row);
if (_cancelButton.tag == 0){
_textFieldQuantity.userInteractionEnabled = NO;
UIImage *buttonImageCancel = [UIImage imageNamed:#"check_over.png"];
[_cancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
UIImage *buttonImageCancel1 = [UIImage imageNamed:#"cancel.png"];
[_cancelButton setBackgroundImage:buttonImageCancel1 forState:UIControlStateNormal];
UIImage *buttonImage = [UIImage imageNamed:#"edit.png"];
[_checkButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
_cancelButton.tag = 1;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"iHomeDelivery" message:#"Do You Want To Cancel the Order" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
Try to make _cancelButton and _checkButton with different tag in the same cell, I think it is puzzle to make both of them with same tag in the same row.
Give different tag for each buttons first like :
[_checkButton setTag:indexPath.row];
[_cancelButton setTag:indexPath.row+100];
and in your editQuantity: method
button = (UIButton *)sender;
row = button.tag;
NSIndexPath *currentIndexPath = [NSIndexPath indexPathForRow:row inSection:0];
UITableViewCell *newCell = [yourTable cellForRowAtIndexPath:currentIndexPath];
newCancelButton = (UIButton *)[newCell.contentView viewWithTag:row+100];
[newCancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
Do same in your cancelOreder: method to change image of _checkButton.
Also, use this for remove all subViews,
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
please try this solution
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CELL_ID"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CELL_ID"];
UIButton *_cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_cancelButton setFrame:CGRectMake(65, 13, 100, 28)];
[_cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
[_cancelButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[_cancelButton addTarget:self action:#selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
[_cancelButton setTag:1];
UIButton *_checkButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_checkButton setFrame:CGRectMake(200, 13, 100, 28)];
[_checkButton setTitle:#"Check" forState:UIControlStateNormal];
[_checkButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[_checkButton addTarget:self action:#selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
[_checkButton setTag:2];
[cell.contentView addSubview:_checkButton];
[cell.contentView addSubview:_cancelButton];
}
return cell;
}
-(void) cancelOreder:(id)sender
{
UITableViewCell *cell = (UITableViewCell *)[(UIButton *)sender superview];
UIButton *btn = (UIButton *)[cell viewWithTag:2];
if(btn != nil)
{
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
-(void) editQuantity:(id)sender
{
UITableViewCell *cell = (UITableViewCell *)[(UIButton *)sender superview];
UIButton *btn = (UIButton *)[cell viewWithTag:1];
if(btn != nil)
{
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
in this example i cheange text color of label you can change image,hope this will help you

Changing value on a label with UISlider control in WePopoverControler

I am in a process of implementing WePopovercontroller in my app and I have a question on how to implement uislider on it. I am able to show the slider in the UIView and get the action when slider moved but not sure how to set the value of the label in that view. Here is the part of the code
-(void)popoverSliderMoved:(UISlider *) sender{
NSLog(#"slider %f",sender.value);
}
-(IBAction)showSettingsMenu:(UIButton *)sender{
if(!self.popoverSettingsController) {
// Create a label and button for the popover
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
[label setText:#"Bookmark it!"];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setTextAlignment:UITextAlignmentCenter];
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 45, 100, 40)];
[slider addTarget:self action:#selector(popoverSliderMoved:) forControlEvents:UIControlEventValueChanged];
UIFont *font = [UIFont boldSystemFontOfSize:20];
[label setFont:font];
CGSize size = [label.text sizeWithFont:font];
CGRect frame = CGRectMake(0, 0, size.width + 10, size.height + 10); // add a bit of a border around the text
label.frame = frame;
UIButton *button = [[UIButton alloc] initWithFrame:label.frame];
[button addSubview:label];
[button addTarget:self action:#selector(popoverButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIViewController *viewCon = [[UIViewController alloc] init];
[viewCon.view addSubview:slider];
[viewCon.view addSubview:button];
CGRect popOverFrm = CGRectMake(0, 0, frame.size.width + slider.frame.size.width, frame.size.height + slider.frame.size.height);
viewCon.contentSizeForViewInPopover = popOverFrm.size;//CGSizeMake(100, 36);
NSLog(#"Label Frame: %#", NSStringFromCGRect(label.frame));
NSLog(#"Popover size: %#", NSStringFromCGSize(viewCon.contentSizeForViewInPopover));
NSLog(#"ViewCon: %#", NSStringFromCGRect(viewCon.view.frame));
self.popoverSettingsController = [[WEPopoverController alloc] initWithContentViewController:viewCon];
//[self.popoverSettingsController setDelegate:self];
}
if([self.popoverSettingsController isPopoverVisible]) {
[self.popoverSettingsController dismissPopoverAnimated:YES];
// [navPopover setDelegate:nil];
self.popoverSettingsController = nil;
} else {
[self.popoverSettingsController presentPopoverFromRect:CGRectMake(sender.frame.size.width, 0, 200, 57)
inView:self.navigationController.view
permittedArrowDirections:UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown
animated:YES];
}
}
Would i need to use a delegate in this case or a property for this viewcontroller to set the label.
Thanks!!!
Yes using a property would be what I would do.