2011-03-04 240 views
0

我有一個根視圖控制器,它有幾個按鈕。點擊這些按鈕後,我顯示出一個標籤欄控制器,每個控制器顯示錶格視圖控制器。我已經實現了這一點。所以基本上,對於每個表視圖控制器我有一個專門的類。繼續,我替換了標籤欄控制器,並將分段控制器添加到導航標題視圖。導航控制器+段控制器:無法更改視圖

問題是如何根據選定的索引設置視圖。我可以將導航標題設置爲分段控制,但在選擇時我無法設置視圖。

以下是我迄今爲止所取得的成就。 注意:重要的是運行代碼,稍後我會做代碼優化。我不想隱藏意見。 我想調用不同的視圖控制器類。

RootViewController的類(按鈕的點擊,我所說的第一個視圖控制器,這樣我就可以設置區段控制器:

-(IBAction) pushSegmentController: (id) sender 
{ 

    NSLog(@"My Location Button being clicked/touched") ; 

    FirstViewController *firstViewController = [[FirstViewController alloc] init] ; 
    [self.navigationController pushViewController:firstViewController animated:YES]; 

    // Releae the view controllers 
    [firstViewController release]; 

} 

IN FirstViewController類:

-(void)viewDidLoad { 

    [super viewDidLoad]; 

    NSArray *viewControllers = [self segmentViewControllers]; 


    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Table1", @Table2"]]; 

    self.navigationItem.titleView = segmentedControl ; 

    [self.segmentedControl addTarget:self 
           action:@selector(indexDidChangeForSegmentedControl:) 
        forControlEvents:UIControlEventValueChanged]; 

    self.segmentedControl.selectedSegmentIndex = 0; } 

-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)aSegmentedControl { 

    NSUInteger index = aSegmentedControl.selectedSegmentIndex; 

    if(index ==0) { 
    UIViewController *table1Controller = [[AustraliaViewController alloc] initWithStyle:UITableViewStylePlain]; 
     **???? HOW SHOULD I SET THE VIEW OVER HERE... AS ITS A PART OF THE NAVIGATION CONTROLLER** 
    } 
    else { } 
} 

注:我試過使用這個選項:

[navigationController setViewControllers:theViewControllers animated:NO]; 

但是這個選項並不能給我正確的結果。我應該如何繼續,因爲我想調用一個視圖控制器類,並根據選定的索引設置其視圖。

在此先感謝。

+0

最後一行「但是這個選項犯規給我正確的結果」你能解釋一下實際問題?我看到你在使用Red Artisan的教程。你確定你在試圖將這種技術應用於你自己的代碼之前理解它的工作原理嗎? – pt2ph8 2011-03-04 22:44:33

回答

1

您可能不要想要有一個視圖控制器具有不同的視圖取決於按鈕索引,特別是因爲你已經有不同的視圖控制器爲您的不同屏幕。

如果你想表視圖控制器推到你的導航控制器,所以它有一個返回按鈕,讓你回到FirstViewController,使用

-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)aSegmentedControl { 
    NSUInteger index = aSegmentedControl.selectedSegmentIndex; 

    UIViewController *newViewController = nil; 
    if(index ==0) { 
     newViewController = [[AustraliaViewController alloc] initWithStyle:UITableViewStylePlain]; 
    } else { 
     newViewController = [[YourOtherViewController alloc] initWithStyle:UITableViewStylePlain]; 
    } 
    [self.navigationController pushViewController:newViewController animated:YES]; 
} 

如果你寧願它滑入從底部和要處理設置了所有必要的用戶界面(如解僱按鈕),更換

[self presentModalViewController:newViewController animated:YES]; 
-1

那麼呢?

self.view = table1Controller; 

[self.view addSubview:table1Controller]; 

我剛纔看見你做了一個更錯誤。你正在分配一個UIViewController,但是像tableViewController(initWithStyle)一樣初始化它。

如果它是UITableViewController的子類,則使用它進行分配,而不是UIViewController。

+0

您無法將視圖控制器分配給視圖控制器的視圖成員。如果你不需要任何更專門的子類方法,那麼可以將UIViewController的子類分配給變量類型的UIViewController。 – Anomie 2011-03-05 01:37:46

相關問題