视图控制器

来源:互联网 发布:php获取p标签中的img 编辑:程序博客网 时间:2024/04/28 07:36
UIViewController *viewCtrl=[[UIViewController alloc]init];
self.window.rootViewController=viewCtrl;


1.//创建视图 self.view loadView  self.view=nil会被调用
-(void)loadView
{
    [super loadView];
}


- (void)viewDidLoad     //self.view!=nil会被调用
首先是调用loadView 然后调用viewDidLoad


2.两个ViewController之间的传值问题  单例  KVO  通知 代理


(1)、单例的话 还是要借助三方类  三方类是单例   防止被多次创建
static Content *content=nil;  全局变量不会被销毁  有值的话就不会赋值
+(Content *)shareContent
{
//static Content *content=nil;
if(content==nil)
{
content=[[self alloc]init];
}
return content;
}


+(id)alloc
{
if(content==nil)
{
content=[self alloc];
}
return content;
}


(2)、通知  发送时有个userInfo 实质还是有三方类的帮助  两个类之间没有任何关联
NSDictionary *dic=@{@"text":textFiled.text};
- (void)notificationAction:(NSNotification *)notification
{
    NSString *text = [notification.userInfo objectForKey:@"text"];
    _textLabel.text = text;
}
(3)、KVO 的话  还是利用全局变量的性质   -----给被观察者设置为全局变量 ---属性的改变必须通过set、kvc方法




_contentV=[[ContentView alloc]init];
        [_contentV addObserver:self forKeyPath:@"textFiledContent.text" options:NSKeyValueObservingOptionNew context:nil];
会发生的问题  监听到得全局类 是空的   被观察者的属性是否存在 
把文本属性  初始化时就创建
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    _logLabel.text=[change objectForKey:@"new"];
}




(4)、代理方法  协议其实就是避免双方拿错
先拿到 转到对方视图前,就拿到对方的text 设置其相关属性


(5)、创建真正意义的三方类 




视图切换时  以前的视图并没有被释放掉、或销毁 只是看不到


在其上面赋值、取值  不能直接先赋值给label,因为其还没创建


contentView.loginView=self;
_loginView.logLabel.text=_textFiledContent.text;


代理 替换掉loginView  并且有方法
代理实现代理协议  别人找代理,帮其实现功能


视图创建
 [self presentViewController:loginView animated:YES completion:nil];
视图销毁
[self dismissViewControllerAnimated:<#(BOOL)#> completion:<#^(void)completion#>]




  



0 0
原创粉丝点击