1, Uipickerview Introduction
-
Uipickerview inherits from UIView and complies with the Protocol; the height of the Uipickerview (height = 162) cannot be changed, but the row height in the Uipickerview can be changed.
-
Uipickerview two data source methods that must be implemented:
-
Uipickerview Common Properties:
-
Uipickerview Proxy Method:
#pragma mark-<uipickerviewdelegate>/** * pickerview each column width */-(cgfloat) Pickerview: (Uipickerview *) Pickerview widthforcomponent: (nsinteger) component{ return 100;} /** * pickerview each row height */-(cgfloat) Pickerview: (Uipickerview *) Pickerview rowheightforcomponent: (NSInteger) component{ return 60;} /** * Pickerview Each line of content */-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) row Forcomponent: (Nsinteger) component{ return @ "He Hua";} /** * Pickerview Display string, String Rich Text property (can set string font size, font color ...), return nil does not affect the display text */-(nsattributedstring *) Pickerview: ( Uipickerview *) Pickerview Attributedtitleforrow: (nsinteger) Row forcomponent: (Nsinteger) component{ return nil;} /** * Pickerview Each row shows the view, when with pickerView:titleForRow:forComponent: simultaneous implementation, first call */-(UIView *) Pickerview: ( Uipickerview *) Pickerview Viewforrow: (nsinteger) Row forcomponent: (Nsinteger) component Reusingview: (Nullable UIView * ) view{ return nil;} /** * Pickerview Called when a row or column is selected, the */-(void) Pickerview will be called only if the user is dragged: (Uipickerview *) Pickerview Didselectrow: (Nsinteger) Row incomponent: (nsinteger) component { NSLog (@ "%s", __func__);}
-
Uipickerview Display ? Problems:
- Cause: Only the data source is set, and the proxy implementation is not set to return the content pickerview to display; Therefore, Pickerview displays the default
? .
-
Click the Random order button to achieve random ordering:
/*** random order */-(ibaction) random { for (int i = 0; I < self. Foods.count; i++) { //1. Random number int random = Arc4random_uniform ((u_int32_t) [Self.foods[i] count]); //2. Select the Pickerview column, row [Self.pickview selectrow:random incomponent:i animated:yes]; //3. Sets the randomly selected data to the corresponding label, where the row remembers no more using the random number //[self Pickerview: Self.pickview Didselectrow:arc4random_uniform ((u_int32_t) [Self.foods[i] count]) incomponent:i]; [self PickerView:self.pickView didselectrow:random incomponent:i]; }}
- There are two important things to consider when implementing a random order:
- When using the random number function, be aware that each call generates a new random number, if you want to use the same random number multiple times; You need to define a variable to hold the random number you want to use multiple times.
- When calling Pickerview
selectRow:inComponent:animated: , the proxy method of Pickerview is pickerView:didSelectRow:inComponent:方法 not invoked and requires us to call it manually; However, the proxy method is called automatically when the user drags the Pickerview pickerView:didSelectRow:inComponent:方法 .
2, Uipickerview implementation of the national Flag selection (MVC mode)
- Model (models): Hfflag
- Inherit from NSObject class, set properties to save data;
- View: Hfflagview
- Using Xib to encapsulate the flag view, set the control data in the data model's set method;
- Controller (Controllers): Controller
- Lazy Load Read plist file, get data;
- Implement the data source method;
- Implements the proxy method, returning the Uipickerview custom display view.
3, Uidatepicker Introduction
-
Uidatepicker inherited from Uicontrol:
-
Three main steps to using Uidatepicker:
- <1> Create Uidatepicker:
// 1. 创建UIDatePicker: 具有默认尺寸UIDatePicker *datePicker = [[UIDatePicker alloc] init];
-
<2> Set uidatepicker display mode:
// 2. 设置UIDatePicker显示模式datePicker.datePickerMode = UIDatePickerModeDate;
- Uidatepicker display Mode:
-
<3> Set Uidatepicker display time zone:
// 3. 设置UIDatePicker时区datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];// datePicker.locale = [NSLocale currentLocale]; // 当系统语言为中文时与上面代码等价// datePicker.locale = [NSLocale systemLocale];
-
Uidatepicker Monitoring Time Change:
4, Uitextfield Agent method
- To implement the Uitextfield proxy method, the agreement must be adhered to first; Become an agent of Uitextfield.
#pragma mark-<UITextFieldDelegate> /** * @return YES: text box allows editing & nbsp * @return No: text box does not allow editing */ -(BOOL) textfieldshouldbeginediting: (Uitextfield *) textfield{ return no; } /** * text box becomes first responder */ -(void) textfielddidbeginediting: (Uitextfield *) textfield{ NSLog (@ "%@", TextField); } /** * allow the current text box to end edit * * @return YES: The text box allows you to end editing and the text box you are currently editing resigns from the first responder * @return No: The current text box does not allow you to end editing until the Bundle */ -(BOOL) textfieldshouldendediting: (Uitextfield *) textfield{ return no; } /** * text box has ended edit */ -(void) Textfielddidendediting: (Uitextfield *) textfield{ NsloG (@ "%s", __func__); } /** * text can display input * * @param range The current text box cursor position, and the cursor position after input * @param string Replace range content in text box * * @return YES: The contents of the Input text box can be replaced by the range range, the cursor moves * @return No: text box cannot be replaced, cursor does not move */ -(BOOL) TextField: (Uitextfield * ) TextField Shouldchangecharactersinrange: (nsrange) Range replacementstring: (NSString *) string{ return no; } /** * Just click the text box to clear the button, call this method * * @return YES: Click the empty button to empty the text box * @return No: Click the empty button to ignore, the text box content does not empty   ; */ -(BOOL) Textfieldshouldclear: (Uitextfield *) textfield{ return YES; } /** * Just click on the keyboard return button to call this method */ -(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{ return NO; }
-
The following properties are set text box the next time the edit starts, the text box contents are automatically emptied, and the next time the edit starts, it is automatically called textFieldShouldClear:方法 to empty the text box:
-
自定义键盘, set the properties of the text box, and enter the view for the inputVeiw custom text box.
5. Display the time in a format string as a step:
- <1> get the time to display;
- <2> Create time format management objects;
- <3> set time Format strings for time format objects;
-
The <4> time format object calls it stringFromDate:方法 , passes in the time to be displayed, and returns the format time string.
-
Sample program:
// 1. 创建datePickerUIDatePicker *datePicker = [[UIDatePicker alloc] init];datePicker.datePickerMode = UIDatePickerModeDate;datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];// 2. 获取datePicker时间NSDate *date = datePicker.date;// 3. 时间格式管理对象NSDateFormatter *formatter = [[NSDateFormatter alloc] init];// 4. 设置时间格式字符串formatter.dateFormat = @"yyyy-MM-dd";// 5. 获取格式时间字符串NSString *dateStr = [formatter stringFromDate:date];
Small Knowledge Points:
-
<1> Storyboard Direct Drag line set controller for the control of the agent and data source, the controller can not specify the corresponding protocol; Because the controller is automatically set to comply with the protocol when connected, keeping the code readable will usually still be declared in the Controller code to comply with the corresponding protocol.
-
Images in JPG, PNG, and GIF formats are available directly in <2> Xcode.
-
<3> ID differs from instancetype:
- Instancetype: As the return value type of a method, it is possible to automatically identify whether the current receive type is consistent with the method substance return type and cannot declare variables;
- ID: When returning a value type as a method, the receive type of the method return object can be any type, and the variable can be declared, an object of type ID can call any method, compile without error, and the runtime will crash if a method that is not implemented is called, and the ID type variable cannot use point syntax.
-
The <4> string calls the NSString class capitalizedString方法 , returning the string after the first letter of the string is capitalized.
-
<5> all controls that inherit from the Uicontrol class can take a addTarget:action:forControlEvents:方法 listen control.
Practical tips:
-
The <1> NSLog method uses %s re __func__ -printing: The output invokes the class where the current method resides;
-
<2> Debug in Debug displays all layers (layer) in the current interface:
-
<3> Xcode shortcut keys:
- 1, COMMAND + OPTION + ENTER: Split screen;
- 2, COMMAND + ENTER: Exit split screen.
- 3. Command + OPTION + [: Code increase
- 4. Command + OPTION +]: code downgrade
- 5. Command + [: Shift the code to the left
- 6. Command +]: Move code right
-
<4> when you 无法确认控件监听触发事件 type, you can wire the controls in storyboard to your code for easy viewing of event types.
iOS Core note--uipickerview