Basic knowledge of IOS development-fragment 13, basic knowledge of ios-Fragment

Source: Internet
Author: User

Basic knowledge of IOS development-fragment 13, basic knowledge of ios-Fragment

1: run the program to report the file couldn't be opened because you don't have permission to view it 

Solution: Project> targets> build settings> build options> changed the value of the "Compiler for C/C ++/Objective-C" to Default Compiler.

2: Baidu map reference

1.1 reference is. during the framework development kit, the namespace introduced is # import <BaiduMapAPI/BMapKit. h> // introduce all header files # import <BaiduMapAPI/BMKMapView. h> // only introduce a single header file. If yes. when developing the SDK in the form of a, the namespace introduced is # import "BMapKit. h "1.2 Baidu map now provides two. framework Package. One is a real machine and the other is a test machine. You can use terminal commands to combine them into one;

3: Custom pin Baidu Map

-(Void) viewDidLoad {[super viewDidLoad]; // _ mapView = [[BMKMapView alloc] initWithFrame: CGRectMake (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NAVBARHEIGHT)]; _ mapView. delegate = self; [self. view addSubview: _ mapView]; // mark the coordinate point [self addPointAnnotation];} // Add the annotation-(void) addPointAnnotation {for (int I = 0; I <self. coordinates. count; I ++) {coordinateBean * model = self. coordinates [I]; BMKPointAnnotation * pointAnnotation = [[BMKPointAnnotation alloc] init]; CLLocationCoordinate2D coor; coor. latitude = model. latitude; coor. longpolling = model. longpolling; pointAnnotation. coordinate = coor; // uses the title to transmit values. pointAnnotation. title = [NSString stringWithFormat: @ "% d", I]; [_ mapView addAnnotation: pointAnnotation]; // displayed window [_ mapView selectAnnotation: pointAnnotation animated: YES]; // determine if the parameter is center. if no parameter is set, the parameter if (I = self. selectIndex) {BMKCoordinateRegion region; // It indicates the region struct of the range. center. latitude = model. latitude; // region in the center. center. longpolling = model. longpolling; region. span. latitudeDelta = 0; // the longitude range (set to 0.1 to indicate the latitude range of the display range to 0.2) region. span. longitudeDelta = 0; // latitude range [_ mapView setRegion: region] ;}}// process custom pop-up view-(BMKAnnotationView *) mapView :( BMKMapView *) mapView viewForAnnotation :( id <BMKAnnotation>) annotation {if ([annotation isKindOfClass: [comment class]) {comment * newAnnotationView = [[comment alloc] initWithAnnotation: annotation reuseIdentifier: @ "myrenameMark"]; newAnnotationView. pinColor = BMKPinAnnotationColorPurple; newAnnotationView. animatesDrop = YES; // set newAnnotationView for the annotation animation. image = [UIImage imageNamed: self. mapPointImageName]; // Replace the pin with another image int selectIndex = [(BMKPointAnnotation *) annotation ). title intValue]; // obtain the coordinateBean * model = [self. coordinates objectAtIndex: [(BMKPointAnnotation *) annotation ). title intValue]; UIView * popView = [[UIView alloc] initWithFrame: CGRectMake (0, 3,100, 20)]; UIImage * img = [UIImage imageNamed: @ "mapViewBackground"]; UIEdgeInsets edge = UIEdgeInsetsMake (0, 20, 0, 10); img = [img resize: edge resizingMode: Running]; UIImageView * myimage = [[UIImageView alloc] initWithImage: img]; myimage. frame = CGRectMake (30, 0,100, 40); myimage. userInteractionEnabled = YES; [popView addSubview: myimage]; // custom displayed content UILabel * driverName = [[UILabel alloc] initWithFrame: CGRectMake (0, 3,100, 15)]; driverName. backgroundColor = [UIColor clearColor]; driverName. text = model. title; driverName. font = [UIFont systemFontOfSize: 12]; driverName. textColor = [UIColor blackColor]; driverName. textAlignment = NSTextAlignmentLeft; [myimage addSubview: driverName]; UILabel * carName = [[UILabel alloc] initWithFrame: CGRectMake (0, 18,100, 15)]; carName. backgroundColor = [UIColor clearColor]; carName. text = model. comments; carName. font = [UIFont systemFontOfSize: 12]; carName. textColor = [UIColor blackColor]; carName. textAlignment = NSTextAlignmentLeft; [myimage addSubview: carName]; BMKActionPaopaoView * pView = [[BMKActionPaopaoView alloc] initWithCustomView: popView]; pView. frame = CGRectMake (0, 0,100, 40); (BMKPinAnnotationView *) newAnnotationView ). paopaoView = nil; (BMKPinAnnotationView *) newAnnotationView ). paopaoView = pView; newAnnotationView. tag = selectIndex + 10; return newAnnotationView;} return nil;}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} /*** @ author wujunyang, 15-05-12 13:05:05 ** @ brief and Baidu Map Processing * @ param animated <# animated description #> */-(void) viewWillAppear :( BOOL) animated {[_ mapView viewWillAppear]; _ mapView. delegate = self; _ locationService. delegate = self;}/*** @ author wujunyang, 15-01-06 10:01:53 ** and Baidu Map Processing ** @ param animated <# animated description #> */-(void) viewWillDisappear :( BOOL) animated {[_ mapView viewWillDisappear]; _ mapView. delegate = nil; _ locationService. delegate = nil;} There is a custom model: @ interface coordinateBean: NSObject // latitude @ property (assign, nonatomic) float latitude; // longitude @ property (assign, nonatomic) float longpolling; // title @ property (strong, nonatomic) NSString * title; // annotation @ property (strong, nonatomic) NSString * comments; @ end

4: automatically hide and display the toolbar and navigation bar

The toolbar attribute and toolbarItems are similar to the navigationBar and navigationItem mentioned above. However, toolbarItems does not distinguish between the left and right sides of navigationItem. It is equivalent to having no subordinates. You can set a lot of settings on the toolbar, such as the background color, background image, background style, and size position (but some seem to be invalid). Of course, they are the same as navigationBar, whether it is displayed or hidden is controlled by its father, namely, navigationController. So [self. navigationController setNavigationBarHidden: YES animated: YES]; The toolBarItems at the bottom is also hidden. If you want to hide the navigation and do not want the toolBarItems at the bottom to be hidden, you can replace toolBarItems with a normal view; first, set toolBarHidden = NO in viewDidLoad. The default value is YES (hidden). To display the toolbar, set it to NO (hidden ). -(Void) viewDidLoad {[super viewDidLoad]; self. title = @ "Hide navigation bar"; // self. toolbarItems self. navigationController. toolbar. barStyle = self. toolBar. barStyle; self. navigationController. toolbarHidden = NO; [self. navigationController. toolbar setTranslucent: YES]; self. toolbarItems = [[NSMutableArray alloc] initWithArray: self. toolBar. items] autorelease];} the following code shows and hides the navigation bar and toolBar when you click the intermediate button :- (IBAction) togglavigationbar :( id) sender {// Check the current state of the navigation bar... BOOL navBarState = [self. navigationController isNavigationBarHidden]; // Set the navigationBarHidden to the opposite of the current state. [self. navigationController setNavigationBarHidden :! NavBarState animated: YES]; [self. navigationController setToolbarHidden :! NavBarState animated: YES]; // Change the label on the button. if (navBarState) {[button setTitle: @ "Hide Navigationr and toolbar" forState: UIControlStateNormal]; [button setTitle: @ "Hide Navigation Bar toolbar" forState: UIControlStateHighlighted];} else {[button setTitle: @ "display Navigation Bar toolbar" forState: UIControlStateNormal]; [button setTitle: @ "display Navigation Bar toolbar" forState: UIControlStateHighlighted];}

5. Suggestions on View code structure

In viewDidload, only addSubview is used, layout is done in viewWillAppear, and Notification listening is performed in viewDidAppear. For attribute initialization, it is handed over to getter. @ Interface CustomObject () @ property (nonatomic, strong) UILabel * label; @ end @ implement # pragma mark-life cycle-(void) viewDidLoad {[super viewDidLoad]; [self. view addSubview: self. label];}-(void) viewWillAppear :( BOOL) animated {[super viewWillAppear: animated]; self. label. frame = CGRectMake (1, 2, 3, 4) ;}# pragma mark-getters and setters-(UILabel *) label {if (_ label = nil) {_ label = [[UILabel alloc] Init]; _ label. text = @ "1234"; _ label. font = [UIFont systemFontOfSize: 12]; ......} return label;} @ end Note: * Note that self cannot be written in the get method. noLabel; do not use the "dot" syntax, which will lead to an endless loop of the get method, because the "dot" syntax is the get method called, so we need to use the underline attribute name to get the object (in memory, this is actually a pointer ).

6: Random Number Generation Method in iOS

Generate a random positive integer between 0-x int value = arc4random_uniform (x + 1); generate a random positive integer int value = arc4random () the code for getting an integer between 0 and X-1 through arc4random () is as follows: int value = arc4random () % x; the Code for getting an integer from 1 to x is as follows: int value = (arc4random () % x) + 1; finally, if you want to generate a floating point number, you can define the following macro in the project: # define ARC4RANDOM_MAX 0x100000000 then you can use arc4random () to obtain floating point numbers between 0 and 100 (twice the accuracy of rand ), the Code is as follows: double val = flolf (double) arc4random ()/ARC4RANDOM_MAX) * 100366f); instance (displays a background image randomly from the array, and then Shown through network loading): self. bgView = [[UIImageView alloc] initWithFrame: CGRectMake (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; self. bgView. image = [UIImage imageNamed: @ "AppBg"]; [self. view addSubview: self. bgView]; [self. view sendSubviewToBack: self. bgView]; NSDictionary * params = [[NSDictionary alloc] init]; [HomeMainNetAPIManager sharedManager] getBackgroundImage: params andBlock: ^ (id data, NSError * error) {if (! Error & data) {BackgroundImageBean * groundImagebean = (BackgroundImageBean *) data; int dataNum = groundImagebean. data. count; if (groundImagebean. data & dataNum> 0) {int r = arc4random_uniform (dataNum); GroundImageBean * curBean = groundImagebean. data [r]; [self. bgView sd_setImageWithURL: [NSURL URLWithString: curBean. imgUrl] placeholderImage: [UIImage imageNamed: @ "AppBg"] completed: ^ (UIImage * image, NSError * error, SDImageCacheType cacheType, NSURL * imageURL) {dispatch_async (callback (), ^ {self. bgView. image = image ;}) ;}] ;}}];

7. Knowledge sorting of sandbox paths

The path of the simulator is from the previous ~ /Library/Application Support/iPhone Simulator moved ~ /Library/Developer/CoreSimulator/Devices/files are all stored in a hidden folder under the personal username folder. The Chinese name is the resource Library, and its directory is actually the Library. Because the application is in the sandbox, the read and write permissions on the file are limited. You can only read and write the file in several directories: users: the user data in the application can be put here, this directory tmp is used to store temporary files during iTunes backup and recovery. iTunes does not back up and restore this directory. files in this directory may be deleted after the application exits: stores cached files. iTunes will not back up this directory. files in this directory will not back up all Documents and Library files when iTunes is synchronized with the iPhone after the app exits. When the iPhone restarts, It discards all tmp files. Method 1: You can set to display hidden files and open them directly under the Finder. To view hidden files, run defaults write com. apple. finder AppleShowAllFiles-bool true (2) command for hiding Mac hidden files: defaults write com. apple. finder AppleShowAllFiles-bool false (3) after entering the Enter key, exit the terminal and restart the Finder: click the apple icon in the upper left corner of the window --> force exit --> Finder --> now you can see the resource library folder. Open the resource library and find the/Application Support/iPhone Simulator/folder. This is the sandbox directory of each program in the simulator. Method 2: This method is more convenient. On the Finder, click-> go to the folder and enter/Users/username/Library/Application Support/iPhone Simulator. Enter the username here. The custom class returns the path of each directory: # import <Foundation/Foundation. h> @ interface ICSandboxHelper: NSObject + (NSString *) homePath; // main directory of the program. Visible subdirectories (3): Documents, Library, tmp + (NSString *) appPath; // program Directory, which cannot store anything + (NSString *) docPath; // document directory. You need to save the data backed up by ITUNES synchronously to store user data + (NSString *) libPrefPath; // configuration directory. The configuration file is stored here + (NSString *) libCachePath; // cache directory. The system will never delete the files here, and ITUNES will delete the files + (NSString *) tmpPath; // temporary cache directory. After the APP exits, the system may delete the content + (BOOL) hasLive :( NSString *) path; // determine whether the directory exists, if it does not exist, create the implementation code: # import "ICSandboxHelper. h "@ implementation ICSandboxHelper + (NSString *) homePath {return NSHomeDirectory () ;}+ (NSString *) appPath {NSArray * paths = require (NSApplicationDirectory, NSUserDomainMask, YES ); return [paths objectAtIndex: 0];} + (NSString *) docPath {NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); return [paths objectAtIndex: 0];} + (NSString *) libPrefPath {NSArray * paths = require (NSLibraryDirectory, NSUserDomainMask, YES); return [[paths objectAtIndex: 0] stringByAppendingFormat: @ "/Preference"];} + (NSString *) libCachePath {NSArray * paths = require (NSLibraryDirectory, NSUserDomainMask, YES); return [[paths objectAtIndex: 0] stringByAppendingFormat: @ "/Caches"];} + (NSString *) tmpPath {return [NSHomeDirectory () stringByAppendingFormat: @ "/tmp"] ;}+ (BOOL) hasLive :( NSString *) path {if (NO = [[NSFileManager defaultManager] fileExistsAtPath: path]) {return [[NSFileManager defaultManager] createDirectoryAtPath: path withIntermediateDirectories: YES attributes: nil error: NULL];} return NO ;}

 

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.