iOS: dismissViewControllerAnimated crashes upon execution - ios

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

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

My Custom UINavigationController won't show right button

I have a "LoginViewController" which presents a new Controller which is a subclass of UINavigationcontroller when clicking a button:
MPNavigationViewController *controller = [[MPNavigationViewController alloc] initWithRootViewController:[[MPQuestionFirstViewController alloc] init]];
[self presentViewController: controller animated:YES completion:nil];
"MPNavigationViewController" subclass UINavigationController and uses "REMenu" to have a sliding-from-top menu ("Link") and on viewDidLoad I try to add a right button to open it:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *toggleMenuButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(toggleMenu:)];
self.navigationItem.rightBarButtonItem = toggleMenuButton;
[self initMenu];
}
It doesn't show any button on the navigation bar. Why could it be?
If I try to add the button from one of the "viewControllers" that will handle sections on the menu. It shows the button, but it doesn't paint it at all.
Thanks.
You are using subclass of UINavigationcontroller which is not actually view controller.
There is only one solution, You need to create your custom button and add it to UINavigationbar as a subview..
Use this hope it will help.
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(goToDoneButtonAction)];
self.navigationItem.rightBarButtonItem = doneButton;

XCODE: presentViewController from UITableView does not show back button

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;

barbuttonitem now showing up, presentmodalviewcontroller

I have a need to show PDF files modally, when my user clicks on some buttons I load the PDF into a class and present it modally, now I need to provide the user with ab utton on the modalVC to close or go back so I added RightBarButtonItem, the prioblem is that its not showing up? This is code the load
the PDF
[self.pdfViewController.view addSubview:pdfViewer];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:pdfViewController];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(shutDown)];
[self presentModalViewController:navController animated:YES];
What am I doing wrong here?
You are attaching the bar button to the wrong viewController. It should belong to the viewController inside your presented navigationController. This is pdfViewController.
Additionally...
assuming self.pdfViewController is the same object as pdfViewController, it's best if you refer to it consistently
presentModalViewController is deprecated since at least ios5.0, you should be using presentViewController:animated:completion
So your code should look something like this
[self.pdfViewController.view addSubview:pdfViewer];
UINavigationController *navController =
[[UINavigationController alloc] initWithRootViewController:self.pdfViewController];
self.pdfViewController.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(shutDown)];
[self presentViewController:navController
animated:YES
completion:nil];
Your shutDown method will be sent to the presenting ViewController, not to your presented pdfViewController... so that's where you need to implement it.

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

Resources