iOS design mode-kvo Viewer mode

Source: Internet
Author: User

Alas, the new year has passed. I wish you a happy new year.

Because in the previous project using the Observer mode to solve some problems and the effect is better, such as the keyboard (this is used in the UIWindow of the notification type), there is a project in my years ago to write something specific, need to continue to locate the app background to the server, And the field returned by the server to determine whether to end the positioning, I said the returned field passed to delegate a property to record whether the current need to locate the state, I chose KVO to detect a property in Appdelegate, once the property value of the property becomes 1, stop positioning, This may be more troublesome but personally think the idea is clearer, haha. The code is at the bottom!

////VIEWCONTROLLER.M//Strategydemo////Created by Sansing on 16/2/8.//copyright©2016 year Sansing. All rights reserved.//#import "ViewController.h"#import "StrategyClass.h"@interfaceViewcontroller () {Uitextfield*text;}@end@implementationViewcontroller- (void) Viewwillappear: (BOOL) animated{[[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector ( Postbeginmynotification:) name:uikeyboardwillshownotificationObject: nil]; [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (postbeginmynotification:) Name: UikeyboardwillhidenotificationObject: nil];}- (void) Viewwilldisappear: (BOOL) animated{[[Nsnotificationcenter defaultcenter]removeobserver:self Name: UikeyboardwillshownotificationObject: nil]; [[Nsnotificationcenter defaultcenter]removeobserver:self name:uikeyboardwillhidenotificationObject: nil];}- (void) viewdidload {[Super viewdidload]; Text= [[Uitextfield alloc]initwithframe:cgrectmake (0, [UIScreen mainscreen].bounds.size.height- -, [UIScreen Mainscreen].bounds.size.width, - )];    [Text Setbackgroundcolor:[uicolor Redcolor]; [Self.view Addsubview:text];}- (void) Postbeginmynotification: (nsnotification*) notification{Nsvalue* Endrect = [Notification.userinfo valueforkey:@"Uikeyboardframeenduserinfokey"]; NSNumber* animationduaring = [Notification.userinfo valueforkey:@"Uikeyboardanimationdurationuserinfokey"]; //Animation DurationCGFloat Animationduaringvalue =[animationduaring Floatvalue]; //Keyboard frame at the end of an animationCGRect Endrext =[Endrect Cgrectvalue]; [UIView animatewithduration:animationduaringvalue animations:^{self.view.frame= CGRectMake (0, endrext.origin.y-[UIScreen mainscreen].bounds.size.height, Self.view.frame.size.width, Self.view.frame.size.height]; }];}@end
The core code is as follows:
- (void) viewdidload {[Super viewdidload]; Myannotation=[[Bmkpointannotation alloc]init]; if(!Locationservice) {Locationservice=[[Bmklocationservice alloc]init]; Locationservice.Delegate=Self ; } [self startgetselflocation]; [__appdelegate addobserver:self Forkeypath:@"Isshare"Options:nskeyvalueobservingoptionnew Context:nil]; [Self addmapview]; [Self addsharebtn];}//KVO return detection of changes in Isshare in delegate- (void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID)ObjectChange: (nsdictionary *) Change context: (void*) Context {sharebtn.selected= ([[Change Valueforkey:@"New"] Isequaltostring:@"1"])?Yes:no;}-(void) dealloc{//Remove Kvo[__appdelegate removeobserver:self Forkeypath:@"Isshare"];}

iOS design mode-kvo Viewer mode

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.