zoukankan      html  css  js  c++  java
  • (ios) nsnotification总结

    1  文本输入,键盘显示时,view向上,键盘隐藏时,view向下

    1.1 注册键盘显示,关闭通知,并实现主界面上下变动

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    
    
    -(void)keyboardWillShow:(NSNotification *)aNotification
    {
    
        CGRect keyBoardRect=[[[aNotification userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue];
        
        NSTimeInterval animalInterval=[[[aNotification userInfo]objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        CGRect frame=self.view.frame;
        frame.origin.y=-keyBoardRect.size.height;
        [UIView beginAnimations:@"keyboardshow" context:nil];
        [UIView setAnimationDuration:animalInterval];
        self.view.frame=frame;
        [UIView commitAnimations];
    
    
    }
    
    -(void)keyboardWillHide:(NSNotification *)aNotification
    {
    
        NSTimeInterval animalInterval=[[[aNotification userInfo]objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        CGRect frame=self.view.frame;
        frame.origin.y=0;
        [UIView beginAnimations:@"keyboardhide" context:nil];
        [UIView setAnimationDuration:animalInterval];
        self.view.frame=frame;
        [UIView commitAnimations];
        
    }

    1.2 文本框初始化,并实现UITextViewDelegate委托

        self.textbox.returnKeyType=UIReturnKeyDone;
        self.textbox.delegate=self;
    }
    
     - (BOOL)textFieldShouldReturn:(UITextView *)textView
    {
     [textView resignFirstResponder];
        return YES;
    }

    2 自定义notification

     2.1 定义侦听自定义notification观察者

    //注册观察者,侦听自定义通知
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(selfNotificationDO:) name:@"CustomNotification" object:nil];
    
    }
    
    -(void)selfNotificationDO:(NSNotification *)aNotification
    {
         //处理notification
        //........
    }

    2.2 生成一个自定义notification

    //生成一个自定义Notification
        [[NSNotificationCenter defaultCenter] postNotificationName:@"CustomNotification" object:self];
  • 相关阅读:
    redis分布式锁解决超卖问题
    redis使用
    Xcode 解决日志打印不全问题
    苹果电脑系统怎么重装?这几步就可以轻松搞定
    Mac 一键显示所有隐藏文件 不要那么六好吧
    iOS导入高德地图出现缺失armv7--"Undefined symbols for architecture armv7"
    如何生成.a文件,小心有坑!!
    保护你的代码,生成.a文件以及.framework文件需要注意的地方
    二维码扫描工具实现
    iOS 调整图片尺寸,告诉你的UI,别问我尺寸!我要最大的
  • 原文地址:https://www.cnblogs.com/macroxu-1982/p/3526632.html
Copyright © 2011-2022 走看看