簡體   English   中英

Xcode中的工具欄和后退按鈕問題

[英]Toolbar & Back button issue in xcode

我正在創建一個工具欄應用程序,該應用程序中的所有工具欄圖標和操作均成功完成。但是當我使用該圖標進行下一個視圖調用時,從該視圖中調用后退按鈕,它將崩潰。在此處添加代碼

/////在View中加載了

    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];
   }

當我單擊后退按鈕時,它會崩潰..請給我解決方案

為了回溯,您不應該創建上一個類的新對象並再次添加它。 相反,您需要從超級視圖中刪除當前子視圖以返回。

-(IBAction)back:(id)sender
{

  [self.view removeFromSuperview];
}

最好為此使用導航或模型轉換,而不是僅僅執行addSubView

只需從當前視圖的超級視圖中刪除即可。

   [self.view removeFromSuperview]; 

 // or  if you want to call the method from your MainViewController use this line [objectFirstViewController removeFromSuperview];

由於您正在執行[self.view addSubview:Viewcontroller.view]; 從邏輯[self.view removeFromSuperview]; ,您應該執行[self.view removeFromSuperview]; 在添加的子視圖中。 盡管如此,建議您使用導航控制器並執行推/彈出操作以顯示/刪除視圖。 如果您的問題是導航欄,則可以通過self.navigationController.navigationBarHidden = YES;將其隱藏self.navigationController.navigationBarHidden = YES; 並使用您的工具欄作為替換。

代替這個

[self.view addSubview:objectFirstViewController.view];

采用

[self.navigationController pushViewController:objectFirstViewController動畫:是];

在FirstviewController集合的Viewdidload處,

self.navigationController.navigationBar.hidden = YES;

並使用這個

-(IBAction)back:(id)sender
{

        [self.navigationController popViewControllerAnimated:YES];
}

而已。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM