iPhone: Find Button at Custom Row - iphone

(Have a question...A have a table view xib and a custom row xib which I use in Table View...In Custom row xib I put 2 buttons (Button1 and Button2) by Interface Builder...So when I run my app a have a Table View with a lot of rows..and in every row I have 2 buttons...What I need is when I press Button1 in current row, to set Button2 Highlighted in the same row...
I just simply created an IBAction to do that:
- (IBAction)buttonTouchIn:(id)sender{
if (sender == self.button1) {
[button2 setHighlighted:YES];
}
But the problem is that I cannot get button2 in the same row with button1...All the time used button2 in last row...Any suggestions ? Thanks...

set tag property in Interface Builder for your buttons, let's say 11 for button1 and 12 for button2
get your button2 with the tag
- (IBAction)buttonTouchIn:(id)sender{
if ([sender tag] == 11){
UITableViewCell* cell = (UITableViewCell*)[[sender superview] superview];
UIButton* button2 = (UIButton*)[cell viewWithTag:12];
[button2 setHighlighted:YES];
}
}
I edited this code on my PC, so there could be errors

Use this method. Tag your button with a specific tag let's assume 11 for button 1 and 12 for button 2. And In an IBACtion write this code to get the cell
- (IBAction) btn1Tapped:(id)sender {
UIButton *btn = (UIButton*)sender;
UITableiewCell *cell = [[btn superview] superview];
//Now you can get the button with tag no 12
UIButton *btn2 = [cell viewWithTag:12];
[btn setHighlighted:YES];
}

Well with the lil info provided i can guess the problem as follows:
You are reusing your cells in uitable view
Probably assigning self.button2 in your cellforrowatindex delegate method
So the last button is getting highlighted. You should create a view controller for your tableview cell and write IBactions for the button press events in that view controller. This way every action on button goes to a seperate viewcontroller.
Let me know if this hint is useful or if you need more directions.

Related

how to create a UITableViewCell with an "info symbol" in the accessoryView/disclosure area? (like the iTunes Connect app)

What's the best way to create a UITableViewCell with an "info symbol" in the accessoryView/disclosure area? Like the iTunes Connect app, that is when you login there is teh Account Name cell, which has an "info" button you can click on. It basically has the text Account Name on the left, then a text field for entry of the account name, and on the right the info icon you can click on.
Aspects of the question:
- which UITableViewCell style is it using?
- is there a way to set the info button as a disclosure type? or is it a custom cell where you have to put your own image there
- then depending on the answer to the above item, how would you trap the info button button push to trigger some help text to be displayed as a modal dialog
Background - I wanted to use this as a means to allow users to get context based help on each item.
try this set the frame according to where u want
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
button.frame = CGRectMake(3,8,30, 30);
[button addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
I think what you want is
You can just set a detail accessory view:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureIndicator;
This gives a little arrow > on the right. When the user taps a cell, the tableView delegate gets a didSelectRowAtIndexPath: message. With a disclosure indicator, you generally respond by pushing a new viewController containing the desired information.
[self.navigationController pushViewController:self.itemHelpVC animated:YES];
From iOS 8 the UITableViewCellAccessoryDetailButton has the form of an info button, so you just need this:
cell.accessoryType = UITableViewCellAccessoryDetailButton;
Result:

added button to uitableviewcell not responding

I have created a custom tableviewcell through interface builder with a label and a button. In the .h and .m file I have made outlets and actions for the button which I have connected.
This cell is added to a tableview controlled by a uiviewcontroller class. However my problem is that when I tap the button, the button is not activated, however I am pushed on to the detailed view belonging to the cell. It seems like the button is behind the cell.
Any suggestions for what I have forgotten or should change?
I have created a button programatically and added as subview to the cell instead and this works, however this gives me another problem as the button is added everytime a cell is loaded.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(200.0f, 5.0f, 100.0f, 40.0f)];
//Set image
UIImage *img = [UIImage imageNamed:#"myPackage.png"];
[button setImage:img forState:UIControlStateNormal];
[img release];
[button addTarget:self action:#selector(myPackagePushed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
I would really like to use the interface builder approach - any suggestions?
Regards
try
[cell.contentView addSubview:button];
also
UIImage *img = [UIImage imageNamed:#"myPackage.png"]; here you are not allocating memory so no need for
[img release]; because there is a possibility of crash
The button should fire the IBAction method that you have set in IB. Have you checked by inserting an NSLog or by setting a breakpoint if the method is entered?
Is the button set to be initially activated in IB?
The crux with the IB construction is that the IBOutlet gets its value when the cell is loaded from the xib. The IBOutlet will then be the button in the cell that the tableview happend to load last.
WHen you add the button programatically, you might need to have the previous added button to removeFromSuperview before you add a button again to that cell.
In my case, the problem was that the button was a subview of the background view.
Buttons assigned to the background view apparently can't respond to touches.

Iphone : How to return a image or anything from second view to first view?

please check the picture you will more clear what I want to present
This is the first launch in First View
It's an empty table view
There is a add button on the right bar
If you press the Add button it will push to the Second View
The second is a only 3 rows table view
Tap the first row it will pop up a alert view to let you set a name
Press create ,the text will appear in the first row
<That's the question 1,I don't know how to pass the text to the cell...>
And keep forward ,That's a custom radio button in the row 2 and row 3
Unless You click the radio button first , otherwise you can't call this methord
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This is last two view
Icon View 1
Icon View 2
There are many buttons in the view,This the second question:
If I press one of this button,it will return to the second view and show the button icon on the tableview row 2 or row 3
Finally,press the Done button(same on the right bar)
It will back to the First View and show what you type the name , and which icon you selected
(Basis on radio button is selected)
That's the last question.
I upload my source code here:Link
You can download it ...
and here is the code about how I push the view...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row==0) {
[self nameInputAlertView];
}
RadioButtonClass *btn = [radioButtonArray objectAtIndex:[indexPath row]];
if (btn.selected) {
//Radio Button is selected ,use that icon
if (indexPath.row == 1) {
IconViewOne *iconViewOne = [[IconViewOne alloc]init];
iconViewOne.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:iconViewOne animated:YES];
[iconViewOne release];
}
if (indexPath.row==2) {
IconViewTwo *iconViewTwo = [[IconViewTwo alloc]init];
iconViewTwo.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:iconViewTwo animated:YES];
[iconViewTwo release];
}
}
}
so...Summary all the question...
1.How to write a text via textfield in the alert view to table view cell ?
2.How to selected an icon from last view to second view ?
3.Send all this two elements to First view : Name & Icon
I only know how to pass elements from view 1 to view 2...
Don't know how to return back......
Thanks mate , please help me to figure out this problem.I will very very appreciate !
BTW:you can take the radio button class to add in your any project
I dont know this is the correct way or not but i think this may help you.
Since you want to retrieve the name and image in both views declare them in the appdelegate so that it can be accessed globally.
Que 1:
When selecting a row didSelectRow Delegate will be called. In that you will get the IndexPath. Store that indexPath globally. Then
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if ([alertView tag]==1) {
if (buttonIndex == 1){
NSLog(#"Created");
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:tableIndexPath]; //tableIndexpath is declared globally as NSindexpath
ReturnToFirstViewAppDelegate *appObj=(ReturnToFirstViewAppDelegate *)[[UIApplication sharedApplication]delegate];
appObj.selectedName = macroNameSetting.text; //Setting the Name globally
cell.detailTextLabel.text =macroNameSetting.text;
[self.tableView reloadInputViews];
}
}
}
Que 2 and 3
In the AppDelegate declare
NSString *selectedName;
UIImage *selectedImage;
Assign property and synthesize them
In your iconViews
- (void)setIcon : (id)sender{
UIButton *button = (UIButton *)sender;
UIImage *selectedImg = [UIImage imageNamed:[NSString stringWithFormat:#"marco%d",button.tag]];
ReturnToFirstViewAppDelegate *appObj=(ReturnToFirstViewAppDelegate *)[[UIApplication sharedApplication]delegate];
appObj.selectedImage = selectedImg;
// push your viewcontroller
}
In any Viewcontroller access them as
ReturnToFirstViewAppDelegate *appObj=(ReturnToFirstViewAppDelegate *)[[UIApplication sharedApplication]delegate];
//For example
//cell.image = appObj.selectedImage;
In any case if you dont like using AppDelegate Variables , there is one other method which You can use in case of Navigation Controllers :-
for eg. in "IConViewOne.m"
UITableViewCell* cell = [[[self.navigationController.viewControllers objectAtIndex:1] tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]] ;
cell.imageView.image=[UIImage imageNamed:[NSString stringWithFormat:#"marco%d.png",button.tag]];
and then in "ViewWillAppear" of SecondView :-
[self.tableView reloadData];
Similarly for passing to RootViewController u can give:-
self.navigationController.topViewController.tableView
OR
[[self.navigationController.viewControllers objectAtIndex:0]tableView]
A great way to pass information from one object to another, without knowing who the recipient is (and there may be multiple objects interested in the information), is to post an NSNotification. It allows you to completely avoid direct knowledge of other objects.
Read Apple's document Notification Programming Topics and give it a try.

What is a good way for a custom accessory view in a UITableViewCell to message the table view controller?

I am working on adding a custom accessory view, (a button) to a UITableViewCell, and I need it to tell the table view when it is touched, but I can't figure out how to communicate to the table view what button was pressed. Ideally I'd like to somehow call a function like this:
[controller tableView:view didSelectCustomButtonAtIndexPath:indexPath usingCell:self];
when my custom view button is pressed.
Sorry if this is a bit vague, I'm not really sure how to explain this well. I am basically looking for how to mimic the implementation for tableView:didSelectRowAtIndexPath: without having to subclass UITableViewCell.
Thanks for any help.
When I had to do this, I did it like so:
//when adding accessoryView to cell in cellForRowAtIndexPath:
UIButton *b = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[b addTarget:self action:#selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = b;
//the handling method
- (void)doAction:(id)sender {
NSIndexPath *indexPath = [table indexPathForCell:(UITableViewCell *)[sender superview]];
// code goes here, indexPath tells you what row was touched
}
Should work with a control other than a UIButton.

UIButton not working in tableview cell

I've added a button to a tableview cell, but I'm having two problems with it:
1) I've invoked a setTarget:action: method on the button, but the action: method never gets called:
[playButton addTarget:self action:#selector(playButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
2) the cell itself ends up getting selected on the touch, invoking didSelectRowAtIndexPath, which I don't want to happen.
I've verified that the button is at least seeing the touch by setting its showsTouchWhenHighlighted property and seeing it "glow" when pressed. But for whatever reason it looks like the touch event is then getting passed up to its cell superview instead of being handled by the button itself.
I've tried adding the button directly to the cell's contentView, as well as to other cell subviews, with no effect. I've also made sure that the button's superview(s) were all touch-enabled.
Any thoughts?
Howard
There are two ways to do that, the simplest is to add the button to the cell with IB, set a tag number to the button and when you are in the cellForRowAtIndexPath method updating the cell use something like:
UIButton *myButton = (UIButton *)[cellYouAreUpdating viewWithTag:-1969];
being -1969 the tag number you previously set in IB and cellYouAreUpdating the cell reference. After that you will have a reference to the button in the cell and you could set the add target with the proper selector.
Hope this helps.
My be your button Y axis OR Y axis + height is Higher than its super View (Super view may be cell or contentview of cell) check the heght of superview
-->For your first problem,use this code
UIButton *imgV1 = [UIButton buttonWithType:UIButtonTypeCustom];
[imgV1 setFrame:CGRectMake(0, 0, 320, 53)];
[imgV1 setBackgroundColor:[UIColor clearColor]];
[imgV1 setImage:[UIImage imageNamed:#"expired.png"] forState:UIControlStateNormal];
[imgV1 addTarget:self action:#selector(show_product_details:) forControlEvents:UIControlEventTouchUpInside];
-->For your second problem use this in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}