Simple writing of Login interface (via Nsnotificationcenter)

Source: Internet
Author: User
Tags notification center

Before you introduce the content, take a look at the implementation results.

Through the details can be found that only when the phone number and password are entered in the case of the login button will be lightened. So how does this come true?

First of all, we need to know that the first condition of this situation is to know the text of two textfield every moment. So what can be achieved, there is a very old stupid method. That is to check the text content of TextField every second through Nstimer. Obviously this method consumes memory very much, it is impossible to adopt in actual development, but it is not necessarily a way for the novice to develop the brain. Gossip less said, the following describes today's protagonist Nsnotificationcenter.

Nsnotificationcenter is a listening mechanism.

With uitextfieldtextdidchangenotification, you can monitor all the time.

Since the purpose of this article is to introduce ideas, I will not introduce the basic knowledge points. Children's shoes that you want to learn can be viewed on other blogs, and if necessary, you can comment on this article, and I will explain the points of knowledge to you when I see the comments.

The code is as follows (just the key, the controls can be self-acting):

////VIEWCONTROLLER.M//Cx-login////Created by Ma C on 16/1/22.//Copyright (c) 2016 Xubaoaichiyu. All rights reserved.//#import "ViewController.h"#import "CustomerTextField.h"#import "CustomerButton.h"@interfaceViewcontroller ()//provides global variables TextField@property (nonatomic, Strong) uitextfield*TextFiledTemp1, @property (nonatomic, strong) Uitextfield*textFiledTemp2, @property (nonatomic, strong) UIButton*btn;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //View Background ColorSelf.view.backgroundColor = [Uicolor colorwithred:247/255.0fGreen248/255.0fBlue243/255.0fAlpha1]; //Set textfiledcustomertextfield* textField1 = [[Customertextfield alloc]initwithframe:cgrectmake ( +, -, screen_width- the, +) ImageName:@"UserName"Placeholder@"Please enter your phone number"];    [Self.view Addsubview:textfield1]; SELF.TEXTFILEDTEMP1=TextField1; //textfield1.keyboardtype = Uikeyboardtypenumberpad;//Keyboard Typecustomertextfield* textField2 = [[Customertextfield alloc]initwithframe:cgrectmake ( +, -, screen_width- the, +) ImageName:@"Password"Placeholder@"Please enter your password"]; Textfield2.securetextentry=YES;    [Self.view Addsubview:textfield2]; SELF.TEXTFILEDTEMP2=textField2; //Set Login buttonSELF.BTN = [[Customerbutton alloc]initwithframe:cgrectmake ( +, About, screen_width- the, +) Backgroundimagename:@"Beijing"Title@"Login"]; Self.btn.enabled=NO;    [Self.view ADDSUBVIEW:SELF.BTN];    [Self notificationcenter]; }-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{[Self.view endediting:yes];}//Notification Center monitoring TextField-(void) notificationcenter{[[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector ( Textdidchangenotification:) name:uitextfieldtextdidchangenotificationObject: nil]; }//Implement button state transitions-(void) Textdidchangenotification: (nsnotification*) notifi{if([Self.textFiledTemp1.text isequaltostring:@""]|| [Self.textFiledTemp2.text isequaltostring:@""]) {self.btn.enabled=NO; }    Else{self.btn.enabled=YES; }}@end

Simple writing of the login interface (via Nsnotificationcenter)

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.