2013-10-01 26 views
1

我正在開發一個具有TableView的應用程序。當我按任何單元格時,應用程序會轉到下一個ViewController。在這個viewController我創建了一個TabBarController的代碼有3個子ViewControllers。所以,我想將一個變量從TableView傳遞給TabBar的子項。我可以將變量傳遞給TabBar,我已經用NSlog函數來觀察它。對我來說,真的很奇怪,在子ViewControllers中,我也輸入了一個NSlog並且該變量爲null,但是在輸出中我首先看到了這一點。在ViewController和TabBar子項之間傳遞數據

2013-10-01 03:01:40.687 Prototype[38131:c07] proId (null) // This is the children log from vc2 ViewController "YPProjectViewController" 
2013-10-01 03:01:40.697 Prototype[38131:c07] projectID 433 // This is the TabBar LOG YPTabBarViewController 

有人知道爲什麼我可以首先孩子NSLog?也許有解決方案。

#import "YPTabBarViewController.h" 
#import "YPProjectViewController.h" 
#import "YPCommentsViewController.h" 
#import "YPProposalsViewController.h" 

@interface YPTabBarViewController() 
@property (nonatomic,strong)UITabBarController *tabBar; 
@end 

@implementation YPTabBarViewController 
@synthesize tabBar; 
@synthesize projectId = _projectId; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self setUpTabBar]; 

} 

// Set up tabBar 
-(void)setUpTabBar 
{ 

     YPCommentsViewController *vc1 = [[YPCommentsViewController alloc] init]; 
     vc1.title = @"Comments"; 
     vc1.view.backgroundColor = [UIColor clearColor]; 
     UINavigationController *contentNavigationController = [[UINavigationController alloc] initWithRootViewController:vc1]; 

     YPProjectViewController *vc2 = [[YPProjectViewController alloc] init]; 
     vc2.title = @"Project"; 
     vc2.view.backgroundColor = [UIColor clearColor]; 

     vc2.proId = _projectId; 
     NSLog(@"PROJECT ID %@", vc2.proId); 
     // UINavigationController *contentNavigationController2 = [[UINavigationController alloc] initWithRootViewController:vc2]; 


     YPProposalsViewController *vc3 = [[YPProposalsViewController alloc] init]; 
     vc3.title = @"Proposal"; 
     vc3.view.backgroundColor = [UIColor clearColor]; 
     UINavigationController *contentNavigationController3 = [[UINavigationController alloc] initWithRootViewController:vc3]; 
     tabBar = [[UITabBarController alloc] init]; 
     tabBar.viewControllers = @[contentNavigationController,vc2,contentNavigationController3]; 
     tabBar.selectedIndex = 1; 

     [tabBar.view setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; 
     [tabBar willMoveToParentViewController:self]; 
     [self addChildViewController:tabBar]; 
     [tabBar didMoveToParentViewController:self]; 
     [self.view addSubview:tabBar.view]; 

}

+0

對不起,我編輯了這個問題。這是真的你說什麼 – croigsalvador

+0

你的意思是? '@implementation YPProjectViewController - (id)init { self = [super init]; if(!self)return nil; NSLog(@「proId ID%@」,_proId); 迴歸自我; }' – croigsalvador

+0

但是,我沒有看到它在初始化,我已經做到了這一點來顯示你,我顯示proId登錄ViewDidLoad – croigsalvador

回答

2

在認識問題方面,你NSLog聲明中的「標籤欄控制器」設置後立即記錄的vc2.proID值。但是你的NSLog輸出告訴我們,第二個選項卡的視圖控制器在之前記錄了其結果。這就是爲什麼當第二個選項卡的視圖控制器的viewDidLoad記錄日誌時,它是nil,因爲該日誌在標籤欄控制器有機會設置值並記錄它自己之前發生。

因此,有幾個方法,你可以解決這個問題:

  1. 右您的vc2.proId轉讓前,你有代碼無害線,說:

    vc2.view.backgroundColor = [UIColor clearColor]; 
    

    這條線的代碼觸發第二個視圖控制器的視圖被加載(並且它的viewDidLoad將被調用)。如果您將vc2.proId的作業移動到您開始訪問vc2的任何視圖之前,這將會更改您的NSLog語句出現的順序(或者更好的是將子控制器的背景色設置爲viewDidLoad)。

  2. 您可以創建自己的init方法,該方法接受項目ID作爲參數。這也將確保它在viewDidLoad之前設置。因此,YPProjectViewController可以有一個方法,如:關於自定義容器調用

    - (id)initWithProjectId:(NSString *)projectId 
    { 
        self = [self init]; 
    
        if (self) 
        { 
         _proId = projectId; 
        } 
    
        return self; 
    } 
    

兩個不相關的意見:

  1. 當你調用addChildViewController,它調用willMoveToParentViewController你。所以你應該刪除電話willMoveToParentViewController。請參閱documentation for that method

  2. 你可能甚至想完全退休,這些定製容器調用,只是讓YPTabBarViewControllerUITabBarController,本身就是一個子類,而不是UIViewController。這消除了完全自定義容器調用的需要。很明顯,如果您對自定義容器有其他需求,那麼請隨意,但在此代碼示例中是多餘的。

+1

我要說的很像羅布在這裏。我想補充一點,在這裏設置的大多數控制器屬性,我認爲應由控制器自己處理。標題和背景色具體。當然,這些移除和遺囑並不屬於這裏。我會推薦Rob的自定義發起者的想法,並且這些視圖之間的任何其他通信應該通過委託/協議或NSNotification的方式進行處理。 –

+2

@DeanDavids同意。順便說一句,這不僅僅是一個好的風格問題,但也有性能/內存的原因,沒有這個標籤欄控制器不嘗試更新子控制器的「視圖」對象。如果標籤欄控制器訪問每個子視圖,它將預先加載所有這些視圖。沒有理由遭受這種(適度)打擊。如果將這些視圖更新推遲到相應子項的「viewDidLoad」,則在您絕對需要它們(例如,用戶在該選項卡上輕擊)之前,不會創建它們的視圖。因此,啓動速度更快,消耗的內存更少。 – Rob

相關問題