且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS中 通知中心Text (实例)

更新时间:2022-08-13 20:08:27

[objc] view plain copy
  1.   
指定根视图

[objc] view plain copy
  1. self.window.rootViewController = [RootViewController new];  

方法实现:

[objc] view plain copy
  1. #import "RootViewController.h"  
  2. #define kScreenHeight [UIScreen mainScreen].bounds.size.height  
  3. #define kScreenWidth [UIScreen mainScreen].bounds.size.width  
  4. @interface RootViewController ()  
  5. @property (nonatomicstrongUITextField *textField;  
  6. @end  
  7. @implementation RootViewController  
  8.   
  9. - (void)viewDidLoad  
  10. {  
  11.     [super viewDidLoad];  
  12.       
  13.     self.view.backgroundColor = [UIColor greenColor];  
  14.       
  15.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeFrame:) name:UIKeyboardWillShowNotification object:nil];  
  16.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hiddenFrame2:) name:UIKeyboardWillHideNotification object:nil];  
  17.       
  18.     self.textField = [[UITextField alloc] initWithFrame:CGRectMake(50, kScreenHeight - 100, kScreenWidth - 10035)];  
  19.     self.textField.borderStyle = UITextBorderStyleRoundedRect;  
  20.     [self.view addSubview:self.textField];  
  21. }  


[objc] view plain copy
  1. - (void)changeFrame:(NSNotification *)sender  
  2. {  
  3.     CGRect frame = self.textField.frame;  
  4.     frame.origin.y = 100;  
  5.     [UIView animateWithDuration:2 animations:^{  
  6.         self.textField.frame = frame;  
  7.     }];  
  8. }  
  9.   
  10. - (void)hiddenFrame2:(NSNotification *)sender  
  11. {  
  12.     [UIView animateWithDuration:2 animations:^{  
  13.         CGRect frame = self.textField.frame;  
  14.         frame.origin.y = kScreenHeight - 100;  
  15.   
  16.         self.textField.frame = frame;  
  17.     }];  
  18. }  


释放:

[objc] view plain copy
  1. - (void)dealloc  
  2. {  
  3.     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];  
  4.     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];  
  5. }  

最终效果:

iOS中 通知中心Text (实例)


有问题可以关注我微博私信我.http://weibo.com/hanjunqiang



原文地址:http://blog.csdn.net/qq_31810357/article/details/49611281