简体   繁体   English

iOS-XCode 4-iPhone 6.0-向后视图添加后退按钮

[英]IOS - XCode 4 - iPhone 6.0 - Add back button to subview

Caution: N00b at IOS development. 注意:IOS开发中的N00b。 I have read several tutorials and code snippets over the past 10 days or so, and have gleaned enough info to be dangerous. 在过去的十天左右的时间里,我已经阅读了一些教程和代码片段,并且收集了足够的信息以免发生危险。 What I am trying to accomplish is to open a webview (as a sub-view) for user login, but for some reason my nav controller is not displaying the back button. 我要完成的工作是打开一个Web视图(作为子视图)以供用户登录,但是由于某种原因,我的导航控制器未显示“后退”按钮。 I've tried a few different things but to no avail, obviously I am doing something wrong, so seeking guidance. 我尝试了几种不同的方法,但都无济于事,显然我做错了,所以寻求指导。

My nav controller is set as the root controller, and next on the stack is epViewController. 我的导航控制器被设置为根控制器,堆栈上的下一个是epViewController。 This view controller is the starting point of the app. 该视图控制器是应用程序的起点。 From this view I have two buttons, one to login, and one to enter some data (inconsequential to this question). 从这个视图中,我有两个按钮,一个用于登录,一个用于输入一些数据(与这个问题无关)。

epViewController code snippet: epViewController代码段:

- (IBAction)buttonToLoginView:(id)sender
{
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];

    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/wsapi/user/login"]]];

    [self.view addSubview:webView];
}

This works great, as the sub-view shows the nav bar and the remote login page, but no back button. 这很好用,因为子视图显示导航栏和远程登录页面,但没有后退按钮。 Based on some other code snippets I tried the following (within the same epViewController file): 基于其他一些代码片段,我尝试了以下操作(在同一epViewController文件中):

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back???" style:UIBarButtonItemStylePlain target:nil action:nil];

    self.navigationItem.backBarButtonItem = backButton;
}

But this custom button only gets added if I click the the button to enter some data (actually goes to another concrete view controller vs a subview). 但是,仅当我单击该按钮以输入一些数据时,才会添加此自定义按钮(实际上是转到另一个具体的视图控制器与子视图)。

What do I need to do to add the back button to the subview? 我需要怎么做才能将后退按钮添加到子视图中? Is it even advisable to use a subview here, should I instead link it to another concrete view controller? 在这里甚至建议使用子视图,我应该将其链接到另一个具体的视图控制器吗?

Think of the navigation controller as a container for all of your actual view controllers. 可以将导航控制器视为所有实际视图控制器的容器。 In this sense, the navigation controller isn't really the "first on the stack", with your epViewController being "next on the stack". 从这个意义上讲,导航控制器并不是真正的“堆栈中的第一个”,而epViewController是“堆栈中的下一个”。 Instead, the navigation controller is the actual stack, with the epViewController being its first (and only) item. 相反,导航控制器实际的堆栈,而epViewController是它的第一(也是唯一)项目。 When your app launches, the navigation controller presents its root view controller, which is what you see onscreen. 应用启动时,导航控制器将显示其根视图控制器,即您在屏幕上看到的内容。 When you call the navigation controller's pushViewController: method, a new view controller is pushed onto the stack, and the back button is shown. 当您调用导航控制器的pushViewController:方法时,新的视图控制器将被推入堆栈,并显示后退按钮。

In your case, I would recommend not showing the web view as a subview on your main view. 对于您而言,建议您不要在主视图上将Web视图显示为子视图。 Instead, put it in its own view controller. 而是将其放在自己的视图控制器中。 In this view controller, you may add the back button as you attempted to formerly. 在此视图控制器中,您可以像以前一样添加后退按钮。 Then present that view controller modally using [self presentModalViewController:] . 然后使用[self presentModalViewController:]模态呈现该视图控制器。 This will feel more organized and fluid to the user, and avoids the business of fiddling with subviews on your main view. 这对用户而言将更有组织性和流畅性,并且避免了在主视图上摆弄子视图的麻烦。

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

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