Ios20--xib2

Source: Internet
Author: User

Story Board Controller:

////VIEWCONTROLLER.M//03-View of custom items via Xib#import "ViewController.h"#import "XMGShopView.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //Load Xib//Xmgshopview *shopview = [[[NSBundle Mainbundle] loadnibnamed:@ "Xmgshopview" Owner:nil Options:nil] firstObject];< /c10>//Xmgshopview *shopview = [[Xmgshopview alloc] Initwithframe:cgrectmake ([ +], []];Xmgshopview*shopview =[Xmgshopview Shopview]; Shopview.frame= CGRectMake ( -, -, the, -); //setting properties for child controls    /*Uiimageview *imageview = [Shopview viewwithtag:100];        UILabel *titlelabel = [Shopview viewwithtag:200];    Imageview.image = [UIImage imagenamed:@ "Danjianbao"];    Titlelabel.text = @ "one shoulder bag"; */[Shopview setName:@"One Shoulder bag"]; [Shopview SetIcon:@"Danjianbao"]; [Self.view Addsubview:shopview];}@end

Xib the corresponding class:

// //   XMGShopView.h#import <UIKit/UIKit.h>@interface  xmgshopview:uiview // provides a set method -(void) SetIcon: (NSString *) icon; -(void) SetName: (NSString *) name; // provides fast creation method + (instancetype) Shopview; @end
//xmgshopview.m/** Xib Use precautions: 1> If a view is loaded from xib, it cannot be used [XXX alloc] init] and [xxx alloc] initWithFrame:] Create 2> If a xib is often used, it should be provided Quick Construct class method 3> If a view is loaded from Xib: Add some child controls in the code, Initwithcoder: and awakefromnib Create 4> If a view loads from Xib, it calls Initwithco Der: And awakefromnib, do not call Init and initWithFrame: Method*/#import "XMGShopView.h"@interfaceXmgshopview () @property (weak, nonatomic) Iboutlet Uiimageview*IconView; @property (Weak, nonatomic) Iboutlet UILabel*Titlelabel; /** Test Label*/@property (nonatomic, weak) UILabel*label; /** Frosted Glass*/@property (nonatomic, weak) Uitoolbar*ToolBar;@end@implementationXmgshopview/** * If view is loaded from Xib, init and initwithframe are not called: Method **//*-(Instancetype) init{if (self = [super init]) {NSLog (@ "%s", __func__); } return self;}    -(Instancetype) initWithFrame: (CGRect) frame{if (self = [Super Initwithframe:frame]) {NSLog (@ "%s", __func__); } return self;} *//** * If view is loaded from Xib, it will call Initwithcoder: Method * To create child controls,... Note: If the child control (Uiimageview,uilabel) is created from Xib, it is in an awake state*/-(Instancetype) Initwithcoder: (Nscoder *) adecoder{if(self =[Super Initwithcoder:adecoder]) {        /*UILabel *label = [[UILabel alloc] init];         Label.backgroundcolor = [Uicolor Graycolor];         Label.text = @ "haha haha haha";         [Self Addsubview:label];        Self.label = label; */NSLog (@"1"); }    returnSelf ;}#pragmaMark-xib Loading Principle-(UIView *) loadformnib{//The load should return to view. Xmgshopview *shopview =[[Xmgshopview alloc] initwithcoder:nil]; Shopview.frame= CGRectMake (0,0, the, -); Uiimageview*iconview =[[Uiimageview alloc] initwithcoder:nil]; Iconview.backgroundcolor=[Uicolor Greencolor]; Iconview.frame= CGRectMake (0,0, the, the); Iconview.tag= -;    [Shopview Addsubview:iconview]; Self.iconview=IconView; UILabel*label =[[UILabel alloc] initwithcoder:nil]; Label.backgroundcolor=[Uicolor Orangecolor]; Label.tag= $;    [Shopview Addsubview:label]; Self.titlelabel=label; returnShopview;}/** * Wake-up from Xib child controls to add child controls created in Xib*/- (void) awakefromnib{//Add frosted glass to the ImageViewUitoolbar *toolbar =[[Uitoolbar alloc] init];    [Self.iconview Addsubview:toolbar]; Self.toolbar=ToolBar; NSLog (@"2");}#pragmaMark-Quick Construction method +(instancetype) shopview{return[[[NSBundle Mainbundle] loadnibnamed:@"Xmgshopview"Owner:nil Options:nil] firstobject];}#pragmaMark-Layout child controls-(void) layoutsubviews{[Super Layoutsubviews]; /*self.label.frame = self.bounds; */Self.toolBar.frame=self.iconView.bounds;}#pragmaMark-Set data-(void) SetIcon: (NSString *) icon{Self.iconView.image=[UIImage Imagenamed:icon];}- (void) SetName: (NSString *) name{Self.titleLabel.text=name;}@end

Ios20--xib2

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.