2011-03-04 127 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

相关问题