XCODE: presentViewController from UITableView does not show back button - ios

I've been reading tutorials, questions and answers here (Stackoverflow) and there but I can't get it. It doesn't work, so I must have missed something.
The situation is: from a table view, once the user selects a row, a new view with information is displayed. Not a detail view, I'd like to embed a navigation controller but I don't know how.
The code I wrote:
...
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIViewController *viewController =nil;
switch (indexPath.section) {
case termaSection:
switch (indexPath.row) {
case 0:{
viewController = [[TermasChavasqueira alloc]init];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:viewController];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
navController.navigationItem.backBarButtonItem = backButton;
[self presentViewController:navController animated:YES completion:nil];
//[self presentViewController:viewController animated:YES completion:nil];
break;
}
default:
break;
}
default:
break;
}
What I obtain on TermasChavasqueira.xib is a NavigationBar, with no back button. The command [self presentViewController:viewController animated:YES completion:nil]; works fine adding the button on Interface Builder, but its a regular button, not the arrowed one.
The next piece of code does not work either:
[self.navigationController pushViewController:navController animated:YES];
Can you help me?
Thanks a lot.

You can't show the typical backbuton with this way. It comes by default when you display a viewcontroller using pusviewcontroller: method.
Instead of doing this, add a UIToolBar to the TermasChavasqueira.xib and add a barbutton for dismissing the view.
Write the action of UIBarbutton like:
- (void)dismiss:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
If you still need to use UINavigationController change:
navController.navigationItem.backBarButtonItem = backButton;
to
navController.navigationItem.leftBarButtonItem = backButton;

Related

presenting a view controller

I want to go to a view controller when the button "Cancel" is pressed. I tried using this method but when I click 'Cancel", it just displays a black screen. I am a beginner at IOS.
-(void) cancelButtonPressed {
homeView = (HomeViewController *)[[UIViewController alloc]init];
[self presentViewController:homeView animated:YES completion:nil];
}
EDITED FOR COMMENTS:
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(cancelButtonPressed)];
self.navigationItem.leftBarButtonItem = cancelButton;
Try doing this:
HomeViewController *homeView = [[HomeViewController alloc]init];
[self presentViewController:homeView animated:YES completion:nil];
Also note that black is the default colour of the window. If you don't have any view controller named HomeViewController, and you present it, by default it will be visible as black colour to the user.
To Avoid this, you need to set the View for the newly presented view controller, which you can access by homeView.view
If you are using storyboard, then use storyboard ID:
HomeViewController *homeView = [self.storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
[self presentViewController:homeView animated:YES completion:nil];

iOS: dismissViewControllerAnimated crashes upon execution

I have below code that will call a modal view. I works fine when presenting the view controller but when i dismiss it, it crashes
_surveySummaryTableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
[_surveySummaryTableViewController setTableView:_surveySummaryTableView];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(closeModalView:)];
[[_surveySummaryTableViewController navigationItem] setRightBarButtonItem:doneItem];
[[_surveySummaryTableViewController navigationItem] setTitle:#"Response Summary"];
navController = [[UINavigationController alloc]initWithRootViewController:_surveySummaryTableViewController];
[navController setModalPresentationStyle:UIModalPresentationFormSheet];
[navController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
....
[self presentViewController:navController animated:YES completion:nil];
Clicking on the DONE button on the modal view will call the closeModalView: method below:
- (void)closeModalView:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
The weird part is, when I comment out the line below, the dismiss works fine. But of cause, the view will be empty without any TableView. What could I have missed? I tried other suggestions posted by others in SO but no luck. Thanks in advance.
_surveySummaryTableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
//[_surveySummaryTableViewController setTableView:_surveySummaryTableView]; //This line commented out
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(closeModalView:)];
[[_surveySummaryTableViewController navigationItem] setRightBarButtonItem:doneItem];
[[_surveySummaryTableViewController navigationItem] setTitle:#"Response Summary"];
===Additional info===
Considering it works when the line above being commented, the table actually populates a custom UITableViewCell. Could that be a problem?
I changed the property type from retain to strong for surveySummaryTableView
#property (strong, nonatomic) IBOutlet UITableView *surveySummaryTableView;
And be sure to make the child views to be of type weak or unsafe_unretained
Thanks #JeslyVarghese & everyone!
Since you are pushing navController, you must dismiss that. Try:
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
OR
[self.parentViewController.navigationController dismissViewControllerAnimated:YES completion:nil];

TabBar back button is not showing in ios app

I have created a UINavigationBarButton where i have placed a button called "update".When update button is clicked it is navigating to SecondViewController.But the problem is in the SecondViewController TabBar is not showing.What I'm trying to do is when i tap the back button in the SecondViewController it should get back to RootViewController.Here is the code
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title=#"GooGle";
[self.navigationItem setLeftItemsSupplementBackButton:YES];
[self.navigationController setToolbarHidden:NO];
UIBarButtonItem *nextView = [[UIBarButtonItem alloc] initWithTitle:#"update" style:UIBarButtonSystemItemPlay target:self action:#selector(updateAddress)];
self.navigationItem.rightBarButtonItem = nextView;
[nextView release];
}
-(void)updateAddress
{
SecondViewController *updateView=[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController presentViewController:updateView animated:YES completion:Nil];
[self.navigationItem setLeftItemsSupplementBackButton:YES];
}
When you tap the back button in SecondViewController use
[self dismissViewControllerAnimated:YES completion:nil];
Since you are not using
[self.navigationController pushViewController:youViewController animated:YES];
the navigationbar won't be visible, if you do so you will have your navigationbar and a back button by its own and while pressing the back button it will itself move to the previous view
share your results

Modal window not being dismissed

I have two programmatically created buttons you can see in my viewDidLoad method. On the modal window I have a button that calls the cancelSearch method via a delegate. When I place a breakpoint on my cancelSearch method it is hit, so I know my delegate is set up correct, but even though it calls this line [self dismissViewControllerAnimated:YES completion:nil]; it's not actually closing the modal window.
The code below is all from my main controller view.
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(showActionMenu:)];
actionButton.style = UIBarButtonItemStyleBordered;
UIBarButtonItem *searchButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(showSearchMenu:)];
searchButtonItem.style = UIBarButtonItemStyleBordered;
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 103.0f, 44.01f)];
NSArray* buttons = [NSArray arrayWithObjects:actionButton, searchButtonItem, nil];
[toolbar setItems:buttons animated:NO];
self.navigationItem.title = #"Census Management";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
[[RKClient sharedClient] get:#"censusmanagement" delegate:self];
}
- (IBAction)showActionMenu:(id)sender
{
[self performSegueWithIdentifier: #"CMActionSegue" sender: self];
}
- (IBAction)showSearchMenu:(id)sender
{
ehrxCMSearchView *search = [[self storyboard] instantiateViewControllerWithIdentifier:#"cmSearch"];
search.selectedOptions = self.selectedOptions;
search.delegate = self;
[self.navigationController pushViewController:search animated:YES];
}
- (void)cancelSearch:(ehrxCMSearchView *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
You would dismiss a modal view using something similar to:
[self dismissModalViewControllerAnimated:YES];
This will dismiss the modal view which was loaded using something similar to:
[self presentModalViewController:search animated:YES];
However looking at your code snippet, it appears the search view controller is being pushed onto the navigation stack using the following line:
[self.navigationController pushViewController:search animated:YES];
So I you probably need to pop the view from the navigation stack rather than trying to dismiss it as a modal view:
[self.navigationController popViewControllerAnimated:YES];
If your view controller is modally presented, you should use this:
[self.presentingViewController dismissModalViewControllerAnimated:YES];
The presentingViewController property is available in iOS 5 only. So, if you're targeting older versions of iOS, you have to use self.parentViewController instead (use the appropriate one for each iOS version, you have to handle this).
If you make this control in your parent/presenting view controller, then just call this:
[self dismissModalViewControllerAnimated:YES];

Changing viewController on Button Click

I am new to iPhone programming. what I am trying is I have one screen with a button. And I want to change the view controller not only the view when I click that button (I know how to add subview) because from that 2nd view controler, I have to go to the third view which is not possible if I add subview in first place. Can anybody please help me with that? Is this possible? and if yes, how? All the views and view controller are created programmaticaly. I am not using IB.
EDIT: here is the relevant code that fires when clicking button
-(id)showCurrentLoc:(id)sender {
locationController = [currentLocController alloc];
[entry removeFromSuperview];
[newLoc removeFromSuperview];
[currentLoc removeFromSuperview];
[self.view setBackgroundColor:[UIColor clearColor]]; //[self.view addSubview: [locationController view]];
[self.navigationController pushViewController:locationController animated:YES]; [locationController release];
return 0;
} //Location Controller is the tableViewController
Thanks
Vik
You can do something like this
YourViewController *objYourViewController = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
[self.navigationController pushViewController:objYourViewController animated:YES];
[YourViewController release];
Usually, you use a navigation controller for this sort of thing so that the user can easily go back to the previous view. Your view controller would then do something like this:
[self.navigationController pushViewController:someNewViewController animated:YES];
If you want to manage the view controllers yourself, you can always just change the window's rootViewController property. Please read View Controller Programming Guide for the complete picture.
UINavigationController is what you need. It manages a stack of UIViewControllers and if you want to add new UIViewController just push it into this navigation stack. It automates back button behavior for you, and you can pop your current UIViewController from stack whenever you are done with it.
You could work with a UINavigationController. Adding your first UIViewController like this in the init method:
[self setViewControllers:[NSArray arrayWithObject:viewController]];
And then when a button is click or a choice is made, your push the second few controller with (in the first viewController):
[self.navigationController pushViewController:controller animated:YES];
This way you will also get an automatic (back button). Basicly you create a stack of UIViewControllers that you can push and pop like with a normal stack.
I hope this helps. Look into the following:
UINavigationController Class Reference
- (void) loadViewAtIndex:(NSInteger)index {
[self unloadViewAtIndex:activeViewIndex];
switch (index) {
case 1:
{
if (viewController1 == nil)
{
viewController1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
}
viewController1.view.frame = CGRectMake(0.0, 0.0, viewController1.view.frame.size.width, viewController1.view.frame.size.height);
[window addSubview:viewController1.view];
}
break;
case 2:
{
if (viewController2 == nil)
{
viewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
}
viewController2.view.frame = CGRectMake(0.0, 0.0, viewController2.view.frame.size.width, viewController2.view.frame.size.height);
[window addSubview:viewController2.view];
}
break;
default:
break;
}
activeViewIndex = index;
}
- (void) unloadViewAtIndex:(NSInteger)index {
switch (index) {
case 1:
{
if (viewController1 != nil)
{
[viewController1.view removeFromSuperview];
[viewController1 release];
viewController1 = nil;
}
}
break;
case 2:
{
if (viewController2 != nil)
{
[viewController2.view removeFromSuperview];
[viewController2 release];
viewController2 = nil;
}
}
break;
default:
break;
}
}

Resources