IOS programming Delegation and Text Input, iosdelegation

Source: Internet
Author: User

IOS programming Delegation and Text Input, iosdelegation

IOS programming Delegation and Text Input

1.1 Text Fields

 

CGRect textFieldRect = CGRectMake (40, 70,240, 30 );
UITextField * textField = [[UITextField alloc] initWithFrame: textFieldRect];

// Setting the border style on the text field will allow us to see it more easily textField. borderStyle = UITextBorderStyleRoundedRect;
[BackgroundView addSubview: textField];

 

Tap on the text field, and the keyboard will slide up from the bottom of the screen, allowing you to input text. to understand how this is happening under the hood, you need to understand the first responder.

When you click text field, the keyboard will slide from the bottom of the screen. To understand what happened at the underlying layer, you must understand first responder.

1.2 UIResponder

UIResponder is an abstract class in the UIKit framework. It is the superclass of three classes that you have already encountered:

UIView

UIViewController

UIApplication

UIResponder is an abstract class of the UIKit framework. He is the superclass of the three classes you have seen below.

UIView

UIViewController

UIApplication

UIResponder defines methods for handling (or "responding to") events: touch events, motion events (like a shake), and remote control events (like pausing or playing ).

UIResponder defines how to process events, touch events, motion events, and remote control events.

Subclasses override these methods to customize how they respond to events.

Subclasses reload these methods to adapt to how they respond to events.

With touch events, it is obvious which view the user has touched. Touch events are sent directly to that view.

The Touch event is clearly the event that the user touches. Send the touch event to that view.

The UIWindow has a pointer called firstResponder which indicates who shoshould respond to the other types of events.

UIWindow has a pointer named firstResponse. It points to who should respond to other types of events.

When you select a text field, for example, the window moves its firstResponder pointer to that text field.

If you select text field, window points fristResponse to text field.

When a text field or a text view becomes firstResponder, it shows its keyboard. When it loses first responder status, it hides its keyboard.

When a text field or text view becomes firstResponse, it displays the keyboard. When it is not firstResponse, it hides the keyboard.

If you want one of these views to become first responder, you send it the message becomeFirstResponder and the keyboard appears. When you want to hide the keyboard, you send it the message resignFirstResponder.

If you want to make one of these views a first responder, you should send the becomeFirstResponder message, and the keyboard will appear. When you want to hide the keyboard, you can send a resignFirstResponder message to it.

Most views refuse to become first responder;

Many views do not want to be first responder.

1.3 encryption ing the keyword

 

TextField. placeholder = @ "Hypnotize me ";
TextField. returnKeyType = UIReturnKeyDone;

The return key now says Done instead of the default Return.

 

In fact, the return key does not do anything automatically; you have to implement the return key functionality yourself.

Return key does not automatically do something. You must implement the return key function.

 

Some of the other useful properties that you can use to configure the keyboard.

The following are some attributes related to keyboard configuration.

AutocapitalizationType

This determines how capitalization is handled. The options are none, words, sentences, or all characters.

This determines how to handle uppercase letters. The options are none, words, sentences, and all characters.

AutocorrectionType

This will suggest and correct unknown words. This value can be YES or NO.

This will suggest and change unknown words. This value can be set to YES or NO.

EnablesReturnKeyAutomatically

This value can be YES or NO. If set to yes, the return key will be disabled if no text has been typed. As soon as any text is entered, the return key becomes enabled.


KeyboardType

This determines the type of keyboard that will be displayed. Some examples are the ASCII keyboard, email address keyboard, number pad, and the URL keyboard.

Set the keyboard to ASCII, email, numeric, or URL.
SecureTextEntry

Setting this to YES makes the text field behave like a password field, hiding the text that is entered.

Set as Password

1.4 Delegation

This is one form of callbacks that is used by UIKit: When a button is tapped, it sends its action message to its target. This typically triggers code that you have written.

When a button is pressed, it sends an action message to the target. This triggers the purchase agent you have written.

 

You introduce the text field to one of your objects: "This is your delegate, when anything interesting happens in your life, send a message to him ."

You introduce text field into one of your objects: This is your delegate. When anything you are interested in occurs in your lifecycle, send a message to her.

The text field keeps a pointer to its delegate.

Text field has a pointer to its delegate.

Messages of the message it sends to its delegates are Ive ive: "OK, I am done editing! ".

Many delegate messages sent to him are: Well, I have completed editing.

-(Void) textFieldDidEndEditing :( UITextField *) textField;
-(Void) textFieldDidBeginEditing :( UITextField *) textField;

Notice that it always sends itself as the first argument to the delegate method.

Note: It always passes itself as the first parameter to its delegate method.

 

Some of the message it sends to its delegate are queries: "I am about to end editing and hide the keyboard. OK? "

Some delegate messages sent to it about Inquiry: I'm about to end editing and hiding the keyboard. Yes?

 

-(BOOL) textFieldShouldEndEditing :( UITextField *) textField;
-(BOOL) textFieldShouldBeginEditing :( UITextField *) textField;-(BOOL) textFieldShouldClear :( UITextField *) textField;
-(BOOL) textFieldShouldReturn :( UITextField *) textField;

 

 

TextField. delegate = self;

 

-(BOOL) textFieldShouldReturn :( UITextField *) textField

{
NSLog (@ "% @", textField. text );

Return YES ;}

1.5 protocol

Before sending an optional message, the object first asks its delegate if it is okay to send that message by sending another message, respondsToSelector :.

When an optional message is sent, the object will use responseToSelector to query its delegate to see if the object can send delegate to it.

 

-(Void) clearButtonTapped

{
// TextFieldShouldClear: is an optional method,
// So we check first
SEL clearSelector = @ selector (textFieldShouldClear :);

If ([self. delegate respondsToSelector: clearSelector]) {if ([self. delegate textFieldShouldClear: self]) {

Self. text = @"";}

}}

If a method in a protocol is required, then the message will be sent without checking first. this means that if the delegate does not implement that method, an unrecognized selector exception will be thrown, and the application will crash.

If a method is required in protocol, the message will be sent without checking. This means that if you do not implement this method, a selector exception that is not detected will be thrown.

This is done either in the class header file or the class extension: the protocols that a class conforms to are added to a commadelimited list inside angled brackets in the interface declaration.

 

@ Interface BNRHypnosisViewController () <UITextFieldDelegate>

@ End

The reason for adding it to the class extension rather than the header file is the same reason as always: add to the class extension if the information (conforming to a participating protocol in this case) does not need to be publicly visible, and add it to the header file if other objects do need to know about the information.

You can add protocol to class extension or interface.

When classes have a delegate pointer, and it is nearly always a weak reference to prevent strong reference cycles.

Many classes have a delegate pointer, and all of them are weak references with a set of strong reference loops.

1.6 Adding the Labels to the Screen

To make things a little interesting, you are going to add instances of UILabel to the screen at random positions.

You will add UILabel at random locations on the screen.

 

Arc4random () % 4 will generate 0, 1, 2 or 3.

Arc4random () % 5 will generate 0, 1, 2, 3, 4

Update the textFieldShouldReturn: method to call this new method, passing in the text field's text, clear the text that the user typed, and then dismiss the keyboard by calling resignFirstResponder.

-(BOOL) textFieldShouldReturn :( UITextField *) textField

{
NSLog (@ "% @", textField. text );
[Self drawHypnoticMessage: textField. text];

TextField. text = @"";
[TextField resignFirstResponder];

Return YES ;}

1.7 Motion Effects

Accelerometer, magnetometer, and gyroscope-help determine the orientation of the device.

Accelerators, FPGAs, and gyroscope help us determine the direction of the device.

When you drive down the road, the signs along the shoulder appear to move much more quickly than trees in the distance.

When you are driving on the road, the pictures on the road are obviously moving faster than the road signs in the distance.

Your brain interprets this difference in apparent speed as movement in space. This visual effect is called "parallax ".

Resolve this difference when your brain moves at an obvious speed. This virtual effect becomes parallax (parallax)

With iOS 7, you have probably noticed this on the home screen where the icons appear to move relative to the wallpaper when you tilt the device. it is used subtly (and not so subtlety) in varous places when SS the operating system and bundled apps, including the red badges on Home screen icons, the volume changer pop-up, and alert views.

You notice that when you tilt the screen at home, the icon moves relatively.

Applications can access the same technology that powers those effects by using the UIInterpolatingMotionEffect class.

Applications can also obtain the same technology by using UIInterpolatingMotionEffect.

Instances are given an axis (either horizontal or vertical), a key path (which property of the view do you want to impact ), and a relative minimum and maximum value (how much the key path is allowed to sway in either direction ).

The instance variable will provide a coordinate axis (horizontal or vertical), a key path (the attribute you want to affect the view), and a relatively minimum maximum value (whichever is up, maximum and minimum shaking intervals)

UIInterpolatingMotionEffect * motionEffect;

MotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath: @ "center. x"

Type: UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis]; motionEffect. minimumRelativeValue = @ (-25 );

MotionEffect. maximumRelativeValue = @ (25); [messageLabel addMotionEffect: motionEffect];

MotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath: @ "center. y"

Type: UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis]; motionEffect. minimumRelativeValue = @ (-25 );

MotionEffect. maximumRelativeValue = @ (25 );

[MessageLabel addMotionEffect: motionEffect];

1.8 Using breakpoints

This navigator shows a stack trace of where the breakpoint stopped execution

This navigation shows the stack trace that breakpoint stops running.

A stack trace shows you the methods and functions whose frames were in the stack when the application broke.

A stack trace displays some methods and functions. These functions and methods indicate when the application stops in the stack.

The method where the break occurred is at the top of the stack trace.

Where the stop method is located at the top of the stack.

Select the method at the top of the stack. in the debug area below the editor area, check out the variables view to the left of the console. this area shows the variables within the scope of BNRHypnosisView's initWithFrame: method along with their current values

Select the top method of the stack and check the variables on the left in the debug area. This area shows the existing values of the variable in initWithFrame.

 

1.8 main () and UIApplication

Open main. m in the HypnoNerd project navigator. It looks like this:

Int main (int argc, char * argv [])

{
@ Autoreleasepool {

Return UIApplicationMain (argc, argv,
Nil, NSStringFromClass ([BNRAppDelegate class]);

}}

 

The function UIApplicationMain creates an instance of a class called UIApplication.

The UIApplicationMain function creates an instance that becomes a UIApplication.

For every application, there is a single UIApplication instance.

Each program has only one UIApplication.

This object is responsible for maintaining the run loop. Once the application object is created, its run loop essentially becomes an infinite loop: the executing thread will never return to main ().

Once the UIApplication is created, its run loop will become an infinite loop: The execution thread will not return to the main.

Another thing the function UIApplicationMain does is create an instance of the class that will serve as the UIApplication's delegate.

UIApplication also creates an instance of the class entrusted by the UIApplication.

Notice that the final argument to the UIApplicationMain function is an NSString that is the name of the delegate's class. so, this function will create an instance of BNRAppDelegate and set it as the delegate of the UIApplication object.

Note that the final parameter of UIApplicMain is a string. The name of the delegate class.

The first event added to the run loop in every application is a special "kick-off" event that triggers the application to send a message to its delegate. This message is application: didfinishlaunchingwitexceptions :.

The first event added to the run loop is a special kick-off event. It will trigger the application to send a message to its proxy. The message is application: didfinishlaunchingwitexceptions.

You implemented this method in BNRAppDelegate. m to create the window and the controller objects used in this application.

You can implement this method in BNRAppDelegate. m to create a window and the controller object used in the application.

Every iOS application follows this pattern.

This mode is applied to every iOS application.

 

 

 

 

 

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.