I have a ViewController called NotepadView.h/.m with a UIButton that has the title "User: (Select)". Once the user touches the button, a UIPickerView appears as a popover, which is initialized from UserPickerViewController.h/.m. Once the button is clicked, the proper rows appear in the picker. But after an IBAction is implemented (tapping the button, choosing the correct row form the picker, then leaving the picker) the button title only updates after the button is pressed again.
I know this is because I am using a touch up inside action, so once the button is touched, the title is immediately changed to "User: (null)" until it is touched a second time after the proper row has been selected in the picker. How do I implement an action so that when the picker row is selected, it immediately changes the button title, instead of filling it with "User: (null)" until i touch it again?
Also, how would I go about exiting the UserPickerViewController.m after the row is selected, and segueing back to the NotepadView.m besides touching outside of the picker view?
NotepadView.m under viewDidLoad:
[_userBTN setTitle: #"User: (Select)" forState: UIControlStateNormal];
Under an IBAction of touch up inside:
- (IBAction)refreshUsername:(id)sender {
[_userBTN setTitle: [NSString stringWithFormat:#"User: %#",gUserInfo.chosenUserName] forState: UIControlStateNormal];
}
In UserPickerViewController.m:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSArray *userArray = [gUserInfo availableUsers];
gUserInfo.chosenUserName = [userArray objectAtIndex:row];
}
As #JasonMcDermott recommended, I changed my UserPickerViewController.m to contain a UIBarButton called "Save". Once the button is pressed, using an unwindSegue, the string from the picker row, is stored in a variable named user, which is then transferred over to the NotepadView.m upon touchUpInside.
Related
On Select a table row when the button is clicked in the row. I have cells that contains a button in each row. My problem is when I click on a button in a row the row does not get selected.How do I make the row get selected when I click a button in that row? Uisnd Xamarin IOS
You need to add addTarget for your button in cellforrowAtIndexpath method like this
[cell.yourbutton addTarget:self action:#selector(buttonclick:) forControlEvents:UIControlEventTouchUpInside];
-(void)buttonclick:(UIButton*)sender
{
// get your button action here..
}
I have a table view (filled by customs cells) and my idea is have a tabbar button (such as "edit button"). Pressing on it, in all row, the button cancell will appear. Is it possible?
You can create the code for the button (you know... UIButton* button = [UIButton alloc]init];) in your cell and set it to Hidden = YES, then add a BOOL to check if the "edit button" was pressed, lets name it BOOL editActive, then when the button is pressed you will check the editActive, if its NO then the Hidden value of the button will be NO and you reload your table with [yourTableView reloadData], otherwise, if editActive = YES, then you must hide your "Cancel button" in the cell and once again use [yourTableView reloadData]
I have a custom UICollectionViewCell that holds a UIButton with an image set. The purpose of this bottom is to allow users who are shopping via the app to add items to a list of their favourite items.
I have 2 versions of the image. One is a default greyed out heart and the other is a black heart. When a user taps to add an item to their favourites this method is fired:
Method fired on tap of heart:
- (void)addToFavouritesButtonTapped
{
NSLog(#"add to favourites button tapped");
}
In my cellForItemAtIndexPath method for my UICollectionView I have this:
_addToFavouritesButton = [cell addFavouriteButton];
[_addToFavouritesButton addTarget:_thisController action:#selector(addToFavouritesButtonTapped) forControlEvents:UIControlEventTouchUpInside];
Upon tapping the heart to save an item to favourites I'd like the image to change to my highlighted version which is the black heart.
How can I achieve this?
Set the other image as the one for the UIControlStateSelected and then when the button is tapped set the button to selected= YES, this will then change the image.
_addToFavouritesButton = [cell addFavouriteButton];
[_addToFavouritesButton addTarget:_thisController action:#selector(addToFavouritesButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[_addToFavouritesButton setImage:blackHeart forState:UIControlStateSelected];
- (void)addToFavouritesButtonTapped:(UIButton *)sender
{
NSLog(#"add to favourites button tapped");
sender.selected = YES;
}
You can use the button state to show a different image. Check the documentation for UIButton, there is a method setImage:forState: that you can use to set an image for normal state, highlighted state, selected state.
You can even do it in Interface Builder.
On a side note, if you want to do something else with the button once it's pressed you should do the following:
Append : to the selector
[_addToFavouritesButton addTarget:_thisController action:#selector(addToFavouritesButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
Receive the UIButton in the method
- (void)addToFavouritesButtonTapped:(UIButton *)sender
Then you can manipulate the button in that method
Is there any way I can get hold of which button I have pressed?
The buttons are created programmatically using a for loop.
I have a scroll-view of images (i used buttons for the images) where the images are taken by the user from the camera. So after the user takes a picture, the "new" picture will appear in the scrollview with the "old" pictures. The pictures are shrink into smaller sizes so what I want is that when I click the button (of any image) the image will pop up in another view in the actual size.
The button of images is created using a for loop. However, I do not know how to get hold of which button the user press. For now, when i press the button (regardless of which image/button), the last picture that is taken will always show up.
Thanks for you time.
You can use the tag property of button.
When you create the button, put a tag to each image buttons.
for(int i = 0 ; i < your_no_images ; i++){
UIButton *button = [UIButton <yourbuttontype>];
-----
button.tag = i;
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[yourView addSubView:button];
}
Now in the button click action..
-(void)buttonClicked:(UIButton*)sender{
//if you has an array of UIImage's
UIImage *clickedImage = (UIImage *)[yourImageArray objectAtIndex:sender.tag];
}
IBAction methods, like those triggered upon clicking a button, include a sender argument. sender is the object that triggered the action, for example the button that was clicked. Like this:
- (IBAction)buttonWasClicked:(id)sender
{
NSLog(#"The %# button was clicked", (UIButton *)sender.currentTitle);
}
I have a simple TableView containing several cells. Normally, I switch to selected cell details by tapping this cell. But what if I need a dedicated button for every cell? I've seen "Table View Cell" properties in Interface Builder, it has what I need, but it can't be added to existing cells.
How to properly add this kind of button to every cell of standard TableView?
I do something similar in an app I am working on right now. I have a cell that has a button on it, and I need to know which button was pushed in which cell. I do that like this..
I add my button to each cell..
// add buy button to each cell
UIImage *image;
buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
image = [UIImage imageNamed:#"buy.png"];
[buyButton setBackgroundImage:image forState:UIControlStateNormal];
buyButton.frame = CGRectMake(220, 35, 96, 34);
[buyButton setTag:cellIndex];
[buyButton addTarget:self action:#selector(buyTickets:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:buyButton];
The method used to determine which "button" in which cell was selected, I then push another view controller with the information of the selected button...
// buy tickets button pressed from main table view
- (void) buyTickets:(id)sender{
ResultViewController *vc = [[ResultViewController alloc] init];
vc.buyMovieID = [sender tag]; // "sender tag" is the cell id the button is located in
[[super navigationController] pushViewController:vc animated:YES];
[vc release];
}
This is what the button looks like on each cell.
Hope this helps!
P.S. Tapping on the CELL, would push another view controller, but tapping on "Buy Tickets" button pushes a different one.
alt text http://luistovar.com/ultratableview.jpg
I think the detail disclosure accessory type is what you need. The doc can be found on UITableViewCell class reference.
It says :
The accessory view appears in the the right side of the cell in the table view’s normal (default) state. The standard accessory views include the disclosure chevron; for a description of valid accessoryType constants, see “Cell Accessory Type.” The default is UITableViewCellAccessoryNone. If a custom accessory view is set through the accessoryView property, the value of this property is ignored. If the cell is enabled and the accessory type is UITableViewCellAccessoryDetailDisclosureButton, the accessory view tracks touches and, when tapped, sends the data-source object a tableView:accessoryButtonTappedForRowWithIndexPath: message.
Setting your cell's accessoryType property to UITableViewCellAccessoryDetailDisclosureButton, you can easily do whatever you wan when the detail disclosure button is pressed. What's great about using this accessory type is that it's a standard button, so it is user-friendly and it does all the job of tracking which cell has been touched for you.
If you want to use a custom button, you should set the accessoryView property to that custom button and listen to events on it.
The way i added a button to a tableview cell was by subclassing UITableViewCell and then building its view in interface builder.