MVC Interface Development

Source: Internet
Author: User

1. What is design mode

MVC is just one of the best solutions for a specific type of problem.

1.MVC:

1.Model (model) view (view) controller (Controller) abbreviation

Model: Process data Logic in a program (data storage, business logic, multithreading, network transport, file storage)

View: Processing data display in a program

The media of Controller:view and model

2. Advantages:

1. Low coupling

2. High re-usability

3. High maintainability (clear structure, reusable, easy to maintain)

1. For reference data types, the array space is created when the property is guaranteed to be changed for the first time. So you have to override the property Get and set methods:

Overriding the set method of the suit property

-(void) Setsuit: (NSString *) suit{

if ([[[Card Allsuit]containsobject:suit]) {card class, + (nsarray*) allsuit;

_suit=suit;

}

}

Override the get method of the Cardinfo property

-(nsstring*) cardinfo{

_cardinfo=[self.suit StringByAppendingString:self.rank];

return _cardinfo;

}

Ensure that the array space is created when the property is changed for the first time

-(Nsmutablearray *) allcards{

if (!_allcards) {

_allcards=[nsmutablearray array];

}

return _allcards;

}

**2. Random number

A random number of unsigned positive integers starting from 0

unsigned int index=arc4random ()%52 0-51

Features: No random operator required

**3.lazy loading lazy loading

Knowledge points

Eight, multi-MVC development (Multi-Interface development)

1. Multiple MVC

The display of each interface is a separate set of MVC, and because the application requires multiple interfaces, it forms multiple sets of MVC.

Note: where C and V are tied together, but M is a set of systems separated from C and V due to the realization of data storage and business logic in the program, so multiple sets of c+v combinations can share a single m. However, do not allow the sharing of the V, each C has its own V, not because you want to switch interface, and let a certain C discard their own V, for another C under the V.

1.1 Multi-Interface switching

Principle: The replacement of c is achieved by replacing the V

1.2 How to implement C replacement?

Direction: Launched from Controla-> Controlb

[ControlA Presentviewcontroller:]

Direction: A After the introduction of B, want to return from B to a

[Controlb Dismissviewcontroller:];

Cases:

Create two controller classes A and B, each of the classes ' Xib interface pulls a button, and then adds a method.

Function: Click on the button of a interface to return to the B interface. Click the B Interface button to return to the A interface

The implementation steps are as follows:

#import "FristViewController.h"

@interface Fristviewcontroller ()

@end

@implementation Fristviewcontroller

-(void) viewdidload{

[Super Viewdidload];

}

-(Ibaction) Gotosecondview: (id) method of sender {//a interface

Create an instance of the VC to be launched

secondviewcontroller* secondvc=[[secondviewcontroller alloc]initwithnibname:@ "SecondViewController" bundle:nil];

Launch a new VC from below the current controller

[Self PRESENTVIEWCONTROLLER:SECONDVC animated:yes completion:nil];

}

#import "SecondViewController.h"

@interface Secondviewcontroller ()

@end

@implementation Secondviewcontroller

-(void) viewdidload{

[Super Viewdidload];

}

Return to the first interface

-(Ibaction) GoBack: (id) method of sender {//b interface

[Self dismissviewcontrolleranimated:yes completion:nil];

}

2. Forward-passing values between interfaces

2.1 What is a forward pass value:

When a new controller B is introduced from controller A, a will also pass the data received in the interface to B, and the process of presentation or processing by B

2.2 How to implement the forward value:

Step1: Add exposed properties for controller B to receive incoming values from outside

STEP2: Controller A in order to launch B, will create an instance of B, after the instance is created, before the launch, will be passed in the property that is stored to B exposed to

STEP3: Introduced controller B in viewwillappear to display the data in the interface

3. Inverse value between interfaces

3.1 What is the inverse value?

After a B has been introduced, when it is returned from B to (dismiss) to launch its A, the data is passed back and the process is displayed by a

3.2 How to achieve

A property that can receive a reference is exposed in step1:b. AVC

A property that can receive the returned data is exposed in the STEP2:A message

Before Step3:a introduced B, it passed its reference to B.

Step4:b before dismiss, the data to be returned is passed to the message property that is exposed in the reference to holding a

STEP5: In the viewwillappear of a, displays the contents of the message

2. Method Two:

4. Using a delegate to implement a reverse pass value

Principal: The introduction of the B Agent: The introduction of B's that a

Three things to do by the client:

. h file A. Define protocol 1) protocol name: class name +delegate 2) The first parameter of the method must be the delegate's own 3) method name as much as possible to reflect the timing of the message

. h file B. Add delegate attribute @property (nonatomic,weak) id<bviewcontrollerdelegate> delegate;

. m file C. Choose the right time to send a message to the agent

Three things the agent should do:

A. Compliance Agreement

B. Implementation method

C. Set yourself as an agent

MVC Interface Development

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.