ios17--Custom Control 2 Improvements

Source: Internet
Author: User
Tags uikit

Controller:

// //   XMGViewController.h#import <UIKit/UIKit.h>@interface  Xmgviewcontroller : Uiviewcontroller
@end
////XMGVIEWCONTROLLER.M#import "XMGViewController.h"#import "XMGShop.h"#import "XMGShopView.h"@interfaceXmgviewcontroller () @property (weak, nonatomic) Iboutlet UIView*Shopcarview, @property (weak, nonatomic) Iboutlet UIButton*AddButton, @property (weak, nonatomic) Iboutlet UIButton*RemoveButton, @property (nonatomic, strong) Nsarray*Dataarr;@end@implementationXmgviewcontroller/** Lazy Loading*/-(Nsarray *) dataarr{if(_dataarr = =Nil) {        //Loading Data//1. Get the full pathNSString *datapath = [[NSBundle mainbundle] Pathforresource:@"shopdata.plist"Oftype:nil]; Self.dataarr=[Nsarray Arraywithcontentsoffile:datapath]; //Dictionary Turn Model//creating a temporary arrayNsmutablearray *temparray =[Nsmutablearray array];  for(Nsdictionary *dictinch_dataarr) {            //Create Shop ObjectsXmgshop *shop =[Xmgshop shopwithdict:dict]; //load the model into an array[Temparray Addobject:shop]; } Self.dataarr=Temparray; }    return_dataarr;}//Initializing Data- (void) viewdidload {[Super viewdidload];}/** * Add to Cart * * @param button*/-(Ibaction) Add: (UIButton *) button {/***********************1. Define some constants *****************************/    //1. Total number of columnsNsinteger Allcols =3; //2. Width and height of the productCGFloat width = the; CGFloat height= -; //3. Finding the horizontal and vertical spacingCGFloat Hmargin = (self.shopcarview.frame.size.width-allcols * width)/(Allcols-1); CGFloat Vmargin= (Self.shopCarView.frame.size.height-2* height)/1; //4. Setting the indexNsinteger index =Self.shopCarView.subviews.count; //5. Find the X valueCGFloat x = (hmargin + width) * (index%allcols); CGFloat y= (vmargin + height) * (Index/allcols); /***********************2. Create a product *****************************/    /*Original Separate creation://1. Create a view UIView *shopview = [[UIView alloc] init];      2. Set Frame Shopview.frame = CGRectMake (x, y, width, height);      3. Set the background color Shopview.backgroundcolor = [Uicolor Greencolor];      4. Add to Cart [Self.shopcarview Addsubview:shopview];    5. Create the Uiimageview object of the product uiimageview *iconview = [[Uiimageview alloc] init];    Iconview.frame = CGRectMake (0, 0, width, width);    Iconview.backgroundcolor = [Uicolor Bluecolor];      [Shopview Addsubview:iconview];    6. Create a product title object UILabel *titlelabel = [[UILabel alloc] init];    Titlelabel.frame = CGRectMake (0, Width, width, height-width);    Titlelabel.backgroundcolor = [Uicolor Yellowcolor]; Titlelabel.textalignment = Nstextalignmentcenter;     Center [Shopview Addsubview:titlelabel]; */Xmgshopview*shopview =[[Xmgshopview alloc] init]; Shopview.frame=CGRectMake (x, y, width, height); [Self.shopcarview Addsubview:shopview]; //Shopview.titlelabel = nil;/***********************3. Setting up data *****************************/    //Setting up dataXmgshop *shop =Self.dataarr[index]; /*shopView.iconView.image = [UIImage ImageNamed:shop.icon];     ShopView.titleLabel.text = Shop.name; */[Shopview SetIcon:shop.icon];    [Shopview SetName:shop.name]; /***********************4. Setting the state of the button *****************************/button.enabled= (Index! =5); //5. Set the status of the delete buttonself.removeButton.enabled =YES; }/** * Remove from cart * * @param button*/-(Ibaction) Remove: (UIButton *) button {//1. Delete the last itemUIView *lastshopview =[Self.shopCarView.subviews Lastobject];        [Lastshopview Removefromsuperview]; //3. Set the status of the Add buttonself.addButton.enabled =YES; //4. Set the status of the delete buttonself.removeButton.enabled = (Self.shopCarView.subviews.count! =0); }@end

Custom controls:

////XMGShopView.h#import<UIKit/UIKit.h>@interfaceXmgshopview:uiview//ReadOnly only calls the Get method/** Picture Control*///@property (nonatomic, weak, readonly) Uiimageview *iconview;//[self addsubview:iconview]; there is already a strong pointer reference, here with weak/** Title Control*///@property (nonatomic, weak, readonly) UILabel *titlelabel;//providing interface methods- (void) SetIcon: (NSString *) icon;- (void) SetName: (NSString *) name;@end
////xmgshopview.m#import "XMGShopView.h"@interfaceXmgshopview ()//Extended Properties@property (nonatomic, strong) Uiimageview *iconview;//[self addsubview:iconview]; With a strong pointer pointing, with weak@property (nonatomic, strong) UILabel *Titlelabel;@end@implementationXmgshopview/** * Initialize child controls (do not set frame) **/-(instancetype) init{if(self =[Super Init]) {        //1. Create a Uiimageview object for a productUiimageview *iconview =[[Uiimageview alloc] init]; Iconview.backgroundcolor=[Uicolor Bluecolor];        [Self addsubview:iconview]; _iconview=IconView; //2. Create a product Title objectUILabel *titlelabel =[[UILabel alloc] init]; Titlelabel.backgroundcolor=[Uicolor Yellowcolor]; Titlelabel.textalignment= Nstextalignmentcenter;//Center[self addsubview:titlelabel]; _titlelabel=Titlelabel; }    returnSelf ;}/** * Layout Sub-controls (can get frame)*/- (void) layoutsubviews{//0. Be sure to call Super[Super Layoutsubviews]; //1. Get the dimensions of the current controlCGFloat width =Self.frame.size.width; CGFloat height=Self.frame.size.height; //2. Set the frame of the child controlSelf.iconView.frame = CGRectMake (0,0, width, width); Self.titleLabel.frame= CGRectMake (0, width, width, height-width);}- (void) SetIcon: (NSString *) icon{//Setting up dataSelf.iconView.image =[UIImage Imagenamed:icon];}- (void) SetName: (NSString *) name{//Setting up dataSelf.titleLabel.text =name;}@end

Bean:

////XMGShop.h#import<Foundation/Foundation.h>@interfaceXmgshop:nsobject/** Name of the picture*/@property (nonatomic, copy) NSString*icon;/** Name of the product*/@property (nonatomic, copy) NSString*name;//provides construction methods/*-(Instancetype) Initwithicon: (NSString *) icon Name: (NSString *) name;+ (instancetype) Shopwithicon: (NSString *) Icon Name: (NSString *) name; */-(Instancetype) Initwithdict: (Nsdictionary *) dict;+ (Instancetype) shopwithdict: (Nsdictionary *) dict;@end
////xmgshop.m#import "XMGShop.h"@implementationXmgshop/*-(Instancetype) Initwithicon: (NSString *) icon Name: (NSString *) name{if (self = [super init]) {Self.icon =        Icon    Self.name = name; } return self;} + (Instancetype) Shopwithicon: (NSString *) icon Name: (NSString *) name{return [[Self alloc] Initwithicon:icon name:name]; } */-(Instancetype) Initwithdict: (Nsdictionary *) dict{if(self =[Super Init]) {Self.icon= dict[@"icon"]; Self.name= dict[@"name"]; }    returnSelf ;}+ (Instancetype) shopwithdict: (Nsdictionary *) dict{return[Self alloc] initwithdict:dict];}@end

ios17--Custom Control 2 Improvements

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.