Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save imrekel/3904628 to your computer and use it in GitHub Desktop.
Save imrekel/3904628 to your computer and use it in GitHub Desktop.
bme-ios - iTravel
#pragma mark - from UIImagePickerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage* selectedImage =
info[UIImagePickerControllerOriginalImage];
self.imageView.image = selectedImage;
[self dismissViewControllerAnimated:YES completion:nil];
self.imageView.backgroundColor = [UIColor whiteColor];
}
//
// ITEditTripViewController.h
// iTravel
//
@class ITEditTripViewController;
#import <UIKit/UIKit.h>
@interface ITEditTripViewController : UIViewController <UITextViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *locationTextField;
@property (weak, nonatomic) IBOutlet UITextView *descriptionTextView;
@end
//
// ITEditTripViewController.m
// iTravel
//
#import "ITEditTripViewController.h"
@interface ITEditTripViewController ()
- (IBAction)textFieldDidEndOnExit:(id)sender;
- (IBAction)textFieldEditingDidBegin:(UITextField *)sender;
- (IBAction)textFieldEditingDidEnd:(UITextField *)sender;
- (void)handeImageViewTap:(UITapGestureRecognizer*)recognizer;
@end
@implementation ITEditTripViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handeImageViewTap:)];
self.imageView.userInteractionEnabled = YES;
[self.imageView addGestureRecognizer:tapRecognizer];
self.descriptionTextView.delegate = self;
}
- (void)viewDidUnload
{
[self setImageView:nil];
[self setNameTextField:nil];
[self setLocationTextField:nil];
[self setDescriptionTextView:nil];
[super viewDidUnload];
}
- (IBAction)textFieldDidEndOnExit:(id)sender
{
[sender resignFirstResponder];
}
- (IBAction)textFieldEditingDidBegin:(UITextField *)sender
{
[UIView animateWithDuration:0.25
delay:0
options:UIViewAnimationCurveEaseInOut|UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.view.frame = CGRectOffset(self.view.frame, 0, -100);
} completion:nil];
}
- (IBAction)textFieldEditingDidEnd:(UITextField *)sender
{
[UIView animateWithDuration:0.25
delay:0
options:UIViewAnimationCurveEaseInOut|UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.view.frame = CGRectOffset(self.view.frame, 0, 100);
} completion:nil];
}
- (void)handeImageViewTap:(UITapGestureRecognizer*)recognizer
{
UIImagePickerController* pickerController = [[UIImagePickerController alloc] init];
pickerController.delegate = self;
pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:pickerController animated:YES completion:nil];
}
#pragma mark - from UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[UIView animateWithDuration:0.25
delay:0
options:UIViewAnimationCurveEaseInOut|UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.view.frame = CGRectOffset(self.view.frame, 0, -210);
} completion:nil];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
[UIView animateWithDuration:0.25
delay:0
options:UIViewAnimationCurveEaseInOut|UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.view.frame = CGRectOffset(self.view.frame, 0, 210);
} completion:nil];
}
#pragma mark - from UIImagePickerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage* selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
self.imageView.image = selectedImage;
[self dismissViewControllerAnimated:YES completion:nil];
self.imageView.backgroundColor = [UIColor whiteColor];
}
@end
- (void)viewWillAppear:(BOOL)animated
{
if (self.trip)
{
if (self.trip[@"image-name"])
self.tripImageView.image = [UIImage imageNamed:self.trip[@"image-name"]];
self.tripNameLabel.text = self.trip[@"name"];
self.tripLocationLabel.text = self.trip[@"location"];
self.tripDescriptionTextView.text = self.trip[@"description"];
self.navigationItem.title = self.trip[@"name"];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TripsTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary* tripData =
[_tripsDataMgr.trips objectAtIndex: indexPath.row];
cell.textLabel.text = tripData[@"name"];
cell.detailTextLabel.text = tripData[@"location"];
NSString* tripImageName = tripData[@"image-name"];
cell.imageView.image = [UIImage imageNamed:tripImageName];
return cell;
}
#pragma mark - New methods
- (IBAction)editTripViewControllerDidSave:(UIStoryboardSegue *)unwindSegue
{
ITEditTripViewController* viewController = unwindSegue.sourceViewController;
NSDictionary* tripDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
viewController.nameTextField.text, @"name",
viewController.locationTextField.text, @"location",
viewController.descriptionTextView.text, @"description",
viewController.imageView.image, @"image",
nil];
[_tripsDataMgr.trips addObject:tripDictionary];
[self.tableView reloadData];
}
- (IBAction)editTripViewControllerDidCancel:(UIStoryboardSegue *)unwindSegue
{
}
#define PADDING 60.0
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
return 190;
else if (indexPath.section == 1 && indexPath.row == 2)
{
CGSize textSize = [self.trip[@"description"] sizeWithFont:[UIFont systemFontOfSize:14.0]
constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
return textSize.height+PADDING;
}
else
return self.tableView.rowHeight;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment