iOS Development-commissioned combat

Source: Internet
Author: User

Last night headache, wrote a part of the hasty collection of pens, and in the morning to replace an article, yesterday on the basic use of the Commission and concepts are a little bit, the beginning of the study commissioned by the Apple website and the Chinese blog articles read a lot, similar index higher. Delegates in the name to be accurate, it is best to look at the name of the use, see the name will know what is used, such as uinavigationcontrollerdelegate,uitableviewdelegate, So named whether it is their own start or other maintenance is a very worry about things, double benefit.

Page Layout

Let's take a look at it, so you probably know what should be done, and the effect is as follows:

In this implementation, the layout in the storyboard is a static table of Navigationcontroller and a uitableview:

In fact, the added button is the bar Buttom Item in the control library, the identifier is set to add can be the above effect, there is a point to explain, add a book in the view is a static cell, you need to delete the controller redundant method, otherwise it will not appear.

Demo Implementation

The above is related to the delegation of the place is to save the data to the main view to add new, rather than their own new data, you can define a delegate to achieve the above effect, specific reference to my previous article. You first need to define a book class to store the data:

#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface book:nsobject<nscoding>@ Property (strong,nonatomic) UIImage *converpicture; @property (strong,nonatomic) nsstring *bookname; @property (Strong, nonatomic) NSString *author; @property (strong,nonatomic) nsnumber *price; @end

BOOK.M need to store data to implement two methods, specifically refer to the previous article Nscoder storage data:

  book.m//  mysearchbar////  Created by Keso on 15/2/4.//  Copyright (c) 2015 Keso. All rights reserved.//#import "Book.h" @implementation book-(void) Encodewithcoder: (Nscoder *) acoder{        // Note Here is the call to store the JPG image    [Acoder encodeobject:uiimagejpegrepresentation (self. converpicture,1.0) forkey:@ "Converpicture"];    [Acoder encodeobject:_bookname forkey:@ "BookName"];    [Acoder encodeobject:_author forkey:@ "Author"];    [Acoder encodeobject:_price forkey:@ "Price"];    } -(ID) Initwithcoder: (Nscoder *) adecoder{self        . Converpicture=[uiimage imagewithdata:[adecoder decodeobjectforkey:@ "Converpicture"];    Self. Bookname=[adecoder decodeobjectforkey:@ "BookName"];    Self. Author=[adecoder decodeobjectforkey:@ "Author"];    Self. Price=[adecoder decodeobjectforkey:@ "Price"];    return self;    } @end

  First look at the first view: declarations in RootViewController.h:

#import <UIKit/UIKit.h> #import "EditViewController.h" @interface Rootviewcontroller:uitableviewcontroller <CustomEditViewControllerDelegate> @end

ROOTVIEWCONTROLLER.M in Viewdiload method load data:

-(void) viewdidload {    [super viewdidload];        Nsarray *codepath= nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES);        Self. bookpath= [codepath[0] stringbyappendingpathcomponent:@ "book.plist"];    This path temporarily seems to have not stored the data of the said            nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];    Self. Booklist=[[nsmutablearray Alloc]init];    NSLog (@ "%@", Nshomedirectory ());    if ([FileManager Fileexistsatpath:_bookpath]) {self         . Booklist=[nskeyedunarchiver unarchiveobjectwithfile:self. Bookpath];}    }

-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {return 1;} -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section {return [self]. Booklist count];} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {static Nsstri        ng *cellflag = @ "Bookcellflag";        UITableViewCell *cell = [TableView Dequeuereusablecellwithidentifier:cellflag]; Book *book=self.        Booklist[indexpath.row]; [Cell.textlabel Settext:book.        BookName]; [Cell.detailtextlabel Settext:book.        Author]; [Cell.imageview Setimage:book.        Converpicture]; return cell;} -(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{if ([Segue.identifier isequaltostring:@ "E        Ditsegue "]) {Editviewcontroller *controller = Segue.destinationviewcontroller;    [Controller setcustomdelegate:self]; }}

 Prepareforsegue is used to handle new actions, the main view lives a custom delegate and needs to implement its methods for the child views to invoke:

-(void) Savebookinfo: (Editviewcontroller *) controller{            if (self. Booklist==nil) {self        . Booklist=[[nsmutablearray alloc]init];    }        Book *book=controller. Originalbook;    [Self. Booklist Addobject:book];        Update data after the whole file is written    [Nskeyedarchiver archiverootobject:self. Booklist tofile:self. Bookpath];        [Self.tableview reloaddata];        }

declaration in the. h file of the child view:

 #import <UIKit/UIKit.h> #import "Book.h" @class editviewcontroller;# Pragma mark-Custom delegate @protocol Customeditviewcontrollerdelegate <nsobject>//Define a method to save data in the root view-(void) Savebookinfo :(Editviewcontroller *) controller; @end @interface Editviewcontroller:uitableviewcontroller < Uiimagepickercontrollerdelegate,uipickerviewdelegate,uipickerviewdatasource,uinavigationcontrollerdelegate> @property (Weak, nonatomic) id<customeditviewcontrollerdelegate> customdelegate; @property (strong,nonatomic) Book *originalbook; @property (weak, nonatomic) Iboutlet Uiimageview *converimage; @property (weak, nonatomic) Iboutlet UI TextField *booknametext; @property (weak, nonatomic) Iboutlet Uitextfield *authortext; @property (weak, nonatomic) Iboutlet Uitextfield *pricetext; @end 

editviewcontroller.m//mysearchbar////Created by Keso on 15/2/4.//Copyright (c) 2015 Keso. All rights reserved.//#import "EditViewController.h" @interface Editviewcontroller () @end @implementation        editviewcontroller-(void) viewdidload {[Super viewdidload];    Uncomment the following line to preserve selection between presentations.        Self.clearsselectiononviewwillappear = NO;    Uncomment the following line to display a Edit button in the navigation bar for this view controller.    Self.navigationItem.rightBarButtonItem = Self.editbuttonitem;    }-(ibaction) Chooseconverimage: (ID) Sender {uiimagepickercontroller *picker=[[uiimagepickercontroller alloc]init];    Specify photo source [Picker setsourcetype:uiimagepickercontrollersourcetypephotolibrary];    Specifies whether to allow modification of [picker setallowsediting:yes];    Designated agent [Picker setdelegate:self]; Show photo selection controller [Self.navigationcontroller presentviewcontroller:picker animated:yes completion:nil];} -(void) IMAgepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) info{UIImage *    Image = info[@ "Uiimagepickercontrollereditedimage"];    Set the picture [Self.converimage Setimage:image]; [Self.navigationcontroller Dismissviewcontrolleranimated:yes completion:nil];} -(Ibaction) SaveBook: (ID) Sender {if (self. Originalbook==nil) {self.    Originalbook=[[book Alloc]init];    }//book name _originalbook.bookname=_booknametext.text;    Cover _originalbook.converpicture=_converimage.image;    _originalbook.author=_authortext.text;        _originalbook.price=[[nsnumber alloc] Initwithfloat:[_pricetext.text Floatvalue]];        Notifies the parent view controller (user list) to save the user record and returns [_customdelegate savebookinfo:self]; Return to Superior view [Self.navigationcontroller Popviewcontrolleranimated:yes];}    -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end

This also is a commissioned small demo bar, than yesterday those two slightly better, use the knowledge point before the blog has been written, if there are problems can be discussed together, if there is inappropriate, can comment area Exchange, grateful ~

iOS Development-commissioned combat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.