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.
Related
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];
The Application I am working on has dialogs/UI entirely done by hand/code.
Presently it only has Views/ViewControllers and no NavigationControllers yet.
"Navigation" is done by displaying controllers on top of another, and/or hiding views.
I am trying to display a ABUnknownPersonViewController.
I manage to do it, but I cannot dismiss it; I tried to add a button but it did not even showed up. I am using the following code, but I do not see any dismiss/cancel in my UIViewController, how should I modify it ?
- (void)AddCallerToAdressBook
{
ABUnknownPersonViewController *controller = [[ABUnknownPersonViewController alloc] init];
controller.allowsAddingToAddressBook = YES;
UINavigationController *newNavigationController = [[UINavigationController alloc] initWithRootViewController:controller ];
newNavigationController.title=#"AddCaller";
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self action:#selector(dismissed)];
[newNavigationController.navigationItem setLeftBarButtonItem:leftBarButton];
[self presentModalViewController:newNavigationController animated:YES];
}
I am trying to present a UINavigationController modally from a UIViewController. From previous experience, I'm pretty sure that when I do this there should be a cancel button by default in the navigation bar, however with the following code the navigation bar is completely blank. Any ideas?
UIViewController *rootVC = [[UIViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootVC];
[self presentViewController:navController animated:YES completion:nil];
You will not get any bar button items by default. You need to either add the button before presenting, or, more correctly, add the button from inside the root view controller. Normally, you want to have delegation set up, where the presented view controller notifies the presenting view controller that it should dismiss it. Self-dismissing from inside the root view controller is usually not recommended.
– presentViewController:animated:completion: only going to present your view controller as a modal view controller, even you set up that as a navigation controller. the easiest solution in your case is in the navController set the left bar button as the cancel button, and call dismiss when click that button,
UIBarButtonItem *cancelButton= [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(dismissViewController)];
navController.navigationItem.leftBarButtonItem = cancelButton;
- (void)dismissViewController
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Hope that would help.
Here is my code for calling a new view controller that is wrapped in a UINavigationController. I want a simple back button on the restaurantResults controller. My selector does not seem to work. I've tried using pop commands. Will those work since I am using presentViewController and not any sort of push?
Pretty sure my selector is wrong right now because it says self.navigationController, which can't possibly be right.
Here is where I call the new view controllers and set up the back button:
- (void)searchBarSearchButtonClicked:(UISearchBar *)foodNearSearchBar
{
restaurantsViewController *restaurantResults = [[restaurantsViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:restaurantResults];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:#selector(backPressed:)];
restaurantResults.navigationItem.leftBarButtonItem = backButton;
[self presentViewController:navController animated:YES completion:Nil];
}
Here is my selector:
-(void)backPressed: (id)sender
{
[self.navigationController popViewControllerAnimated: YES]; // or popToRoot... if required.
}
I also tried:
- (void)backPressed:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
};
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:nil
ction:#selector(backPressed:)];
restaurantResults.navigationItem.leftBarButtonItem = backButton;
these code should be used on the restaurantsViewController;
the target is self.
If you are using a UINavigationController and you want the default iOS back button, you do not need to set it programatically. There is literally nothing to add, it's built in by default.
By pushing the UIViewController into the navigation controller, it'll be in the navigation controller stack of controllers, and iOS will therefore add a navigation bar, and a back button when you dig into the navigation stack.
If its not working, check the following :
That the controller you're pushing from is the root of the UINavigationController. You can set this by code or in storyboard. (you're OK)
That you're pushing from the navigation controller and not the viewcontroller. Essentially you have to do navigationcontroller.push() and not self.push(), otherwise it just won't work. (depends what self is here, but I'm pretty sure your mistake is here)
I see that you're using presentViewController which is for modals, its fine if that is your intent, but if you want a navigation stack, why not embed self in a navigation controller in the first place, hide its navigation bar, and then simply push your next controller onto it.
That way you dont have to manually create that back button, and let iOS deal with everything.
If you must do it that way, you can only "dismiss" when you "present", and "pop" when you "push". But I don't have enough information to know why yours does not work. Try a few things and give us more feedback. But from what I see you're going for a more complicated solution than necessary.
Also I would really start with a simple button that says "close" and see if it works that way before trying to embed it in a bar with an item. That way you tackle one problem, and one new concept at a time
//This works - Just did it
//You can create back button in the view you want to see back button -
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Login"
style:UIBarButtonItemStylePlain
target:self
action:#selector(backPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
[self.navigationItem setHidesBackButton:NO];
// And just do dismiss the view controller in the viewcontroller where the back button is - as below-
- (void)backPressed:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
};
I push new StatusViewController(i created UITabbarViewController programmically in it) from rootviewController (UInavigation controller). Now, i want to click on logout button, it push rootviewcontroller but i used below code, it push rootviewcontroller fine but it still tabbar at the bottom.
This code to call rootviewcontroller:
LoginTab *loginView = [[LoginTab alloc] init];
[self.navigationController pushViewController:loginView animated:YES];
[loginView release];
And this code is created UItabbarcontroller in StatusViewController:
self.tab=[[UITabBarController alloc]init];
UploadTab *uploadview=[[UploadTab alloc]initWithNibName:nil bundle:nil];
UINavigationController *uploadTabItem = [[[UINavigationController alloc] initWithRootViewController: uploadview] autorelease];
uploadview.title=#"Uploading";
uploadview.tabBarItem.image=[UIImage imageNamed:#"Uploading.png"];
self.title = #"FirstViewControllerTitle";
//SecondViewController
ConvertTab *convertView=[[ConvertTab alloc]initWithNibName:nil bundle:nil];
UINavigationController *convertTabItem = [[[UINavigationController alloc] initWithRootViewController: convertView] autorelease];
convertView.title=#"Convert";
convertView.tabBarItem.image=[UIImage imageNamed:#"Convert.png"];
//ThirdViewController
CompletedTab *completedView=[[CompletedTab alloc]initWithNibName:nil bundle:nil];
UINavigationController *completedTabItem = [[[UINavigationController alloc] initWithRootViewController: completedView] autorelease];
completedView.title=#"Completed";
completedView.tabBarItem.image=[UIImage imageNamed:#"Completed.png"];
UIBarButtonItem * LogoutItem= [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Logout.png"] style:UIBarButtonItemStylePlain target:self action:#selector(logout)];
self.navigationItem.rightBarButtonItem = LogoutItem;
self.tab.viewControllers=[NSArray arrayWithObjects:uploadTabItem,convertTabItem, completedTabItem, nil];
// [self.view insertSubview:self.tab.view belowSubview: uploadview.view];
[self presentModalViewController:self.tab animated:NO];
You can see this image :
Use popToRootViewControllerAnimated method instead of pushViewController:
[self.navigationController popToRootViewControllerAnimated:animated];
Your hierarchy does not seem correct. Your tab bar controller should be the root view controller. For each tab, you can have a navigation controller which has its own controllers to push and pop. That said, your tab bar will always be visible since that is the behavior that is expected when you have a tab bar based app. If you want to present a view which does not show a tab bar, you will need to present that view controller as a Modal view controller on top of your tab bar controller.