Contacts (Practice)

Source: Internet
Author: User

#import <UIKit/UIKit.h>

@interface Rootviewcontroller:uiviewcontroller <UITableViewDataSource,UITableViewDelegate>

@end

#import "RootViewController.h"

#import "SecondViewController.h"

@interface Rootviewcontroller () {

Nsmutablearray *dataarr;

UITableView *mytableview;

}

@end

@implementation Rootviewcontroller

-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil

{

self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil];

if (self) {

Custom initialization

}

return self;

}

-(void) Viewwillappear: (BOOL) animated

{

Refresh TableView When returning from the previous interface

[Mytableview Reloaddata];

}

-(void) Presenttosecondviewcontroller

{

Secondviewcontroller *svc = [[Secondviewcontroller alloc]init];

Svc.arr = Dataarr;

[Self presentviewcontroller:svc animated:yes completion:nil];

}

-(void) viewdidload

{

[Super Viewdidload];

Do any additional setup after loading the view.

if ([Self respondstoselector: @selector (setautomaticallyadjustsscrollviewinsets:)]) {

Self.automaticallyadjustsscrollviewinsets = NO;

}

Single-instance object for obtaining Nsuserdefaults

Nsuserdefaults *ud = [Nsuserdefaults standarduserdefaults];

Find the stored array by key

Dataarr = [ud objectforkey:@ "VVVV"];

if (!dataarr.count) {

If the value is not taken, re-open the space and assign a value

If not found, it is a null pointer, and any operation of the null pointer is

Dataarr = [[Nsmutablearray alloc]init];

for (int i = 0; i < 9; i++) {

Nsdictionary *dic = [nsdictionary dictionarywithobjectsandkeys:[nsstring stringwithformat:@ "character%d", i],@ "PersonName", [NSString stringwithformat:@ "135000012%02d", i],@ "PhoneNumber", nil];

In the array are dictionaries, each dictionary has 2 key-value pairs, each representing a name and a phone number

[Dataarr Addobject:dic];

}

Put the array into UD via key, and synchronize

[UD Setobject:dataarr forkey:@ "VVVV"];

[UD Synchronize];

}

Mytableview = [[UITableView alloc]initwithframe:cgrectmake (0, +, +, 480-64) Style:uitableviewstyleplain];

Mytableview.delegate = self;

Mytableview.datasource = self;

[Self.view Addsubview:mytableview];

Uibarbuttonitem *bbi = [[Uibarbuttonitem alloc]initwithtitle:@ "+" style:uibarbuttonitemstylebordered target:self Action: @selector (Presenttosecondviewcontroller)];

Self.navigationItem.rightBarButtonItem = BBI;

}

#pragma mark-tableview

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath

{

return 50;

}

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section

{

return dataarr.count;

}

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath

{

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:@ "Qwq"];

if (cell = = nil) {

cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:@ "Qwq"];

}

Cell.textLabel.text = [[Dataarr objectAtIndex:indexPath.row] objectforkey:@ "PersonName"];

Cell.detailTextLabel.text = [[Dataarr objectAtIndex:indexPath.row] objectforkey:@ "PhoneNumber"];

return cell;

}

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath

{

Secondviewcontroller *svc = [[Secondviewcontroller alloc]init];

Data source pointer passing through

Svc.arr = Dataarr;

Click on the first few

SVC.I = Indexpath.row;

With 1 for push past, if you do not pass the default of 0, it is to play the past

SVC.J = 1;

[Self.navigationcontroller pushviewcontroller:svc Animated:yes];

}

#import <UIKit/UIKit.h>

@interface Secondviewcontroller:uiviewcontroller

Receive the data source from the previous page

@property (Nonatomic,retain) Nsmutablearray *arr;

The last page clicked on the first few cells

@property (nonatomic,assign) int i;

(If the push-over J is equal to 1, the bounce is 0.)

@property (nonatomic,assign) Int J;

@end

#import "SecondViewController.h"

@interface Secondviewcontroller () {

Uitextfield *TF1;

Uitextfield *TF2;

}

@end

@implementation Secondviewcontroller

-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil

{

self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil];

if (self) {

Custom initialization

}

return self;

}

-(void) Okbclick

{

if (!tf1.text.length | |!tf2.text.length) {

Uialertview *av = [[Uialertview alloc]initwithtitle:@ "Write" Message:nil delegate:nil cancelbuttontitle:@ "Good" Otherbuttontitles:nil];

[AV show];

Return

}

Click Save if there are values in 2 tf, create a dictionary to record the information

Nsdictionary *dic = [nsdictionary dictionarywithobjectsandkeys:tf1.text,@ "PersonName", tf2.text,@ "PhoneNumber", nil] ;

Nsuserdefaults *ud = [Nsuserdefaults standarduserdefaults];

if (SELF.J) {

If the push comes over, delete the original, add the new one (modify)

[_arr REMOVEOBJECTATINDEX:SELF.I];

[_arr insertobject:dic ATINDEX:SELF.I];

[UD Setobject:_arr forkey:@ "VVVV"];

[UD Synchronize];

[Self.navigationcontroller Popviewcontrolleranimated:yes];

Return

}

If it's bouncing, add the dictionary directly to the data source (new)

[_arr Addobject:dic];

[UD Setobject:_arr forkey:@ "VVVV"];

[UD Synchronize];

[Self dismissviewcontrolleranimated:yes completion:nil];

}

-(void) viewdidload

{

[Super Viewdidload];

Do any additional setup after loading the view.

Self.view.backgroundColor = [Uicolor Whitecolor];

TF1 = [[Uitextfield Alloc]initwithframe:cgrectmake (50, 80, 220, 30)];

Tf1.backgroundcolor = [Uicolor Graycolor];

[Self.view ADDSUBVIEW:TF1];

TF2 = [[Uitextfield alloc]initwithframe:cgrectmake (50, 120, 220, 30)];

Tf2.backgroundcolor = [Uicolor Graycolor];

[Self.view ADDSUBVIEW:TF2];

If it's a push, write down the name and phone number.

if (SELF.J) {

Tf1.text = [[Self.arr objectatindex:_i] objectforkey:@ "PersonName"];

Tf2.text = [[Self.arr objectatindex:_i] objectforkey:@ "PhoneNumber"];;

}

UIButton *OKB = [UIButton buttonwithtype:uibuttontypecustom];

Okb.frame = CGRectMake (100, 170, 120, 40);

[OKB settitle:@ "Save" forstate:uicontrolstatenormal];

[OKB Settitlecolor:[uicolor Bluecolor] forstate:uicontrolstatenormal];

[OKB addtarget:self Action: @selector (Okbclick) forcontrolevents:uicontroleventtouchupinside];

[Self.view ADDSUBVIEW:OKB];

}

Contacts (Practice)

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.