Design Pattern - Objective-C - MVC Model View Controller -
hi read tutorials around web on mvc , read topics on here. think got concept of mvc i'm not sure of implementation.
i've tried apply simple program, window have label , button. button increase counter , label shows value of it.
i tried in 2 different ways.
in first case ( example works ) melt view , controller. said, example works, want guys tell me if it's correct implementation mvc or it's not following right design.
the second example has model view , controller 3 separated class, example doesnt work because v , c import itself, love guys tell me i'm doing wrong.
first version: model, view-controller
//model.h #import <foundation/foundation.h> @interface model : nsobject { int _counter; } -(void)setcounter:(int)valuecounter; -(int)getcounter; -(void)increasecounter; @end //model.m #import "model.h" @implementation model {} -(void)setcounter:(int)valuecounter { _counter = valuecounter; } -(int)getcounter { return _counter; } -(void)increasecounter{ _counter ++; } @end //viewcontroller.h #import <uikit/uikit.h> #import "model.h" @interface viewcontroller : uiviewcontroller { iboutlet uibutton *_button; iboutlet uilabel *_label; model *mymodel; } -(ibaction)send:(id)sender; @end //viewcontroller.m #import "viewcontroller.h" @interface viewcontroller () @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; mymodel = [[model alloc]init]; _label.text = [nsstring stringwithformat:@"%d",[mymodel getcounter]]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; } - (ibaction)send:(id)sender{ [mymodel increasecounter]; _label.text = [nsstring stringwithformat:@"%d",[mymodel getcounter]]; } @end
is way correct pattern mvc ? code works, before start more complex apps want make sure code in way. how app, way of mvc. bad? good? how change or fix it?
second version: model, view, controller separated
----> model
//model.h #import <foundation/foundation.h> @interface model : nsobject { int _count; } -(void)setcount:(int)value; -(int)getcount; -(void)increasecount; @end //model.m #import "model.h" @implementation model -(void)setcount:(int)value { _count = value; } -(int)getcount { return _count; } -(void)increasecount { _count = _count++; } @end
----> view
//view.h #import <uikit/uikit.h> #import "controller.h" @interface viewcontroller : uiviewcontroller{ iboutlet uilabel *label; iboutlet uibutton *button; controller *mycontroller; } @end //view.m #import "viewcontroller.h" #import "controller.h" @interface viewcontroller () @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; mycontroller = [[controller alloc]init]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; } -(ibaction)pressbutton:(id)sender{ label.text = [nsstring stringwithformat:@"%d",[mycontroller actionincrease]]; } @end
----> controller
//controller.m #import <foundation/foundation.h> @class "model.h" @class "viewcontroller.h" @interface controller : nsobject { model *_mymodel; uiviewcontroller *_myviewcontroller; } -(int)actionincrease; @end //controller.m #import "controller.h" #import "model.h" @implementation controller -(id)init{ _mymodel = [[model alloc]init]; } -(int)actionincrease { [_mymodel increasecount]; return [_mymodel getcount]; } @end
version doesn't work because classes view , controller import each other , compiler gives me warning
simply: uiviewcontroller
not view, it's controller
think of uiviewcontroller
puppeteer , uiview
puppet.
uiviewcontroller
controls displayed on uiviewuiview
's main purpose contain subviews.nsobject
can used class, should useduiviewcontroller
.
admittedly, understood better after completing codeschool's tutorial http://www.codeschool.com/courses/try-ios. highly recommend simple hands-on approach.
let's break down:
note: here utilize @property
declarations instead. these save writing own setter , getter methods. (unless need override them custom functionality)
nsobject (model):
//mymodelobject.h #import <foundation/foundation.h> @interface mymodelobject : nsobject @property (nonatomic) int count; @end
uiview (view):
//myview.h #import <uikit/uikit.h> @interface myview : uiview // holds it's own subviews @property (strong, nonatomic) uiview *anotherview; @property (strong, nonatomic) uiimageview *myimageview; @end
uiviewcontroller (controller, comes here!):
//myviewcontroller.h #import <foundation/foundation.h> #import "myview.h" // custom view #import "mymodel.h" // custom model @interface myviewcontroller : uiviewcontroller @property (strong, nonatomic) myview *myview // see how view "owned" view controller? @end //myviewcontroller.m @implementation myviewcontroller @synthesize myview; - (void) somemethod { [myview dosomething]; } @end
Comments
Post a Comment