0

我正在创建一个工具栏应用程序,在该应用程序中,所有工具栏图标和操作都已成功完成。但是当我使用该图标进行下一个视图调用并从该视图中调用一个后退按钮时,它会崩溃..这里我正在添加我的代码

/////在视图中加载

    objectLabel = [[UILabel alloc]init];
   objectLabel.frame = CGRectMake(10, 10, 300, 40);
   objectLabel.text = @"Press Button";
   [self.view addSubview:objectLabel];
   NSLog(@"label");
   objectToolbar=[UIToolbar new];
    objectToolbar.barStyle = UIBarStyleBlackTranslucent;
   [objectToolbar sizeToFit];
   objectToolbar.frame = CGRectMake(0, 410, 320, 50);
   NSLog(@"toolbar");

   UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                             target:self
                                                                             action:@selector(pressButton1:)];

   UIBarButtonItem *systemItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                                                             target:self
                                                                             action:@selector(pressButton2:)];

   UIBarButtonItem *systemItem3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
                                                                             target:self
                                                                             action:@selector(pressButton3:)];
   UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                          target:nil
                                                                          action:nil];
   NSArray *items = [NSArray arrayWithObjects: systemItem1, flexItem, systemItem2, flexItem, systemItem3, nil];
   [objectToolbar setItems:items animated:NO];
   [self.view addSubview:objectToolbar];

///之后

 -(void) pressButton1:(id)sender{
   // objectLabel.text = @"Add";
       FirstViewController *objectFirstViewController=[[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
       [self.view addSubview:objectFirstViewController.view];

   }

 -(void) pressButton2:(id)sender{
objectLabel.text = @"Take Action";
 }  

-(void) pressButton3:(id)sender{
objectLabel.text = @"Camera";
}

///在FirstViewController.m中

   -(IBAction)back:(id)sender
    {
      ViewController *objectViewController=[[ViewController  alloc]initWithNibName:@"ViewController" bundle:nil];
      [self.view addSubview:objectViewController.view];
   }

当我单击后退按钮时,它会崩溃..请给我解决方案

4

4 回答 4

2

为了返回,您不应该创建上一个类的新对象并再次添加它。相反,您需要从超级视图中删除当前子视图才能返回。

-(IBAction)back:(id)sender
{

  [self.view removeFromSuperview];
}

最好为此使用导航或模型转换,而不是仅仅使用addSubView.

于 2012-12-04T07:25:21.803 回答
2

只需从当前视图的超级视图中删除,例如..

   [self.view removeFromSuperview]; 

 // or  if you want to call the method from your MainViewController use this line [objectFirstViewController removeFromSuperview];
于 2012-12-04T07:27:39.337 回答
0

由于您正在这样做[self.view addSubview:Viewcontroller.view];,因此从逻辑上讲,您应该[self.view removeFromSuperview];在添加的子视图中进行操作。尽管如此,还是建议您使用导航控制器并执行推送/弹出操作来显示/删除视图。如果您的问题是导航栏,您可以通过隐藏它self.navigationController.navigationBarHidden = YES;并使用您的工具栏作为替代品。

于 2012-12-04T07:37:16.403 回答
0

而不是这个;

[self.view addSubview:objectFirstViewController.view];

采用

[self.navigationController pushViewController:objectFirstViewController 动画:YES];

并在 FirstviewController 集的 Viewdidload 处,

self.navigationController.navigationBar.hidden=YES;

并使用它

-(IBAction)back:(id)sender
{

        [self.navigationController popViewControllerAnimated:YES];
}

就是这样。

于 2012-12-04T08:36:14.503 回答