EditingViewController.m
上传用户:hechengdz
上传日期:2020-05-13
资源大小:1591k
文件大小:5k
源码类别:

iPhone

开发平台:

Objective-C

  1. /*
  2.      File: EditingViewController.m
  3.  Abstract: 
  4. Manages editing a single field of data in a Book. Dual mode editor, it supports both plain text field editing
  5. and date value editing with a UIDatePicker.
  6.   Version: 1.9
  7.  
  8.  Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
  9.  Inc. ("Apple") in consideration of your agreement to the following
  10.  terms, and your use, installation, modification or redistribution of
  11.  this Apple software constitutes acceptance of these terms.  If you do
  12.  not agree with these terms, please do not use, install, modify or
  13.  redistribute this Apple software.
  14.  
  15.  In consideration of your agreement to abide by the following terms, and
  16.  subject to these terms, Apple grants you a personal, non-exclusive
  17.  license, under Apple's copyrights in this original Apple software (the
  18.  "Apple Software"), to use, reproduce, modify and redistribute the Apple
  19.  Software, with or without modifications, in source and/or binary forms;
  20.  provided that if you redistribute the Apple Software in its entirety and
  21.  without modifications, you must retain this notice and the following
  22.  text and disclaimers in all such redistributions of the Apple Software.
  23.  Neither the name, trademarks, service marks or logos of Apple Inc. may
  24.  be used to endorse or promote products derived from the Apple Software
  25.  without specific prior written permission from Apple.  Except as
  26.  expressly stated in this notice, no other rights or licenses, express or
  27.  implied, are granted by Apple herein, including but not limited to any
  28.  patent rights that may be infringed by your derivative works or by other
  29.  works in which the Apple Software may be incorporated.
  30.  
  31.  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
  32.  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  33.  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
  34.  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
  35.  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
  36.  
  37.  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
  38.  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  39.  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  40.  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
  41.  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
  42.  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
  43.  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
  44.  POSSIBILITY OF SUCH DAMAGE.
  45.  
  46.  Copyright (C) 2008 Apple Inc. All Rights Reserved.
  47.  
  48.  */
  49. #import "EditingViewController.h"
  50. @implementation EditingViewController
  51. @synthesize textValue, editedObject, editedFieldKey, dateEditing, dateValue, textField, dateFormatter;
  52. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  53.     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  54.         // Create a date formatter to convert the date to a string format.
  55.         dateFormatter = [[NSDateFormatter alloc] init];
  56.         [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
  57.         [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
  58.     }
  59.     return self;
  60. }
  61. - (void)viewDidLoad {
  62.     // Adjust the text field size and font.
  63.     CGRect frame = textField.frame;
  64.     frame.size.height += 10;
  65.     textField.frame = frame;
  66.     textField.font = [UIFont boldSystemFontOfSize:16];
  67.     // Set the view background to match the grouped tables in the other views.
  68.     self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
  69. }
  70. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  71.     // Return YES for supported orientations.
  72.     return (interfaceOrientation == UIInterfaceOrientationPortrait);
  73. }
  74. - (void)dealloc {
  75.     [dateFormatter release];
  76.     [datePicker release];
  77.     [textValue release];
  78.     [editedObject release];
  79.     [editedFieldKey release];
  80.     [dateValue release];
  81.     [super dealloc];
  82. }
  83. - (IBAction)cancel:(id)sender {
  84.     // cancel edits
  85.     [self.navigationController popViewControllerAnimated:YES];
  86. }
  87. - (IBAction)save:(id)sender {
  88.     // save edits
  89.     if (dateEditing) {
  90.         [editedObject setValue:datePicker.date forKey:editedFieldKey];
  91.     } else {
  92.         [editedObject setValue:textField.text forKey:editedFieldKey];
  93.     }
  94.     [self.navigationController popViewControllerAnimated:YES];
  95. }
  96. - (void)viewWillAppear:(BOOL)animated {
  97.     NSString *capitalizedKey = [editedFieldKey capitalizedString];
  98.     self.title = capitalizedKey;
  99.     textField.placeholder = capitalizedKey;
  100.     if (dateEditing) {
  101.         textField.enabled = NO;
  102.         if (dateValue == nil) self.dateValue = [NSDate date];
  103.         textField.text = [dateFormatter stringFromDate:dateValue];
  104.         datePicker.date = dateValue;
  105.     } else {
  106.         textField.enabled = YES;
  107.         textField.text = textValue;
  108.         [textField becomeFirstResponder];
  109.     }
  110. }
  111. - (IBAction)dateChanged:(id)sender {
  112.     if (dateEditing) textField.text = [dateFormatter stringFromDate:datePicker.date];
  113. }
  114. @end