ios memory management & navigation

36
WAKE UP Sunday, January 15, 12

Upload: -

Post on 22-Apr-2015

1.242 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: iOS Memory management & Navigation

WAKE UP

Sunday, January 15, 12

Page 2: iOS Memory management & Navigation

Sunday, January 15, 12

Page 3: iOS Memory management & Navigation

HIG (HUMAN INTERFACE GUIDELINE)

• Прегърни Платформата и Хюман Интерфейс Принципите

• Ясни дефиниции:

- какво?

- на кого?

Sunday, January 15, 12

Page 4: iOS Memory management & Navigation

ПРЕЖИВЯВАНЕТО НА ПОТРЕБИТЕЛЯ

• поведение

• изглед

• начало и край

• употреба на бутони

• употреба на жестове

Sunday, January 15, 12

Page 5: iOS Memory management & Navigation

RESOURCESNavigation of ViewControllers

Sunday, January 15, 12

Page 6: iOS Memory management & Navigation

RESOURCES

• images, icons

• Has 90° corners

• shine or gloss

• not use alpha transparency

• not include a drop shadow

Sunday, January 15, 12

Page 7: iOS Memory management & Navigation

Sunday, January 15, 12

Page 8: iOS Memory management & Navigation

CUSTOM ARTWORK

Sunday, January 15, 12

Page 9: iOS Memory management & Navigation

APPLICATION ICONS

iPhone 2G/3G iPhone 4G iPad

App icon57 x 57

Icon.png114 x 114

[email protected] x 72

Icon~ipad.png

Awtwork 512 x 512iTunesArtwork/.png/

- -

Loading image 320 x 480Default.png

640 x [email protected]

320 x 480Default~ipad.png

Settings icon29 x 29

Icon-settings.png / Icon-Small.png

29 x 29Icon-settings.png /

Icon-Small.png

29 x 29Icon-settings.png /

Icon-Small.png

Documents icon 29 x 29Icon-doc.png

58 x [email protected]

64 x 64Icon-doc~ipad.png

Spotlight search - -50 X 50

Icon-spot~ipad.png/Icon-Small-50.png

Sunday, January 15, 12

Page 10: iOS Memory management & Navigation

IMPORT IN XCODE

Sunday, January 15, 12

Page 11: iOS Memory management & Navigation

THE MEMORY MANAGEMENT

Sunday, January 15, 12

Page 12: iOS Memory management & Navigation

АДРЕСИ И СТОЙНОСТИ

assign = store address

Sunday, January 15, 12

Page 13: iOS Memory management & Navigation

RETAINCOUNT

Sunday, January 15, 12

Page 14: iOS Memory management & Navigation

COPYINGDEEP VS SHALLOWThere are two kinds of object copying: shallow copies and deep copies. The normal copy is a shallow copy

that produces a new collection that shares ownership of the objects with the original. Deep copies createnew objects from the originals and add those to the new collection. This difference is illustrated by Figure1.

Figure 1 Shallow copies and deep copies

Object E4

Shallow copy

Array 1 Array 2 Array 1 Array 2

Deep copy

4 Object E4 Object E4

Object D3 3 Object D3 Object D3

Object C2 2 Object C2 Object C2

Object B1 1 Object B1 Object B1

Object A0 0 Object A0 Object A0

Shallow Copies

There are a number of ways to make a shallow copy of a collection. When you create a shallow copy, theobjects in the original collection are sent a retainmessage and the pointers are copied to the new collection.Listing 1 shows some of the ways to create a new collection using a shallow copy.

Listing 1 Making a shallow copy

NSArray *shallowCopyArray=[someArray copyWithZone:nil];

NSDictionary *shallowCopyDict=[[NSDictionary alloc] initWithDictionary: someDictionary copyItems: NO];

These techniques are not restricted to the collections shown. For example, you can copy a set with thecopyWithZone:method—or the mutableCopyWithZone:method—or an array withinitWithArray:copyItems:method.

Shallow Copies 392010-09-01 | © 2010 Apple Inc. All Rights Reserved.

Copying Collections

NSDictionary *shallowCopyDict=[[NSDictionary alloc] initWithDictionary:someDictionary copyItems: NO];

NSArray *deepCopyArray=[[NSArray alloc] initWithArray: someArray copyItems: YES];

objects retain objects copy

Sunday, January 15, 12

Page 15: iOS Memory management & Navigation

RETAIN / RELEASEApplicationmemorymanagement is the process of allocatingmemory during your program’s runtime, usingit, and freeing it when you are done with it. A well-‐written program uses as little memory as possible. InObjective-‐C, it can also be seen as a way of distributing ownership of limitedmemory resources amongmanypieces of data and code. When you have finished working through this guide, you will have the knowledgeyou need to manage your application’s memory by explicitly managing the life cycle of objects and freeingthem when they are no longer needed.

Althoughmemorymanagement is typically considered at the level of an individual object, your goal is actuallytomanage object graphs. Youwant tomake sure that you have nomore objects inmemory than you actuallyneed.

alloc/init

Retain count = 1

Destroyed

Destroyed

Class A

retain

2

Class B

2

release

2

Class A

release

1

Class B

copy

1

release

0

0Class C

Class C

At a Glance

Objective-‐C provides three methods of application memory management.

1. In the method described in this guide, referred to as “manual retain-‐release” or MRR, you explicitlymanage memory by keeping track of objects you own. This is implemented using a model, known asreference counting, that the Foundation class NSObject provides in conjunction with the runtimeenvironment.

At a Glance 72011-09-28 | © 2011 Apple Inc. All Rights Reserved.

About Memory Management

A well-written program uses as little memory as possible.

Sunday, January 15, 12

Page 16: iOS Memory management & Navigation

RETAIN CYCLES

return self;}

Since the Counter class has an object instance variable, you must also implement a dealloc method. Itshould relinquish ownership of any instance variables by sending them a release message, and ultimatelyit should invoke super’s implementation:

- (void)dealloc { [_count release]; [super dealloc];}

Use Weak References to Avoid Retain Cycles

Retaining an object creates a strong reference to that object. An object cannot be deallocated until all ofits strong references are released. A problem, known as a retain cycle, can therefore arise if two objects mayhave cyclical references—that is, they have a strong reference to each other (either directly, or through achain of other objects each with a strong reference to the next leading back to the first).

The object relationships shown in Figure 1 (page 17) illustrate a potential retain cycle. The Document objecthas a Page object for each page in the document. Each Page object has a property that keeps track of whichdocument it is in. If the Document object has a strong reference to the Page object and the Page object hasa strong reference to the Document object, neither object can ever be deallocated. The Document’s referencecount cannot become zero until the Page object is released, and the Page object won’t be released until theDocument object is deallocated.

Figure 1 An illustration of cyclical references

textparent

parentparagraph

Paragraph

Page

page

Document

retaindon’tretain

don’tretain

retain

Use Weak References to Avoid Retain Cycles 172011-09-28 | © 2011 Apple Inc. All Rights Reserved.

Practical Memory Management

Sunday, January 15, 12

Page 17: iOS Memory management & Navigation

THE MEMORY MANAGEMENT

Sunday, January 15, 12

Page 18: iOS Memory management & Navigation

THE MEMORY MANAGEMENT

My WAY

Sunday, January 15, 12

Page 19: iOS Memory management & Navigation

MEMORY MANAGEMENT

00 0View1 View2 View3

nil

0View4

nilnil

nilalloc retain /ownership/

addSubview

+1+1 +1

subviews/NSArray/

objects to be created/UIView/

Sunday, January 15, 12

Page 20: iOS Memory management & Navigation

11 1View1 View2 View3

nil

1View4

nilnil

nilalloc retain /ownership/

addSubview

+1+1 +1

+1 +1 +1

+1

MEMORY MANAGEMENTUIView *....= [[[UIView alloc] init] autorelease];

Sunday, January 15, 12

Page 21: iOS Memory management & Navigation

31 2retainretain

View2 nil

View1 View2 View3

View3 nil nil

2View4

View2 nil

retain

alloc retain /ownership/

addSubview

+1+1 +1

+1+1

+1

MEMORY MANAGEMENTForm view hierarchy

retain

+1

Sunday, January 15, 12

Page 22: iOS Memory management & Navigation

33 2

View2 nil

View1 View2 View3

View3 nil nil

2View4

View2 nilalloc retain /ownership/

addSubview

+1+1 +1

MEMORY MANAGEMENT

+1

@property(nonatomic, retain)UIView *view1;

Set view1 as a property /UIVIewController/

Controller

+1

Sunday, January 15, 12

Page 23: iOS Memory management & Navigation

43 2

delegatedelegate

View2 nil

View1 View2 View3

View3 nil nil

2View4

View2 nil

MEMORY MANAGEMENT

alloc retain /ownership/

addSubview

+1+1 +1

+1

@property (nonatomic, assign) id delegate;

@property (nonatomic, retain) id delegate;

Controllerassign / weak retain / strong

Sunday, January 15, 12

Page 24: iOS Memory management & Navigation

33 2

retain

View1 View2 View3

View3 nil nil

2View4

View2 nil

retain

nil

MEMORY MANAGEMENT

dealloc release removeFromSuperview

-1-1 -1

-1

[view2 removeFromSuperview];

Controller

Controller kills View1

delegatedelegate

Sunday, January 15, 12

Page 25: iOS Memory management & Navigation

31 2

retain

View1 View2 View3

View3 nil nil

2View4

View2 nil

retain

nil

MEMORY MANAGEMENT

dealloc release removeFromSuperview

-1

-1 -1

- (void)dealloc { [view1 release];}

Controllerdelegatedelegate

-1

-1

assign / weak retain / strong

Sunday, January 15, 12

Page 26: iOS Memory management & Navigation

3nil 2

retain

delegatedelegate

View2 View3

View3 nil nil

2View4

View2 nil

retain

nil

message to nilis legal

MEMORY MANAGEMENT

if ([_delegate respondsToSelector:@selector(requiredMethod)]) {

[_delegate requiredMethod]; }

autorelease

assign / weak retain / strong

Sunday, January 15, 12

Page 27: iOS Memory management & Navigation

2 2

retain

delegate

View2 View3

View3 nil nil

1View4

MEMORY MANAGEMENTKill View4?

retain / strong

dealloc release removeFromSuperview

-1 -1 -1

-1 -1

nil

Sunday, January 15, 12

Page 28: iOS Memory management & Navigation

2 2

retain

delegate

retain

View2 View3

View3 nil nil

RETAIN CYCLE

LEAK!?!

Няма останали *указатели към обектите

Sunday, January 15, 12

Page 29: iOS Memory management & Navigation

3 2

retain

delegate

View2 View3

View3 nil nil

2View4

MEMORY MANAGEMENTKill View4?

retain / strong

dealloc release removeFromSuperview

-1

-1

-1

[subviews <#cleanUpDelegates#>]

-1

View2 nil

Sunday, January 15, 12

Page 30: iOS Memory management & Navigation

1 1View2 View3

nil

1View4

MEMORY MANAGEMENTKill View4?

dealloc release removeFromSuperview

-1 -1-1

-1-1-1

nilnil

They will be autoreleased or released in dealloc

of the owner

Sunday, January 15, 12

Page 31: iOS Memory management & Navigation

0 0View2 View3

nil

0View4

MEMORY MANAGEMENT

nilnil

autorelease

Sunday, January 15, 12

Page 32: iOS Memory management & Navigation

ПРИМЕРИ

if ([defaults objectForKey:@"actorsPlayed"] != nil) {! ! [_actorsPlayed release];! ! _actorsPlayed = nil;! ! _actorsPlayed = [[defaults objectForKey:@"actorsPlayed"] retain];! }

NSDictionary *currRecord = [[NSDictionary alloc] ! ! ! ! ! ! ! ! initWithObjects:[NSArray arrayWithObjects:

[NSNumber numberWithInt:highScore], _game.playerName, nil]

! ! ! ! ! ! ! ! forKeys:[NSArray arrayWithObjects:@"score", @"name", nil]];

NSMutableArray *diffHighScore = [[NSMutableArray alloc] initWithArray:[_localHiscores objectForKey:key]];

Array autorelease

Sunday, January 15, 12

Page 33: iOS Memory management & Navigation

NAVIGATION CONTROLLERОснови

Sunday, January 15, 12

Page 34: iOS Memory management & Navigation

NAVIGATION CONTROLLERFigure 3-3 The navigation stack

UINavigationController

viewControllers(NSArray)

View controller

topViewController

visibleViewController

Navigation stack

Your main responsibility is to push new view controllers onto the stack in response to user actions. Each viewcontroller you push on the navigation stack is responsible for presenting some portion of your application’sdata. Typically, when the user selects an item in the currently visible view, you create a new view controllerobject, assign the data for the selected item to it, and push the new view controller onto the stack. Doing sois how you present the selected data to the user. For example, when the user selects a photo album, thePhotos application pushes a view controller that displays the photos in that album. In most cases, you donot have to pop view controllers off the stack programmatically. Instead, the navigation controller providesa back button on the navigation bar, that when tapped, pops the topmost view controller automatically.

For more information about how to customize the navigation bar, see “Customizing the Navigation BarAppearance” (page 75). For information about pushing view controllers onto the navigation stack (andremoving them later), see “Modifying the Navigation Stack” (page 73). For information on how to customizethe contents of the toolbar, see “Displaying a Navigation Toolbar” (page 81).

Creating a Navigation Interface

You use a navigation interface in situations where the information you want to present is organizedhierarchically. Typically, you use a navigation controller to manage the presentation of hierarchical data butyou could also use one to manage multilevel editing or some other interface that required multiple successivescreens.

Before creating a navigation interface, you need to decide how you intend to use it. There are a handful ofplaces where you might install a navigation interface in your application:

● Install it directly in your application’s main window.

● Install it as the root view controller of a tab in a tab bar interface.

● Install it as one of the two root view controllers in a split view interface. (iPad only)

66 Creating a Navigation Interface2011-01-07 | © 2011 Apple Inc. All Rights Reserved.

CHAPTER 3

Navigation Controllers

Sunday, January 15, 12

Page 35: iOS Memory management & Navigation

NAVIGATION CONTROLLER

• popToRootViewControllerAnimated:<#(BOOL)#>

• popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#>

• popViewControllerAnimated:<#(BOOL)#>

• pushViewController:<#(UIViewController *)#> animated:<#(BOOL)#>

Root VC VC2 VC3 Visible Top

10 2 3 3

NSArray of ViewControllers

Sunday, January 15, 12

Page 36: iOS Memory management & Navigation

XCODE

Sunday, January 15, 12