Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add and remove controls through buttons in iOS

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to add and delete controls through buttons in iOS. It is very detailed and has a certain reference value. Friends who are interested must read it!

Let's first talk about the main part of the demo, that is, adding and deleting icons through buttons.

Analysis:

1. Each icon needs two pieces of data, namely, the picture and the description string, so create an Item class to encapsulate the data read from the plist file:

1) the plist file is as follows:

2) Item class:

.h file

# import @ interface Item: the string @ property (nonatomic,copy) NSString * desStr;// picture path @ property (nonatomic,copy) NSString * imgPath;- (instancetype) initWithString: (NSString *) desStr andimgPath: (NSString *) imgPath;@end described by NSObject//

M file

# import "Item.h" @ implementation Item- (instancetype) initWithString: (NSString *) desStr andimgPath: (NSString *) imgPath {self = [super init]; if (self) {self.desStr = desStr; self.imgPath = imgPath;} return self;} @ end

2. Then create a Model class to encapsulate a custom icon model. My model is to inherit the Model class from the UIScrollView class, then set it to scroll, and then create a button that occupies the size of the entire scrollable part of the scrollview to add. Then add the UIImageView display picture in the upper part of button, and add the UILabel display description text in the lower part. The structure is as follows

Override the init method of model to initialize with the item object when the object is created: the model class:

1) .h file

# import # import "Item.h" @ interface Model: UIScrollView@property (nonatomic,strong) UIButton * button;@property (nonatomic,strong) UILabel * label;// to determine whether button is clicked @ property (nonatomic,assign) BOOL isClicked;- (instancetype) initWithItem: (Item *) item;// reset Model-(void) resetModel;@end

2) .m file

-(instancetype) initWithItem: (Item *) item {self = [super initWithFrame:CGRectMake (20,20,80,100)]; if (self) {/ / set module self.contentSize = CGSizeMake (80, self.frame.size.height * 2); self.pagingEnabled = NO; / / set module attribute self.button = [UIButton alloc] initWithFrame:CGRectMake (0,0, self.frame.size.width, self.contentSize.height)]; [self.button addTarget:self action:@selector (buttonDidClicked) forControlEvents:UIControlEventTouchUpInside] / / add picture view to button UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake (0,0, self.frame.size.width, self.frame.size.height)]; [imgView setImage: [UIImage imageNamed:item.imgPath]]; [self.button addSubview:imgView]; / / set whether button is clicked self.isClicked = NO; [self addSubview:self.button] Self.label = [[UILabel alloc] initWithFrame:CGRectMake (0, self.frame.size.height, self.frame.size.width, self.frame.size.height)]; self.label.text = item.desStr; self.label.font = [UIFont systemFontOfSize:15]; self.label.textColor = [UIColor blackColor]; self.label.numberOfLines = 0; self.label.textAlignment = NSTextAlignmentLeft; [self addSubview:self.label];} return self;}

3) Click event of button: that is, click the image description and it will rise from below, and then click on it and it will go down:

/ label rise and fall-(void) buttonDidClicked {if (self.isClicked = = NO) {[UIView animateWithDuration:0.5 animations: ^ {self.contentOffset = CGPointMake (0, self.frame.size.height);}]; self.isClicked = YES;} else if (self.isClicked = = YES) {[UIView animateWithDuration:0.5 animations: ^ {self.contentOffset = CGPointMake (0,0);}]; self.isClicked = NO;}}

In addition, because you must ensure that a picture is displayed each time you add a model to a view, you need a way to restore it to its original state, that is, once it is removed from the view:

/ / restore-(void) resetModel {self.contentOffset = CGPointMake (0,0); self.isClicked = NO;}

3. When the model is ready, write a method in the viewController class to read the plist file data and encapsulate it in the item object, then initialize the model object with the item object, and store all model objects in the variable array (_ allItems):

/ / load data to items-(void) loadData {/ / read data NSString * filePath = [[NSBundle mainBundle] pathForResource:@ "shop" ofType:@ "plist"]; NSArray * itemArr = [NSArray arrayWithContentsOfFile:filePath]; / / create a model for (int I = 0Traci

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report