[Basic iOS control and basic ios Control

Source: Internet
Author: User

[Basic iOS control and basic ios Control
A. storyboard and xib 1. storyboard: relatively important to xib, it controls all interfaces of the entire application 2.xib: lightweight, generally used to describe partial interfaces B. use 1. create an xib File New File ==> User Interface ==> Empty 2. open the new xib file and a visual window appears. (1) drag a UIView (not UIViewController). (2) set the size: Enable customizable size ==> define size (3) drag the icon image, name, and Download button to adjust the settings. 3. get the view in xib in the Code and set the data (1) Get view a from xib. method 1:

1 // 1. obtain the view in xib. You can define multiple views in xib. Note that the name does not contain the extension 2 NSArray * viewArray = [[NSBundle mainBundle] loadNibNamed: @ "app" owner: nil options: nil]; 3 UIView * appView = [viewArray lastObject];
B. Method 2:
1         UINib *nib = [UINib nibWithNibName:@"app" bundle:[NSBundle mainBundle]];2         NSArray *viewArray = [nib instantiateWithOwner:nil options:nil];3         UIView *appView = [viewArray lastObject];
(2) retrieve the elements in the View and set image a. method 1. Use the SubView array:
1 // 3. Set image 2 UIImageView * iconView = appView. subviews [2]; 3 iconView. image = [UIImage imageNamed: appData. icon];
Note: array elements are arranged in the order specified in the tutorial (imageView should be subviews [0], but it is not found in actual programming, so this method is not stable) B. method 2, using tag:
1 // 3. Set image 2 UIImageView * iconView = [appView viewWithTag: 1]; 3 iconView. image = [UIImage imageNamed: appData. icon];
(3) set the name
1 // 4. Set name 2 UILabel * nameLabel = [appView viewWithTag: 2]; 3 nameLabel. text = appData. name;
(4) The Download button has been defined in xib and you do not need to use code C.
1 # import "ViewController. h "2 # import" App. h "3 4 # define ICON_KEY @" icon "5 # define NAME_KEY @" name "6 # define APP_WIDTH 85 7 # define APP_HEIGHT 90 8 # define MARGIN_HEAD 20 9 # define ICON_WIDTH 50 10 # define ICON_HEIGHT 50 11 # define NAME_WIDTH APP_WIDTH 12 # define NAME_HEIGHT 20 13 # define DOWNLOAD_WIDTH (APP_WIDTH-20) 14 # define DOWNLOAD_HEIGHT 20 15 16 @ interface ViewController () 17 18/** Store application information */19 @ property (nonatomic, strong) NSArray * apps; // Application List 20 21 @ end 22 23 @ implementation ViewController 24 25-(void) viewDidLoad {26 [super viewDidLoad]; 27 // Do any additional setup after loading the view, typically from a nib. 28 29 [self loadApps]; 30} 31 32-(void) didReceiveMemoryWarning {33 [super didReceiveMemoryWarning]; 34 // Dispose of any resources that can be recreated. 35} 36 37 # pragma mark get Application List 38-(NSArray *) apps {39 if (nil = _ apps) {40 // 1. obtain the full path 41 NSString * path = [[NSBundle mainBundle] pathForResource: @ "app. plist "ofType: nil]; 42 43 // 2. load Data 44 NSArray * dictArray = [NSArray arrayWithContentsOfFile: path]; 45 46 // 3. convert all the dictionaries in dictArray into a model and put them in the new array. 47 NSMutableArray * appArray = [NSMutableArray array]; 48 for (NSDictionary * dict in dictArray) {49 // 3.1 create model object 50 App * app = [App appWithDictionary: dict]; 51 52 // 3.2 add to app array 53 [appArray addObject: app]; 54} 55 56 _ apps = appArray; 57} 58 59 return _ apps; 60} 61 62 # pragma mark load all application lists 63-(void) loadApps {64 int appColumnCount = [self appColumnCount]; 65 int appRowCount = [self appRowCount]; 66 67 CGFloat margcounter = (self. view. frame. size. width-APP_WIDTH * appColumnCount)/(appColumnCount + 1); 68 CGFloat marginY = (self. view. frame. size. height-APP_HEIGHT * appRowCount)/(appRowCount + 1) + MARGIN_HEAD; 69 70 int column = 0; 71 int row = 0; 72 for (int index = 0; index <self. apps. count; index ++) {73 App * appData = self. apps [index]; 74 75 // 1. get the view in xib. You can define multiple views in xib. Note that the name does not contain the extension 76 // NSArray * viewArray = [[NSBundle mainBundle] loadNibNamed: @ "app" owner: nil options: nil]; 77 // UIView * appView = [viewArray lastObject]; 78 79 UINib * nib = [UINib nibWithNibName: @ "app" bundle: [NSBundle mainBundle]; 80 NSArray * viewArray = [nib instantiateWithOwner: nil options: nil]; 81 UIView * appView = [viewArray lastObject]; 82 83 // 2. define the position and size of each app. 84 CGFloat appX = marginX + column * (marginX + APP_WIDTH); 85 CGFloat appY = marginY + row * (marginY + APP_HEIGHT); 86 appView. frame = CGRectMake (appX, appY, APP_WIDTH, APP_HEIGHT); 87 88 // 3. set image 89 UIImageView * iconView = [appView viewWithTag: 1]; 90 iconView. image = [UIImage imageNamed: appData. icon]; 91 92 // 4. set Name 93 UILabel * nameLabel = [appView viewWithTag: 2]; 94 nameLabel. text = appData. name; 95 96 // 5. add this app information to the total view 97 [self. view addSubview: appView]; 98 99 column ++; 100 if (column = appColumnCount) {101 column = 0; 102 row ++; 103} 104} 105} 106 107 108 # pragma mark calculates the number of columns 109-(int) appColumnCount {110 int count = 0; 111 count = self. view. frame. size. width/APP_WIDTH; 112 113 if (int) self. view. frame. size. width % (int) APP_WIDTH = 0) {114 count --; 115} 116 117 return count; 118} 119 120 # pragma mark calculates the number of rows 121-(int) appRowCount {122 int count = 0; 123 count = (self. view. frame. size. height-MARGIN_HEAD)/APP_HEIGHT; 124 125 if (int) (self. view. frame. size. height-MARGIN_HEAD) % (int) APP_HEIGHT = 0) {126 count --; 127} 128 129 return count; 130} 131 132 @ end

 

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.