視圖控制器

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#>]




  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章