Change an UIImageView's alpha and show from UIActionSheet - objective-c

In my UIActionSheet I have a button which mark the touched array as favorite. When an array is favorite, I have a star that shows before the array's name. The favorite icon has an alpha 0, and by touching the "Add as Favorite" button the alpha changes to 1. The arrays are located inside an UIViewController, which shows 1 array in each cell.
The problem I have is that I have to reopen the ViewController for the star to show.
How can I fix so the star shows immediately when I press the "Add as Favorite" button, without reopening the ViewController?
The Favorite Icons alpha is updated under:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
And this is the code that updated the alpha:
if ([FavoriteArray containsObject: [mainArray objectAtIndex:indexPath.row]])
{
favoriteImage.alpha = 1;
}
Thanks!

You need to monitor when the action sheet has been actioned on, and update the collection view accordingly. An example implementation is as follows:
UIActionSheet* actionSheet = ...;
// Set yourself as the action sheet delegate, so you can monitor when events occur
actionSheet.delegate = self;
...
// This is called when a user selects an item within the action sheet
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// kFavouriteButtonIndex is the index of the action sheet item to favourite
if (buttonIndex == kFavouriteButtonIndex) {
// Reload your collection view to update the cells
[self.collectionView reloadData];
}
}
Instead of calling reloadData, if you know the indexPath of the item which is being favourited, then you can call reloadItemsAtIndexPaths:#[indexPath] to make the process more efficient.

You will want to update the image's alpha when a button is selected. You can do this by implementing the UIActionSheetDelegate protocol in your UIViewController. You can do this in you View Controller's .m file:
#interface MyViewController : UIViewController <UIActionSheetDelegate>
Then override the actionSheet:clickedButtonAtIndex: method in the .m file:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
//make sure that the button pressed is the one which should change the image alpha:
if(buttonIndex == 2){ //put the button index in the place of "3"
//your code to update the alpha here
}
}
Make sure that you set the UIActionSheet's delegate to self:
myActionSheet.delegate = self;

Related

Editing a Textfield in a Detail Controller After Clicking a Cell

I'm having a little trouble editing names in a cell after a user inputted them in. Essentially they click on a button 'add name' which takes them to a detail Controller and they enter in their first and last name and click 'done'. That delegates back to the master Controller and the cell is updated to show that name.
What I'd like is for them to be able to input many names and then click on a cell afterwards which takes them back to the data they've already inputted (which is stored in an NSMutableArray called 'entry').
-(void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
self.detailView.firstNameField.text = [[self.entry objectAtIndex:indexPath.row]firstName]];
NSLog(#"%#",[[self.entry objectAtIndex:indexPath.row]firstName]);
NSLog(#"%#", self.detailView.firstNameField.text);
}
The first NSLog shows the name just fine, but the second comes back as "null" and the textfields are blank, ready to have a new name added...All and any help is greatly appreciated :)
I think you are doing this the wrong way the way it works, you dont set values directly to the UITextfield , Instead you pass it a NSString which would be a property of your model.
That is the whole point of MVC the data should not interact with the UI directly.
In your MasterViewController you need to import the DetailViewController header:
#import "DetailViewController.h"
perform the segue in the didSelectedRow:
-(void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
[self performSegueWithIdentifier:#"detailSegue" sender:sender];
}
implement the segue deleguate and set its firstName property (NSSrting)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if([segue.identifier isEqualToString:#"detailSegue"]){
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController*detailView = (DetailViewController*)segue.destinationViewController;
detailVC.firstName =[[self.entry objectAtIndex:indexPath.row]firstName]];
NSLog(#"%#",[[self.entry objectAtIndex:indexPath.row]firstName]);
}
}
then in viewDidLoad of DetailView you would assign the firstName string to your UItextfield text property:
_firstNameField.text = self.firstName;

Trigger setEditing: animated: without using an edit button

I have a UITableView with some custom cells in it. In these custom cells I defined a UILongPressGestureRecognizer that triggers the edit mode of this table. So when someone presses and holds a cell for like 1.5 sec, the table goes into edit mode.
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(startEditMode:)];
Which triggers:
- (void)startEditMode:(UISwipeGestureRecognizer *)recognizer {
if (self.allowEdit) {
UITableView *table = (UITableView *)self.superview;
[table setEditing:YES animated:YES];
}
}
But what I want to do is detect when the table goes into edit mode because I need to show/hide some additional buttons in this case. But for some reason in my viewcontroller this is never executed:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
NSLog(#"SET EDITING");
[super setEditing:editing animated:animated];
}
Any suggestion why? Is this just being called when using a proper Edit Button as provided by default in the UINavigationController?
Or how can I detect when my UITableView goes into Edit Mode?
You're sending the message (setEditing) to the table view, you should be sending it to the view controller (presumably a UITableViewController subclass?). It will then take care of the table view for you.
Ok so in case someone else walks into this thread with the same problem, I will show you how I solved this.
In my custom UITableViewCell I have this method now:
- (void)startEditMode:(UISwipeGestureRecognizer *)recognizer {
if (self.allowEdit) {
UITableView *table = (UITableView *)self.superview;
UITableViewController *control = (UITableViewController *)table.dataSource;
[control setEditing:YES animated:YES];
}
}

How to update DetailView using MasterDetail Application Template

I'm new to using the split view for creating iPad applications. When I first create the project just using the standard MasterDetail Application template (Xcode 4.2), it creates a MasterViewController and a DetailViewController. The template has the following method that is called when a row is selected from the popover table (master detail view controller):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
if (!self.detailViewController)
{
self.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
}
[self.navigationController pushViewController:self.detailViewController animated:YES];
Now I understand when you are using a regular navigation controller if you are programing for an iPhone you just do this type of thing to push on another view controller on to the stack. However, with this template, it just pushes the detail view onto the popover rather than updating what is already present. I'm confused as what I need to update to select something from the pop over (master detail view), and then have the detailView update.
Update:
To try and test out the "detailItem" that is already setup for you in the DetailViewController, I commented out the pushViewController and added the following:
//[self.navigationController pushViewController:self.detailViewController animated:YES];
self.detailViewController.detailItem = #"Test";
// setter in detailViewController
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
// detailDescriptionLabel.text is a IBOutlet to the label on the detailView
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
}
}
According to this code, the text of the label on the detailViewController should be updated. However, when I do click on the item in the master view controller table, nothing happens.
There are a couple different ways you could do it. First off, like you said, remove the pushViewController call (I don't know why Apple's template does this... maybe just to show you you can?).
Next, let your MasterViewController know about the DetailViewController that is already displayed. I usually set master.detailViewController = detailViewController in the appDelegate.
Remember, the DetailViewController is already being displayed, so you won't always need to realloc it (unless you are replacing it with some other view)
First Option
Use delegate calls to set the information. Declare a protocol to pass information to the detailView and have it display it appropriately. Here is a tutorial describing this in more detail.
Second Option
Pass DetailViewController some data & override the setter to refresh the detailView. Here is a tutorial describing this in more detail.
// in DetailViewController
- (void)setDetailItem:(id)newDetailItem {
if (detailItem != newDetailItem) {
[detailItem release];
detailItem = [newDetailItem retain];
// Update the view.
navigationBar.topItem.title = detailItem;
NSString * imageName = [NSString stringWithFormat:#"%#.png",detailItem];
[self.fruitImageView setImage:[UIImage imageNamed:imageName]];
}
}
Edit: Just looked at the template again, and setDetailItem type code is already in there, but the code is creating a completely new detailView so the detailView that is viewable on the splitViewController is not changed at all.

Change UITextfield on a detailview by clicking on different Cells

This is what I have so far:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row)
{
// row 0 -> DetailViewController1 anzeigen...
case 0:
{
TableViewController *fvController = [[TableViewController alloc] initWithNibName:#"TableViewController" bundle:[NSBundle mainBundle]];
fvController.selectedCellItem = selectedCellItem;
[self.navigationController pushViewController:fvController animated:YES];
[fvController release];
fvController = nil;
}
break;
default:
break;
}
So my question is: How can I change a UITextfield of the detailView by selecting a cell. Thank you for your help so far;)
Do you want to edit the text in a UITextField in a cell? If so, there are a few ways you can do this, I'll touch on two of them.
First, why don't u just edit the cell right from within the tableView? This entails you to keep track of each cell's contents but if you always have a custom cell with a UITextField in it, you're half way there.
If you want to make changes to the text in a regular cell, then the user selects it and you give them an edit screen, this is a lot simpler. You can have your tableView push a view controller with a textField that has the text from your cell. They can edit this textField, and then press save. When they save, pop the view off the stack, save the changes to the data model, and update the tableView.
I'm not sure exactly what you're trying to do, but having a textField in a cell and then making edits in a detail screen seems redundant.
If you provide us with a little more info, I'm sure we can help.
UPDATE based on your clarification
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row)
{
// row 0 -> DetailViewController1 anzeigen...
case 0:
{
ViewControllerName *vController = [[UIViewController alloc] initWithNibName:#"ViewControllerName" bundle:[NSBundle mainBundle]];
vController.textField.text = //set the text here from the data source. Assuming textField is an property of vController.
[self.navigationController pushViewController:vController animated:YES];
[vController release];
}
break;
default:
break;
}

How to use a UITableView to return a value

How would I use a tableView as a value selector?
So I have a series of input fields and what I want is when you select a cetian field it opens a tableview of options that you can pick from as a value for that field.
Upon selecting an option it returns to the previous View with the selected value filling that field.
This is what I do, similar to the Settings > General > International > Language table view in the iPhone/iPod.
The user can tap a row and a check mark will appear. The view is dismissed when "Done" or "Cancel" is tapped.
First, create a UITableViewController that will display your options. Have a toolbar on the top with a Cancel and Done button. Also have these properties:
SEL selector; // will hold the selector to be invoked when the user taps the Done button
id target; // target for the selector
NSUInteger selectedRow; // hold the last selected row
This view will be presented with the presentModalViewController:animated: method so it appears from the bottom of the screen. You could present it in any other way, but it seems kind of standard across iPhone applications.
Before presenting the view, set target and selector so a method will be called when the user taps the "Done" button.
Now, in your newly created UITableViewController you can implement the thetableView:didSelectRowAtIndexPath:` method as:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark; // show checkmark
[cell setSelected:NO animated:YES]; // deselect row so it doesn't remain selected
cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]];
cell.accessoryType = UITableViewCellAccessoryNone; // remove check from previously selected row
selectedRow = indexPath.row; // remember the newly selected row
}
Also implement cancel and done methods for the toolbar buttons:
- (IBAction)done:(UIBarButtonItem *)item
{
[target performSelector:selector withObject:[stringArray objectAtIndex:selectedRow]];
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)cancel:(UIBarButtonItem *)item
{
[self dismissModalViewControllerAnimated:YES];
}
You should use UITableViewDelegate's tableView:didSelectRowAtIndexPath:, remember the value somewhere in another object (a share instance/singleton maybe? - depending on your architecture) and then dismiss this table view.
I implemented a ViewController for Date pick.
I create a protocol to return the date picked to the previous view.
#protocol DataViewDelegate
#optional
- (void)dataViewControllerDidFinish:(NSDate*)dateSelected;
#end
...
- (void) viewDidDisappear:(BOOL)animated
{
if ([ (id)(self.delegate) respondsToSelector:#selector(dataViewControllerDidFinish:)])
{
[self.delegate dataViewControllerDidFinish:self.data];
}
[super viewDidDisappear:animated];
}
In the picker view you can use the
tableView:didSelectRowAtIndexPath:
to select the row you want. Here i set the data property.
The previous view is the delegate for the protocol.