IOS work notes (2), ios work notes

Source: Internet
Author: User

IOS work notes (2), ios work notes

1. Lazy loading (that is, delayed loading) is initialized only when called to prevent resource waste. The get method of the object needs to be rewritten and must be written as a member variable, such as _ imageData. You can write it like this, for example:

1 @ property (nonatomic, strong) NSArray * imageData; 2 3-(NSArray *) imageData {// rewrite the imageData get Method 4 if (_ imageData = nil) {5 // initialize data 6 NSMutableDictionary * image1 = [NSMutableDictionary dictionary]; 7 image1 [@ "icon"] = @ "hello "; 8 image1 [@ "desc"] = @ "this is an image description"; 9 10 NSMutableDictionary * image2 = [NSMutableDictionary dictionary]; 11 image2 [@ "icon"] = @ "hello2"; 12 image2 [@ "desc"] = @ "this is an image description 2"; 13 14 // self. imageData = @ [image1, image2]; // original 15 _ imageData = @ [image1, image2]; // lazy Loading Method 16 17} 18 return _ imageData; 19}

The above get method cannot be written like this

1 -(NSArray *) imageData{2     if(self.imageData == nil){3         //4     }5     return self.imageData;6 }

In this case, the write will be in an endless loop. self. imageData calls the get method of imageData, so you have to write it in the form of a member variable "_ imageData.

 

2. When there is a large amount of data, you can store the data in the plist file, read the plist file in this way, fixed format, three rows.

// A bundle represents a folder, and the mainBundle can be used to access any resources of the mobile phone. NSBundle * bundle = [NSBundle mainBundle]; // obtain the plist file path, which is a full path, instead of obtaining NSString * path = [bundle pathForResource: @ "imageData" ofType: "@" plist "]; _ imageData = [NSArray arrayWithContentOfFile: path] by file name alone; // generally, the path containing the File must be a full path, instead of providing the name as imageNamed.

 

3. This error occurs:

Undefined symbols for architecture arm64:
"_ OBJC_CLASS _ $ _ BMKMapView", referenced from :"..................

This is a program that does not support 64-bit commands. There are two ways to modify it.
① In the target build settings, delete the valid ubuntures, arm64, and set Build Active Architecture Only to NO.

② The other is to modify the ubuntures of ubuntures

${ARCHS_STANDARD_32_BIT}

You can, the original is

$(ARCHS_STANDARD)

4. For the click event of UIButton, if you add a btn to the view

[self addSubView:btn];

If the coordinates of the button are outside the view, the button cannot accept the button event. Others are similar

 

5. viewWithTag can quickly obtain the desired view based on the tag value. However, the tag size is required because the tag value is small, for example, 0--100 is reserved for Apple.

Therefore, we need to set the tag to a large level during customization, such as 100000.

 

6. userInteractionEnabled is the property of UIView. You can set whether the view can accept user events and messages and interact with users. If you do not want to, set it to NO.

For example, a parent view contains two child views a and B. If B is overwritten by a, B cannot respond to the event.

a.userInteractionEnabled = NO;b.userInteractionEnabled = YES;

In this way, B can receive message events.

 

7. keyWindow is used to accept information about the keyboard and non-contact classes, and each program can only have one window that is keywindow.

// Define keywindowUIWindow * keyWindow = [[UIApplication sharedApplication] keyWindow];

 

8. Obtain the text on UIButton. You can use

myButton.titleLabel.text

 

9. Define variable array and initialization

1 @ property (strong, nonatomic) NSMutableArray * allMedicBtn; 2 3-(NSMutableArray *) allMedicBtn {4 if (! _ Allmedical BTN) {5 _ allmedical BTN = [NSMutableArray array]; // 20th notes about the reason for this write 6} 7 return _ allmedical BTN; // return self. allMedicBtn 8 // here _ allMedicBtn is equivalent to self. allMedicBtn, which actually calls the get method of allMedicBtn. Therefore, these two statements are equivalent to 9 // self. allMedicBtn and _ allMedicBtn; 10}

 

10. Differences between [NSMutableArray array] and [[NSMutableArray alloc] init;

[NSMutableArray array] is equivalent to [[NSMutableArray alloc] init] autorelease]. The autorelease object is sometimes release when it is not used, and you have to rebuild it once later.

 

11.Xcode shortcut

Xocde does not have a bracket pair prompt like eclipse. You can only double-click any curly brackets, and the pair of curly brackets will appear in the form of shadows.
Show and close a single method, the shortcut key is command + option + left and right keys, (left-click Close, right-click to expand)
To perform these operations on all methods, add shift, command + option + shift + left and right keys.

 

12. In a common view, some methods can load initWithFrame, such:

1-(id) initWithFrame :( CGRect) frame {2 self = [super initWithFrame: frame]; 3 if (self) {4 // Add selector 5 [self setupChoiceView]; 6} 7 return self; 8}

However, in tableViewCell, it is best to write these methods in initWithStyle to facilitate reuse. For example

1-(instancetype) initWithStyle :( UITableViewCellStyle) style reuseIdentifier :( NSString *) reuseIdentifier2 {3 if (self = [super initWithStyle: style reuseIdentifier: reuseIdentifier]) {4 // custom Method 5 [self setupFTEHitorySwitchBtn]; 6} 7 return self; 8}

 

13.The definition of coordinates in ios. After the UIView is added to the keyWindow, the coordinates of the view are changed. In the ipad horizontal screen program, the home key is on the left when you enter the home page by default, and the coordinate origin is in the upper right corner ).

If it is not added to the keyWindow, the coordinate origin is still in the upper left corner.

 

The keyWindow is defined in appDelegate, for example

1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{2       self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];3       self.window.rootViewController = [[LoginViewController alloc]init];4       [self.window makeKeyAndVisible];5 6       return YES;7 }

You can solve this problem (this is to add view in view)

① Add a new method in the. h file of view. For example

1 @ class HomeViewController; // provides the interface for the class to be used in the following method. 2 @ interface MedicineSelectView: UIView3-(id) initWithFrame :( CGRect) frame andSuperViewController :( HomeViewController *) home; 4 @ end

② Add a custom method to the. m file of the view. The original method is no longer needed. The original

1 -(id)initWithFrame:(CGRect)frame{2     self = [super initWithFrame:frame];3     if(self){4         [self setupAddMedicineBtn];5     }6     return self;7 }

At this time, the above method should be changed

1-(id) initWithFrame :( CGRect) frame andSuperViewController :( HomeViewController *) home {2 self = [super initWithFrame: frame]; 3 if (self) {4 self. home = home; // of course, you still need. m declares 5/** 6 * @ property (weak, nonatomic) UIViewController * home; 7 * because of self. home = home the home on the left of the medium number refers to the home of the property, and the home on the right of the equal number refers to the parameter 8 */9 in the method [self setupAddMedicineBtn]; 10} 11 return self; 12}

③ Adding a view to the view. m file also changes. It was originally added to the keyWindow, as shown in figure

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];[keyWindow addSubView:self.allMedicineView];

The changed method is to add the view to the view where the controller is located.

[self.home.view addSubview:self.allMedicineView];

④ In the controller to add the view, the size and position of the view must also be changed.
It turns out that

MedicineSelectView *medicineView = [[MedicineSelectView alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];

After the change

MedicineSelectView * medicineView = [[MedicineSelectView alloc] initWithFrame: CGRectMake (0, 0,300, 44) andSuperViewcontroller: self]; // self indicates the Controller to add the view.

 

14.The ipad solves the problem of hiding text boxes on the keyboard. This method is simple and is used to listen to text boxes. If there are two text boxes userName and passWord (both UITextField), you can do this.

① Add a listening event to the two text boxes. Here, we only use userName as an example.

1 [userName addTarget:self action:@selector(textFieldBeginEdit:) forControlEvents:UIControlEventEditingDidBegin];2 [userName addTarget:self action:@selector(textFieldEndEdit:) forControlEvents:UIControlEventEditingDidEnd];

② Add a specific moving Method

// When you start editing, move up as a whole-(void) textFieldBeginEdit :( UITextField *) textField {[self moveView:-230];} // After finishing editing, move back to the original location-(void) textFieldEndEdit :( UITextField *) textField {[self moveView: 230];} // specific method for moving the view-(void) moveView :( float) move {NSTimeInterval animationDuration = 0.3f; CGRect frame = self. view. frame; frame. origin. x + = move; // the x axis of the view moves up self. view. frame = frame; [UIView beginAnimations: @ "2" context: nil]; // 2 is the animation identifier [UIView setAnimationDuration: animationDuration]; self. view. frame = frame; [UIView commitAnimations];}

③ Hide the keyboard when you click the "Hide keyboard" button in the lower right corner of other areas and in viewDidLoad.

-(Void) viewDidLoad {// click another area to close the keyboard UITapGestureRecognizer * gesture = [[using alloc] initWithTarget: self action: @ selector (hideKeyBoard)]; gesture. numberOfTapsRequired = 1; // confirm that you click another region, rather than double-click another operation [self. view addGestureRecognizer: gesture]; // This method is particularly important // click the "Hide keyboard" button in the lower right corner of the keyboard to close the keyboard and restore the view position [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (hideKeyBoard) name: UIKeyboardWillHideNotification object: nil];} // hide the keyboard and restore the initial position of the view-(void) hideKeyBoard {[userName resignFirstResponder]; [passWord resignFirstResponder]; [self resumeView];} // restore the original view location-(void) resumeView {NSTimeInterval animationDuration = 0.5f; [UIView beginAnimations: @ "3" context: nil]; [UIView setAnimationDuration: animationDuration]; [UIView commitAnimations];}

 

15. the reuse of UITableViewCell. For example, if a table contains 100 rows of data and only 10 rows can be displayed on each page, 11th rows will appear when the row is down and 1st rows will disappear. In order to save memory, it is necessary to reuse the cell of 1st rows to 11th rows. The Code is as follows:

+ (Instancetype) cellWithTableView :( UITableView *) tableView {static NSString * ID = @ "MedicineProcessCell"; MedicineProcessView * cell = [tableView progress: ID]; // defines a cell, find the UITableViewCell with the MedicineProcessCell identifier in the reusable queue of tableView. It has been reused. // If the queue has such a UITableView, assign it to the cell. If there is no UITableView, then return nil to cell if (! Cell) {cell = [[MedicineProcessView alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: ID];} return cell ;}

 

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.