This is the 31st day of my participation in the August Gwen Challenge. For details, see: August Gwen Challenge “juejin.cn/post/698796…”
The introduction
I believe that the old fans know that I have been more than a month in a row, the next review UIKit including delegate
The UIKit framework provides the infrastructure needed for iOS or Apple tvOS apps. It provides the window and view architecture for implementing the interface, the event processing infrastructure for providing multi-touch and other types of input to the app, and the main runtime loop needed to manage interactions between users, the system, and the App.
Unless otherwise specified, use UIKit classes only from the main thread or main scheduling queue of your app. This restriction applies especially to classes derived from UIResponder, or classes that involve manipulating the app’s user interface in any way.
I UIKit review
- UIButton->UIControl->UIView
- UILabel ->UIView
- UIImageView->UIView
- UITextField->UIControl->UIView
1.1 Setting control status
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIControl : UIView
@property(nonatomic.getter=isEnabled) BOOL enabled;// Enable and disable controls
@property(nonatomic.getter=isSelected) BOOL selected;// Select, unselect
@property(nonatomic.getter=isHighlighted) BOOL highlighted; // Highlight, not highlight
Copy the code
1.2 Setting the layout of the control content
@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;// Vertical center method
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;// Horizontal center direction
Copy the code
1.3 Adding a Listening method
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
- (void)removeTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
Copy the code
II Agent design pattern
Oc is the most widely used design pattern in OC
2.1 What is the role of agency?
-
Listen for events that cannot be listened on by addTarget
-
It’s responsible for passing data or messages between two objects when something happens
2.2 Steps of proxy implementation
-
Become a proxy for (child) controls; The father (view controller) becomes the proxy for the son (textField)
-
Optional steps: Follow the protocol, take advantage of the IDE’s hints, and write code quickly
-
Implementation protocol method
#import "ViewController.h"
@interface ViewController()"UITextFieldDelegate>
@end
@implementation ViewController
/ * * * /
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
[btn setCenter:self.view.center];
[self.view addSubview:btn];
// Register the listener click to the Run loop. When the ControlEvent event is triggered, the Run loop tells Target (ViewController) to execute the action (@selector).
//Adds a target and action for a particular event (or events) to an internal dispatch table. Adds execution goals and actions for a particular event to the internal scheduling table
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100.100.80.20)];
[textField setText:@"lydia"];
[textField setTextColor:[UIColor blackColor]];
[textField setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[textField setDelegate:self];
[self.view addSubview:textField];
}
#pragmaMark - Text box proxy
/** What will you do after you become an agent? And how it works: Some pre-defined implementations have no concrete method names, and each method corresponds to a different event. @protocol UITextFieldDelegate
*/
//Asks the delegate if the specified text should be changed.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSLog(@"replacementString =%@, textField = %@,NsRange = %@",string,textField.text,NSStringFromRange(range));
// Limit the length of input
long location = range.location;
return location<7;//YEStrue if the specified text range should be replaced; otherwise, NOfalse to keep the old text.
}
/ * * * /
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
// NSLog(@"%s",__FUNCTION__);
return YES;// if an editing session should be initiated
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
// NSLog(@"%s",__FUNCTION__);
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
// NSLog(@"%s",__FUNCTION__);
return YES;//if editing should stop
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"%s",__FUNCTION__);
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"%s",__FUNCTION__);
return YES;// If the text field's contents should be cleared;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"%s",__FUNCTION__);
return YES;//if the text field should implement its default behavior for the return button;
}
- (void)click:(UIButton *)button{
NSLog(@"%s",__FUNCTION__);
[button removeTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
Copy the code
III The difference between contentSize and contentInset
ContentSize adjusts offset according to ContentInset — other actions are implemented besides assignment
ContentInset does not adjust offset based on contentSize — it simply assigns values to properties
Implementation differences of setter methods (can be seen by setting breakpoints)
// ViewController.m
@interface ViewController(a)
@property (weak.nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak.nonatomic) IBOutlet UIButton *lastButton;
@end
@implementation ViewController
/ * * * /
// After loading the storyBoard, the system calls setScrollView: to assign a value to the scrollView object
- (void)setScrollView:(UIScrollView *)scrollView{// This method precedes viewDidLoad:; This method is automatically called by the underlying system
_scrollView = scrollView;//1, the first sentence of the setter method, is assignment
//2 other actions
[_scrollView setContentInset:UIEdgeInsetsMake(64.0.10.0)];
[_scrollView setContentSize:CGSizeMake(0.CGRectGetMaxY(self.lastButton.frame))];
}
/** Execute */ after the view is loaded
- (void)viewDidLoad {
[super viewDidLoad];
//1. Set spacing: Just specify the margins on the outside of content and do not automatically adjust contentOffset according to contentSize
// [self.scrollView setContentInset:UIEdgeInsetsMake(64, 0, 0, 0)];
//2. Set the scroll view content size
//1> Automatically adjusts contentOffset according to the spacing, if contentInset is present
//2> contentOffset is (0,0)
// [self.scrollView setContentSize:CGSizeMake(0, CGRectGetMaxY(self.lastButton.frame)+10)]; //CGRectGetMaxY(self.lastbutton.frame)+10) is used to show the lastButton more clearly
//3. Set the offset
// [self.scrollView setContentOffset:CGPointMake(0, -64)];
}
@end
Copy the code
IV timer playback implementation
Kunnan.blog.csdn.net/article/det…
4.1 Add timer to the run loop
One way to add a timer to a running loop
Creates and returns a new NSTimer object and schedules it on the current run loop in the default mode.
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
Copy the code
Parameters that
- Seconds: indicates the double interval
2. Target: The object to which to send The message specified by aSelector when The timer fires. 3. Selector: The message to send to target when The timer fires. The selector should have The following signature: timerFireMethod:Copy the code
- (void)timerFireMethod:(NSTimer *)timer
Copy the code
- UserInfo: The user info for The timer. It is usually nil and can be used to distinguish timers
Repeats: if YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires. Whether to repeat
Add the timer to the run loop in method two#import "ViewController.h"
@interface ViewController()"UIAlertViewDelegate>
@property (weak.nonatomic) IBOutlet UILabel *counterLabel;
@property (nonatomic.strong) NSTimer *timer;
@end
@implementation ViewController
#pragmaMark - timer playback implementation
- (IBAction)start{
NSLog(@"%s",__FUNCTION__);
// Update the display of counterLabel every second
/ / timer
/ / add the timer to the running cycle 2 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
/ / Registers a given the timer with a given input mode. NSRunLoopCommonModes (listening scroll mode)
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode: NSRunLoopCommonModes];
}
Copy the code
4.2 Clock Update Method
/** Clock update method */
- (void) updateTimer:(NSTimer *) timer{
//1> Retrieve the label number
int count = self.counterLabel.text.intValue;
/ / if counterLabel. Text. IntValue = 0, pop-up hints information, otherwise the countdown
if (0 > --count) {//count-- assign first and then decrement
[self pause];
[[[UIAlertView alloc]initWithTitle:@"start" message:@"In the beginning, laydia" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"done".@"kevin".nil] show];
return;
}
//2: Modifies the text of the counterLabel
self.counterLabel.text = [NSString stringWithFormat: @"%d",count];//--count is decrement and then assigned
}
Copy the code
4.3 Stopping the Clock
Once the invalidate: method is called, the timer is invalidated and needs to be re-instantiated to restart the clock
- (IBAction)pause{
NSLog(@"%s",__FUNCTION__);
// Stop the clock. Once the invalidate: method is called, the timer is invalidated. To restart the clock, you need to re-instantiate it
[self.timer invalidate];// The only way to stop the clock
}
- (IBAction)clean{
NSLog(@"%s",__FUNCTION__);
}
#pragmaMark-ui AlertView delegate protocol method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"%lu",buttonIndex);
}
- (void)alertViewCancel:(UIAlertView *)alertView{
NSLog(@"%s",__FUNCTION__); } - (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView{
NSLog(@"%s",__FUNCTION__);
return NO;
}
@end
Copy the code
see also
More content please pay attention to the public number: iOS reverse, nuggets because can’t automatically synchronize the public number article, so want to get the latest article for the first time, please pay attention to the public number: iOS reverse
Developer.apple.com/cn/document…