In a typical MVC structure, the model part is responsible for saving the target data, the view part is responsible for implementing the data interface and displaying the data, both work together under the controller's operation. In iOS apps, the implementation of view is primarily implemented by UIView and its derived classes, which display different information primarily by classes such as Uilabel, Uiimageview, and so on.
Here is a demo to illustrate an individual's view of the interaction between the UIView and the data, and personal opinions are for reference only and are welcome to discuss.
1. First create a UIView subclass to customize our view objects.
Header file:
#import <UIKit/UIKit.h> @interface userinfoview:uiview//@property (nonatomic,copy) NSString *imgstring;//@ Property (nonatomic,copy) NSString *namestring;//@property (nonatomic,copy) nsstring *addrstring;//@property ( nonatomic,copy) nsstring *infostring;//@property (nonatomic,copy) nsstring *countstring;//@property (nonatomic,copy) NSString *attstring;//@property (nonatomic,copy) nsstring *fansstring; @property (nonatomic,retain) nsdictionary * param;-(void) loaddata; @end
M File:
#import "UserInfoView.h" #import "RectButton.h" @interface Userinfoview ()//ui control @property (Nonatomic,retain) Uiimageview *userimage; @property (nonatomic,retain) UILabel *namelabel; @property (Nonatomic,retain) UILabel *addresslabel, @property (nonatomic,retain) UILabel *infolabel; @property (nonatomic,retain) UILabel *count Label; @property (nonatomic,retain) Rectbutton *attbutton; @property (nonatomic,retain) Rectbutton *fansbutton; @prop Erty (Nonatomic,retain) UIButton *profilebutton, @property (nonatomic,retain) UIButton *morebutton;//data member//@pro Perty (nonatomic,copy) nsstring *imgstring; @property (nonatomic,copy) nsstring *namestring; @property (nonatomic,copy) NSString *addrstring; @property (nonatomic,copy) nsstring *infostring; @property (nonatomic,copy) NSString *countstring ;//@property (nonatomic,copy) nsstring *attstring;//@property (nonatomic,copy) NSString *fansstring;@ End@implementation userinfoview-(ID) init{CGRect framerect = CgrecTmake (0, 0, 320, 200); self = [self initwithframe:framerect]; if (self) {NSLog (@ "Init called"); } return self;} -(ID) initWithFrame: (cgrect) frame{self = [super Initwithframe:frame]; if (self) {self.backgroundcolor = [uicolor Lightgraycolor]; _userimage = [[Uiimageview alloc] Initwithframe:cgrectzero]; [Self addsubview:_userimage]; _namelabel = [[UILabel alloc] Initwithframe:cgrectzero]; [Self Addsubview:_namelabel]; _addresslabel = [[UILabel alloc] Initwithframe:cgrectzero]; [Self Addsubview:_addresslabel]; _infolabel = [[UILabel alloc] Initwithframe:cgrectzero]; [Self Addsubview:_infolabel]; _attbutton = [[Rectbutton alloc] Initwithframe:cgrectzero]; [Self Addsubview:_attbutton]; _fansbutton = [[Rectbutton alloc] Initwithframe:cgrectzero]; [Self Addsubview:_fansbutton]; _profilebutton = [[UIButton alloc] Initwithframe:cgrectzero]; [Self Addsubview:_profilebutton]; _morebutton = [[UIButton alloc] Initwithframe:cgrectzero]; [Self Addsubview:_morebutton]; _countlabel = [[UILabel alloc] Initwithframe:cgrectzero]; [Self Addsubview:_countlabel]; } return self;} -(void) SetParam: (nsdictionary *) param{_param = param; _namestring = [_param objectforkey:@ "Name"]; _addrstring = [_param objectforkey:@ "Address"]; _infostring = [_param objectforkey:@ "infomation"]; _countstring = [_param objectforkey:@ "Count"]; [Self loaddata];} -(void) layoutsubviews{_userimage.frame = CGRectMake (20, 20, 80, 80); _userimage.backgroundcolor = [Uicolor Yellowcolor]; _namelabel.frame = CGRectMake (120, 20, 180, 20); _namelabel.backgroundcolor = [Uicolor Yellowcolor]; _addresslabel.frame = CGRectMake (120, 50, 180, 20); _addresslabel.backgroundcolor = [Uicolor Yellowcolor]; _infolabel.frame = CGRectMake (120, 80, 180, 20); _infolabel. backgroundcolor = [Uicolor Yellowcolor]; _attbutton.frame = CGRectMake (20, 110, 60, 60); _attbutton.backgroundcolor = [Uicolor Greencolor]; _fansbutton.frame = CGRectMake (93, 110, 60, 60); _fansbutton.backgroundcolor = [Uicolor Greencolor]; _profilebutton.frame = CGRectMake (167, 110, 60, 60); _profilebutton.backgroundcolor = [Uicolor Greencolor]; _morebutton.frame = CGRectMake (240, 110, 60, 60); _morebutton.backgroundcolor = [Uicolor Greencolor]; _countlabel.frame = CGRectMake (20, 180, 280, 15); _countlabel.backgroundcolor = [Uicolor Whitecolor]; [Self loaddata];} -(void) loaddata{if (self.nameString.length! = 0) {_namelabel.text = self.namestring; } if (Self.addrString.length! = 0) {_addresslabel.text = self.addrstring; } if (Self.infoString.length! = 0) {_infolabel.text = self.infostring; } if (Self.countString.length! = 0) {_countlabel.text = self.countstring; }} @end
When this userinfoview is built, the sub-views are created in the initWithFrame, but only a new object is created, and its frame is set to 0. In addition, the INIT function is overridden to set the specified view size in the function so that no parameters need to be specified directly to the specified value when the controller creates a new view.
The settings for each child view in the Userinfoview, completed in Layoutsubview, including setting the frame and background color of the sub-view. The Layoutsubview function may be called frequently, mainly by the following situations:
- When Addsubview is called, the Layoutsubview of the added view and its child views are called;
- When the frame of the view changes, the view's layoutsubview is called;
- The Layoutsubview of the view and its parent view are called when scrolling uiscrollview;
- When rotating a device;
- When the setneedlayout message is sent to the view.
2. The controller sends the data to the view.
The Viewcontroller class is implemented as follows:
#import "ViewController.h" #import "UserInfoView.h" @interface Viewcontroller () @property (nonatomic,retain) UIButton * People1, @property (nonatomic,retain) UIButton *people2; @property (nonatomic,retain) Userinfoview *userview;@ End@implementation viewcontroller-(void) viewdidload{[Super viewdidload];//do no additional setup after loading the View, typically from a nib. _userview = [[Userinfoview alloc] init]; [Self.view Addsubview:_userview]; _people1 = [UIButton Buttonwithtype:uibuttontypesystem]; _people1.frame = CGRectMake (20, 240, 120, 40); [_people1 settitle:@ "Zhang San" forstate:uicontrolstatenormal]; _people1.backgroundcolor = [Uicolor Lightgraycolor]; [_people1 addtarget:self Action: @selector (Setpeople1data) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:_people1]; _people2 = [UIButton Buttonwithtype:uibuttontypesystem]; _people2.frame = CGRectMake (180, 240, 120, 40); [_people2 settitle:@ "John Doe" Forstate:uicontrolstatenorMAL]; _people2.backgroundcolor = [Uicolor Lightgraycolor]; [_people2 addtarget:self Action: @selector (Setpeople2data) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:_people2]; /* view.namestring = @ "Zhang San"; view.addrstring = @ "Beijing"; View.infostring = @ "Student"; view.countstring = @ "12345"; view.namestring = @ "John Doe"; view.addrstring = @ "Shanghai"; View.infostring = @ "Engineer"; view.countstring = @ "54321"; */}-(void) setpeople1data{NSLog (@ "setpeople1data called."); Nsdictionary *param = @{@ "Name": @ "Zhang San", @ "Address": @ "Beijing", @ "infomation": @ "Student", @ "Count": @ "12345"}; _userview.param = param;} -(void) setpeople2data{NSLog (@ "setpeople2data called."); Nsdictionary *param = @{@ "Name": @ "John Doe", @ "Address": @ "Shanghai", @ "infomation": @ "engineer", @ "Count": @ "54321"}; _userview.param = param;} -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
On the default view of Viewcontroller, two buttons are implemented and the response function is set separately. Our goal is to change the data displayed in the Userinfoview by selecting different buttons. From the implementation of the two response functions Setpeople1data and Setpeople2data, it is known that the information required by Userinfoview is encapsulated in a typical variable param of a word, and only one operation is made on the change of view. The dictionary variable is assigned to a property of the Userinfoview instance, which changes the display information in such a way as to change the target view properties. In this way, the controller does not care about how the Userinfoview instance resolves the dictionary parameters, nor does it need to perform other operations on the instance, and only needs to be assigned once when the data needs to be updated. This maximizes the decoupling of the controller and view, and improves the logic simplicity and reusability of the code.
Go back to the implementation method in the Userinfoview class. How do you implement rewriting data for your own child views while the dictionary property changes? The method is simple. First, detach the code that overrides the child view data into the LoadData function, and then rewrite the Nsdictionary *param the property's set method (that is, SetParam). You can then call the LoadData method in the Set method and the Layoutsubview method.
Demo DOWNLOAD Link: click here.
"Some summary of iOS7" an effective method of using UIView