查看控制器隐藏问题

时间:2011-03-28 17:23:58

标签: iphone objective-c xcode

海, 我有一个基于窗口的tabbar应用程序。我创建了5个标签栏项目,其中一个标签栏项目是客户。点击客户后,我需要添加另一个标签栏和3个视图控制器,如客户列表,选择客户和发票清单。为此,我使用了分段控制器和3段控制器按钮。

我创建了一个IBaction:

-(IBAction) segmentedControlIndexChanged
{

  switch (self.segmentedControl.selectedSegmentIndex) 
{

        case 0:
    CustomerListviewController *customerListViewController=[[CustomerListviewController alloc]init];
    [self.view addsubView:customerListViewController.view];
    break;

        case 1:
    SelectCustomerviewController *selectCustomerviewController =[[ SelectCustomerviewController alloc]init];
    [self.view addsubView:*selectCustomerviewControllerr.view];

         break;

    case 2:
    InvoiceListViewController *invoiceListViewController=[[ InvoiceListViewController alloc]init];
    [self.view addsubView:invoiceListViewController.view];
     break;

        default:

    break;
   }
}

但是当我点击一个分段控制器时,视图会显示在后台的前一个视图中。 我怎么解决这个问题。如果我有替代想法而不是使用分段控制器,我同意使用。 Plz任何人帮我解决这个或替代解决方案。

1 个答案:

答案 0 :(得分:0)

现在,在添加新视图之前,您不会删除任何视图,请使用“removeFromSuperview”方法删除不再需要的子视图。如果你想在每个开关后想要完整的tabula rasa尝试这样的事情:

-(IBAction) segmentedControlIndexChanged
{

  // Remove all subviews of the main view of the view Controller
  for (UIView *view in [self.view subviews]) {
     [view removeFromSuperview];
  }

  switch (self.segmentedControl.selectedSegmentIndex) {

        case 0:
    CustomerListviewController *customerListViewController=[[CustomerListviewController alloc]init];
    [self.view addsubView:customerListViewController.view];
    break;

        case 1:
    SelectCustomerviewController *selectCustomerviewController =[[ SelectCustomerviewController alloc]init];
    [self.view addsubView:*selectCustomerviewControllerr.view];

         break;

    case 2:
    InvoiceListViewController *invoiceListViewController=[[ InvoiceListViewController alloc]init];
    [self.view addsubView:invoiceListViewController.view];
     break;

        default:

    break;
   }
}