Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improving app experience based on device orientation change

  • Loading branch information...
commit d2333249b5447155fe527795cbf7bbeda772fe3c 1 parent 6deb10e
Ryan Blunden authored
View
7 Homepwner/Homepwner/DetailViewController.h
@@ -12,5 +12,10 @@
@interface DetailViewController : UIViewController <UIImagePickerControllerDelegate, UITextFieldDelegate,
UINavigationControllerDelegate, UIPopoverControllerDelegate>
-@property (nonatomic, strong) BNRItem *item;
+
+@property(nonatomic, strong) BNRItem *item;
+@property (nonatomic, copy) void (^dismissBlock)(void);
+
+- (id)initForViewItem:(BOOL)isNew;
+
@end
View
58 Homepwner/Homepwner/DetailViewController.m
@@ -9,6 +9,7 @@
#import "DetailViewController.h"
#import "BNRItem.h"
#import "BNRImageStore.h"
+#import "BNRItemStore.h"
@interface DetailViewController () {
UIPopoverController *_imagePickerPopover;
@@ -21,6 +22,7 @@ @interface DetailViewController () {
@property(weak, nonatomic) IBOutlet UIToolbar *toolbarView;
@property(weak, nonatomic) IBOutlet UIImageView *imageView;
@property(weak, nonatomic) IBOutlet UIButton *deletePictureButton;
+@property(weak, nonatomic) IBOutlet UIBarButtonItem *cameraButton;
- (IBAction)backgroundTapped:(id)sender;
@@ -44,11 +46,48 @@ - (id)init {
return self;
}
+- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
+ @throw [NSException exceptionWithName:@"Wrong initializer" reason:@"Use initForNewItem:"
+ userInfo:nil];
+}
+
- (void)setItem:(BNRItem *)item {
_item = item;
[[self navigationItem] setTitle:[[self item] itemName]];
}
+- (id)initForViewItem:(BOOL)isNew {
+ self = [super initWithNibName:@"DetailViewController" bundle:nil];
+
+ if (self) {
+ if (isNew) {
+ UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
+ target:self
+ action:@selector(save:)];
+ [[self navigationItem] setRightBarButtonItem:doneItem];
+
+ UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
+ target:self
+ action:@selector(cancel:)];
+ [[self navigationItem] setLeftBarButtonItem:cancelItem];
+ }
+ }
+
+ return self;
+}
+
+- (void)save:(id)save {
+ [[self presentingViewController] dismissViewControllerAnimated:YES completion:[self dismissBlock]];
+}
+
+- (void)cancel:(id)cancel {
+ // If the user cancalled, then remove the BNRItem from the store
+ [[BNRItemStore sharedStore] removeItem:[self item]];
+
+ [[self presentingViewController] dismissViewControllerAnimated:YES completion:[self dismissBlock]];
+}
+
+
- (IBAction)deletePicture:(id)sender {
[[BNRImageStore sharedStore] removeImageForKey:[[self item] imageKey]];
[[self item] setImageKey:nil];
@@ -135,10 +174,10 @@ - (void)viewWillAppear:(BOOL)animated {
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
- [self saveItem];
+ [self saveItem:nil];
}
-- (void)saveItem {
+- (void)saveItem:(id)o {
// Save changes to item
BNRItem *item = [self item];
[item setItemName:[[self nameField] text]];
@@ -147,7 +186,7 @@ - (void)saveItem {
}
- (void)finishEditing {
- [self saveItem];
+ [self saveItem:nil];
[[self view] endEditing:YES];
[[self navigationController] popToRootViewControllerAnimated:YES];
}
@@ -243,4 +282,17 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
+
+- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
+ if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
+ if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
+ [[self imageView] setHidden:YES];
+ [[self cameraButton] setEnabled:NO];
+ }
+ else {
+ [[self imageView] setHidden:NO];
+ [[self cameraButton] setEnabled:YES];
+ }
+ }
+}
@end
View
20 Homepwner/Homepwner/DetailViewController.xib
@@ -337,6 +337,14 @@
<int key="connectionID">164</int>
</object>
<object class="IBConnectionRecord">
+ <object class="IBCocoaTouchOutletConnection" key="connection">
+ <string key="label">cameraButton</string>
+ <reference key="source" ref="372490531"/>
+ <reference key="destination" ref="202025213"/>
+ </object>
+ <int key="connectionID">421</int>
+ </object>
+ <object class="IBConnectionRecord">
<object class="IBCocoaTouchEventConnection" key="connection">
<string key="label">backgroundTapped:</string>
<reference key="source" ref="191373211"/>
@@ -1067,7 +1075,7 @@
<nil key="activeLocalization"/>
<dictionary class="NSMutableDictionary" key="localizations"/>
<nil key="sourceID"/>
- <int key="maxID">419</int>
+ <int key="maxID">421</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<array class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -1094,15 +1102,21 @@
</object>
</dictionary>
<dictionary class="NSMutableDictionary" key="outlets">
+ <string key="cameraButton">UIBarButtonItem</string>
<string key="dateLabel">UILabel</string>
<string key="deletePictureButton">UIButton</string>
<string key="imageView">UIImageView</string>
<string key="nameField">UITextField</string>
+ <string key="pictureButton">UIBarButtonItem</string>
<string key="serialField">UITextField</string>
<string key="toolbarView">UIToolbar</string>
<string key="valueField">UITextField</string>
</dictionary>
<dictionary class="NSMutableDictionary" key="toOneOutletInfosByName">
+ <object class="IBToOneOutletInfo" key="cameraButton">
+ <string key="name">cameraButton</string>
+ <string key="candidateClassName">UIBarButtonItem</string>
+ </object>
<object class="IBToOneOutletInfo" key="dateLabel">
<string key="name">dateLabel</string>
<string key="candidateClassName">UILabel</string>
@@ -1119,6 +1133,10 @@
<string key="name">nameField</string>
<string key="candidateClassName">UITextField</string>
</object>
+ <object class="IBToOneOutletInfo" key="pictureButton">
+ <string key="name">pictureButton</string>
+ <string key="candidateClassName">UIBarButtonItem</string>
+ </object>
<object class="IBToOneOutletInfo" key="serialField">
<string key="name">serialField</string>
<string key="candidateClassName">UITextField</string>
View
3  Homepwner/Homepwner/Homepwner-Info.plist
@@ -30,9 +30,10 @@
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
- <string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
+ <string>UIInterfaceOrientationPortraitUpsideDown</string>
+ <string>UIInterfaceOrientationPortrait</string>
</array>
</dict>
</plist>
View
27 Homepwner/Homepwner/ItemsViewController.m
@@ -73,15 +73,21 @@ - (IBAction)addNewItem:(id)sender {
/// Create a new BNRItem and add it to the store
BNRItem *newItem = [[BNRItemStore sharedStore] createItem];
- // / Figure out where that item is in the array
- int lastRow = [[[BNRItemStore sharedStore] allItems] indexOfObject:newItem];
- NSIndexPath *ip = [NSIndexPath indexPathForRow:lastRow inSection:0];
-
- // Insert into table
- // We could get a reference to the UITableView instance by using [self view] but as this is inherited
- // from UIViewController, it has no knowledge about what type of view it is
- // Using [self tableView] tells the compiler (and your IDE) that we're dealing with a view of type UITableView
- [[self tableView] insertRowsAtIndexPaths:@[ip] withRowAnimation:UITableViewRowAnimationTop];
+ DetailViewController *detailViewController = [[DetailViewController alloc] initForViewItem:YES];
+
+ [detailViewController setItem:newItem];
+
+ [detailViewController setDismissBlock:^{
+ [[self tableView] reloadData];
+ }];
+
+ UINavigationController *navController = [[UINavigationController alloc]
+ initWithRootViewController:detailViewController];
+
+ [navController setModalPresentationStyle:UIModalPresentationFullScreen];
+ [navController setModalTransitionStyle:UIModalTransitionStylePartialCurl];
+
+ [self presentViewController:navController animated:YES completion:nil];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
@@ -102,7 +108,8 @@ - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sou
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- DetailViewController *detailViewController = [[DetailViewController alloc] init];
+
+ DetailViewController *detailViewController = [[DetailViewController alloc] initForViewItem:NO];
NSArray *items = [[BNRItemStore sharedStore] allItems];
BNRItem *selectedItem = [items objectAtIndex:[indexPath row]];
Please sign in to comment.
Something went wrong with that request. Please try again.