简体   繁体   中英

IOS - XCode 4 - iPhone 6.0 - Add back button to subview

Caution: N00b at IOS development. 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. 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. 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:

- (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):

- (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". Instead, the navigation controller is the actual stack, with the epViewController being its first (and only) item. 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.

In your case, I would recommend not showing the web view as a subview on your main view. 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:] . This will feel more organized and fluid to the user, and avoids the business of fiddling with subviews on your main view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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