简体   繁体   English

在IOS中不使用导航控制器的后退按钮

[英]Back button without using navigation controller in IOS

I haven't embedd navigation controller with my view controller. 我尚未在视图控制器中嵌入导航控制器。 Now i'm using a code to go back to home screen to with the use of back button but there nothing appears, i don't know why. 现在我正在使用代码,使用后退按钮返回主屏幕,但是什么也没出现,我不知道为什么。 I don't want to use navigation controller in my vc. 我不想在我的vc中使用导航控制器。 This is the code, 这是代码,

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"car_marker.png"]
                                                               style:UIBarButtonItemStylePlain
                                                              target:self.navigationController
                                                              action:@selector(popViewControllerAnimated:)];
self.navigationItem.leftBarButtonItem = backButton;

It should look like this only, 看起来应该只有这样 在此处输入图片说明

You will need to change your approach because you cannot pop without a navigation controller . 您将需要更改方法,因为没有navigation controller就无法弹出。

You should embed your viewcontroller into navigation controller and then you can hide with [[self navigationController] setNavigationBarHidden:YES animated:YES]; 您应该将viewcontroller嵌入到navigation controller ,然后可以使用[[self navigationController] setNavigationBarHidden:YES animated:YES];进行隐藏[[self navigationController] setNavigationBarHidden:YES animated:YES]; and then you can add any kind of custom button to perform pop action. 然后您可以添加任何类型的自定义按钮来执行弹出操作。

For adding a custom back button to your view 用于向视图添加自定义后退按钮

- (void)addCustomBackButtonToView
{
    UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(20, 20, 44.0f, 30.0f)];
    [backButton setImage:[UIImage imageNamed:@"back.png"]  forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(popVC) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
}

- (void) popVC{
  [self.navigationController popViewControllerAnimated:YES];
}

You can hide your navigation controller by below code self.navigationController.navigationBar.isHidden = true 您可以通过以下代码隐藏您的导航控制器self.navigationController.navigationBar.isHidden = true

and use swipegesture option to move back, even though you have to use navigation controller,@ iPeter has a valid point. 并使用swipegesture选项向后移动,即使您必须使用导航控制器,@ iPeter也有一个有效点。

1) Add two ViewControllers make first as initial controller(entry point). 1)首先添加两个ViewControllers作为初始控制器(入口点)。

2) Add view on both ViewController with size you wish to have. 2)在两个ViewController上添加所需大小的视图。

3) Add buttons on both views as subviews. 3)在两个视图上添加按钮作为子视图。

4) create segue from firstButton ->(use show) -> second controller. 4)从firstButton->(使用show)->第二个控制器创建segue。

5) From second ->(use show) -> first controller. 5)从第二个->(使用show)->第一个控制器。

see the image below-: 见下图-:

在此处输入图片说明

This is not preferred way of doing pop/push. 这不是执行弹出/推送的首选方法。 You must use Navigation Controller(that works like a stack for controllers you push) that will provide you default Back button or you can create custom . 您必须使用导航控制器(对于您推送的控制器,它的作用类似于堆栈),它将为您提供默认的“后退”按钮,或者您可以创建custom。

在此处输入图片说明

You can not push or pop view controllers as you are not using the Navigation controller. 由于不使用导航控制器,因此无法推送或弹出视图控制器。 Try using Present & dismiss controller which can called from any controller Instace. 尝试使用可以从任何Instace控制器调用的Present&dismiss控制器。

Step 1 : Present to " Controller which is shown with no navigation bar but back button" 步骤1:向“没有导航栏,但显示后退按钮的控制器”显示

[self presentViewController:@"<ViewControllerToBePresented>" animated:NO completion:nil];

Step 2: add backBatton in ViewDidLoad 步骤2:在ViewDidLoad中添加backBatton

-(void)addBack{ - (无效){回加

UIButton *backBtn = [[UIButton alloc] initWithFrame: CGRectMake(20.0f, 20.0f, 50.0f, 30.0f)];
[backBtn setTitle:@"back" forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBtn];

} }

Step 3: Dismisscontroller to go back to previous scene 步骤3:Dismisscontroller返回上一场景

-(void) dismissViewController{ -(void)dismissViewController {

[self dismissViewControllerAnimated:NO completion:nil]; [自行关闭ViewControllerAnimated:没有完成:无];

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM