IOS work notes (1), ios work notes
Note: I recorded some trivial things of learning IOS, some of which seem very naive now, but I can leave it alone!
1. The xib development ipad uses a landscape screen. You can set orientation to landscape in the xib view, and portrait to a portrait screen.
2. After declaring UITextField in the. m file, if you want to obtain it. For example, to obtain pwd
@ Property (nonatomic, weak) UITextField * pwd;
You can use self. pwd. text.
_ Pwd. text; // This can also be obtained.
Because self. view actually calls the pwd get method. Yes
1 -(UITextField *)pwd{2 return _pwd;3 }
3. compare strings to see if they are equal
1 NSString * str1 = self. pwdField. text; 2 if ([str1 is1_tostring: @ "123"]) {3 // other operations 4}
4. Create a new project. If you do not want to use the built-in viewController and storyboard to delete it, the main interface is blank after the xib file is created. In this case, we can solve the problem by defining the actual view.
① In the Supporting Files folder, edit the plist file. Leave the Main storyboard file base name empty (because the storyboard file has been deleted ).
② In Appdelegate. m
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
Method. To display LoginViewController, write it like this. Fixed format
1 self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds]; 2 self. window. rootViewController = [[LoginViewController alloc] init]; 3 [self. window makeKeyAndVisible]; // visible to the view and displayed on the screen
5. When connecting xib files, you must connect the view. Otherwise, there will be no screen programs. Remember.
6. Use the following code to display controls. For example:
1 [self.view addSubview:btn1];2 [self.view addSubview:btn2];
You can use
1 [btn1 addTarget: self action: @ selector (btnPressed :) forControllerEvents: Events]; 2-(void) btnPressed :( id) sender {3 UIButton * btn = (UIButton *) sender; 4 // event code of the button 5}
Of course there are also the following forms, it is best to use the above. (Id) sender this parameter refers to the button itself,
1-(void) login {// This function has no parameters and is prone to errors. 2 // event 3}
7. Buttons are generally not defined in this way.
1 CGRect frame4 = CGRectMake(430, 200, 200, 100);2 UIButton *loginBtn = [[UIButton alloc]initWithFrame:frame4];
It is generally defined in this way.
1 loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];2 loginBtn.frame = frame4;
8. Set the root controller, which must be written in AppDelegate. Generally, this format is fixed and must be
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {}
. For example:
1-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {2 // It must be written on the frontend. Otherwise, 3 self does not appear in the view. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]; 4 // to display other things, such as Baidu map, declare 5 self. mapManager = [[BMKManager alloc] init]; // Delete 6 7 if you do not want to display it. // define MainViewController as the root controller 8 UINavigationController * controller = [UINavigationController alloc] initWithRootViewController: [[MainViewController alloc] init]; 9 self. window. rootViewController = controller; 10 [self. window makeKeyAndVisible]; 11 self. window. backgroundColor = [UIColor whiteColor]; 12 return YES; 13 14}
If you do not want to use UINavigationController, you can directly define
self.window.rootViewController = [[LoginViewController alloc]init];
Delete the line of code that defines the root controller.
Next, there are two ways to jump between pages.
[Self presentViewController: mainView animated: YES completion: nil]; // attributes of view [self. navigationController pushViewController: mainView animated: true]; // you must set the root controller. Otherwise, this method is invalid.
9. The content displayed on the homepage during program loading is implemented in the following method, and [super viewDidLoad] cannot be omitted.
1-(void) viewDidLoad {2 [super viewDidLoad]; 3 // other code 4}
10. @ property properties
Weak: generally used for UI components, such as UIButton, UILabel, and UITextField.
Strong: used for general objects, such as arrays.
Copy: used for NSString
Assign: used for basic types
There is a difference between xib and pure code. In xib, weak and subViews are used for UI components, and the subViews array will strongly reference these components. When pure code is written, strong attributes will be used; otherwise, a message will be reported.
Assigning retained object to weak variable; object will be released after assignment
Remember.
The usage example of NSArray and NSDictionary. The dictionary itself is a key-value pair, where the key is icon and desc, and the key of different images corresponds to different values.
1 @ property (nonatomic, strong) NSArray * imageData; 2 NSMutableDictionary * image1 = [NSMutableDictionary dictionary]; 3 image1 [@ "icon"] = @ "hello "; // belongs to the key-Value Pair Form 4 image1 [@ "desc"] = @ "this is an image description"; 5 6 NSMutableDictionary * image2 = [NSMutableDictionary dictionary]; 7 image2 [@ "icon"] = @ "hello2"; 8 image2 [@ "desc"] = @ "this is an image description 2 "; 9 10 // quickly create an array 11 self. imageData = @ [image1, image2];
When the program uses a large number of keys, to prevent key misspelling, you can define macro constants, such
#define IconKey @"icon"
When @ "icon" is used in the future, you can use IconKey instead.