Use. xib files in iOS to encapsulate a custom view

Source: Internet
Author: User
Tags uikit

1. Create a custom view that inherits UIView, assuming the class name is called Myappvew

#import <UIKit/UIKit.h>@class  MyApp; @interface Myappview:uiview@property (strong,nonatomic) MyApp*app; +(instancetype) Appview; @end

MyAppView.h

#import "MyAppView.h"#import "MyApp.h"@interfaceMyappview () @property (weak, nonatomic) Iboutlet Uiimageview*IconView, @property (weak,nonatomic) iboutlet UILabel*namelable;@end@implementationMyappview-(void) Setapp: (MYAPP *) app{_app=app; Self.iconView.image=[UIImage ImageNamed:app.icon]; Self.nameLable.text=App.name; //NSLog (@ "%@", App.icon);}+(instancetype) appview{NSBundle*bundle=[NSBundle Mainbundle]; Nsarray*objs=[bundle loadnibnamed:@"Appinfoview"Owner:nil Options:nil]; return[Objs lastobject];}+ (Instancetype) Appviewwithapp: (MYAPP *) app{Myappview*appview=[self appview]; Appview.app=app; returnAppview;}@end

2. Create a new Myappview.xib file to describe the Myappview internal structure

3. Modify the type of UIView to Myappview type

4. Connect the internal child controls to the Myappview property

5.MyAppView provides a model property

@interface myappview ()

@property (weak, nonatomic) iboutlet uiimageview *iconview;

@property (weak,nonatomic) iboutlet UILabel *namelable;

6. Override the Set method of the Model property, because the set method can get the model data passed by the outside

-(void) Setapp: (MYAPP *) app{    _app=app;    Self.iconView.image=[UIImage ImageNamed:app.icon];    Self.nameLable.text=app.name;     // NSLog (@ "%@", App.icon);}

7. Detach the model data and set the data to the corresponding sub-controls respectively

8. Supplement: Provide a class method for creating Myappview that will mask the code that reads the. xib file.

+(instancetype) appview{        *bundle=[NSBundle mainbundle];     *objs=[bundle loadnibnamed:@ "appinfoview"  owner:nil Options:nil];     return [Objs lastobject];} + (Instancetype) Appviewwithapp: (MYAPP *) app{    *appview=[self appview];    Appview.app=app;     return Appview;}

By encapsulating the data, we have a preliminary understanding of MVC.

Use. xib files in iOS to encapsulate a custom view

Related Article

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.