touchUpInside event on a UINavigationItem - ios

In my app's main view, I have a button that segues to a subclassed UINavigationController holding a UITableViewController to change settings.
To get back to the app's main view, I have this in the UINavigationController subclass:
navigationBar.setItems([UINavigationItem(title: "Back"),
UINavigationItem(title: "Settings")],
animated: false)
When I tab the Back button, the app segues back to the same view - but the navigation bar now only have the Settings item.
When I click the Back button, I want to dismiss the UINavigationController subclass and come back to my main view.
So how do I get a touchUpInside event on the UINavigationItem or is there some other way I can do this?

So it turns out there's a very easy way of doing this:
adding a UIBarButtonItem in the storyboard
subclass the UITableViewController
control-drag from the UIBarButtonItem to the UITableViewController subclass to create an action
dismiss the UITableViewController in the action

Related

Double tap on UITabbarItem to reload Viewcontroller/UITableView

I'm using UITabbarController in my project. All viewControllers are loading properly and the navigation from parentViewController in UITabbarController to a childViewController and then back to parentViewController when tabbaritem is tapped is working.
Now I want to reload the parentViewController if the user tap on the UITabbarItem while the parentViewController is already visible.
I'm not using and don't want to use viewwillappear method to reload the viewcontroller which will not work here in this case anyways as the view is already visible.

How to handle a view controller independantly in UITabBarController?

My app's root is a UITabBarController with 5 sections, each of them contains a UINavigationController.
I also want to add a chat feature in the app, that could be accessed with a rightBarButton present in every navigation bar of the app. I would like it to show a chat UIViewController on the screen, unselecting the currently selected tab bar item and without losing the navigation state of the five navigation controllers, even the one that was previously selected before tapping the chat button. What would be my best bet to do it?
Thanks for your help/ideas.
Step 1: In your storyboard add a ChatViewController
- Embed your ChatViewController in Navigation View Controller if you wanna have a navigation bar. Add a close BarButtonItem in your ChatViewController.
Step 2: Create a close Action in your ChatViewController and binding with BarButtonItem in the StoryBoard.
#IBAction func CloseAction(_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
Step 3:
In storyboard, select the Navigation Controllers with the rightBarButton and choose Present Modally and connect to Navigation Controller of the ChatViewController.
You can go to the ChatViewController without losing the navigation state of any navigation controller.

segue to the TabbarController

In my image, my first Tabbar is HomeViewController and the second Tabbar is CameraViewController.
What is the proper way to segue to the Tabbarcontroller? You can see the read line, I try to segue this but I
always get the back button in my HomeViewController and It display weird like not showing the navigation name.
In CameraViewController I hide the Tabbar for the use camera button. I try to use segue programmatically like this one.
[self performSegueWithIdentifier:#"sample" sender:sender]
but It doesn't work properly. Is this possible to segue to TabbarController?
You can't call
[self performSegueWithIdentifier:#"sample" sender:sender]
Because you're in TabBarController already. You could implement custom flow. By pressing "Back" button - just show TabBar and change selected tabBarItem for example…
update
used this
[self.tabBarController setSelectedIndex:0];

How to set common Bar Button Item on Navigation Bar

I want to set the common Bar Button Item on the right of the Navigation Bar, it should be display on all screens managed by Navigation Controller and it calls the same action.
Just for example, in Master Detail Application template, there is "addButton" on the right of the Navigation Bar, I want to display it on DetailViewController as well (it won't be work because action is missing though).
I have created a subclass of UINavigationController, in which I can change something like Navigation Bar color, but I can't set Bar Button Items. I can set Bar Button Items in each screen's ViewController so that I have to duplicate action for each screen.
Also I've tried to create a subclass of UINavigationBar, but I don't know if I can add Bar Button Item on it.
How to set common Bar Button Item on Navigation Bar?
Thanks in advance.
Another easy way to do instead of creating an extension
implement UINavigationControllerDelegate in root view controller
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
let backBarButton = UIBarButtonItem.init(title: "HINIBO", style: .Plain, target: self, action: Selector("menuButtonAction:"))
viewController.navigationItem.rightBarButtonItem = backBarButton
}
You can use category to approach it. Let's say UIViewController+NavigationBar category
1.Create a category
2.Add a method, -(void)setNavigationBarItem method (in this case) in .h file.
3.implement the method in .m file to deal with the set Bar Button Items stuff.
- (void)setNavigationBarItem
{
UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(something)];
self.navigationItem.rightBarButtonItem = searchItem;
}
4.In which viewController you want the NavigationBarItem, import the category header and call [self setNavigationBarItem] method.

Programmatically dismiss popover from view embedded in navigation controller

I have a view controller. When I press a button in it, a popover controller with a uitableview shows up. I select a row, which shows another view with some controls in it. When I press a button that says "Save Item", I want it to dismiss the popover. How do I do this?
Here's what I've tried:
Using the delegate and protocol pattern. This hasn't worked since in order to push another view inside my tableview, the whole thing must be embedded in a navigation view controller, so when I segue, it segues to a nav controller, not the tableview which I could set the popover delegate for.
Adding my main view as a member of the view I want to dismiss from. I don't know why this doesn't work.
The Hard Clean Way
There are four view controllers in the story, plus a popover controller. I will call the three view controllers "main view controller", "nav", "vcA", and "vcB". As I understand it, "nav" is the initial content view controller of the popover and has "vcA" as its root view controller.
main view controller -> popover controller -> nav -> vcA -> (later) vcB
When you present the popover from your main view controller, you keep a reference to the popover controller. This is what makes dismissing possible, as you know.
When you create the Save button, you make its target the main view controller and its action a method in the main view controller. You will have to set this up in code; it cannot be configured from a storyboard because you cannot form an action from one scene to another. (You are able to do this because you started out with a reference to nav and vcA when you configured the popover controller initially. Thus you can hand vcA a reference to self, the main view controller. If necessary, you can then pass this reference down the chain from vcA to vcB as vcB is summoned and pushed onto the navigation stack.)
Now the user taps Save, your main view controller's method runs, and it uses its reference to the popover controller to tell it to dismiss.
The Easy Dirty Way
The heck with all that. The main view controller registers for an NSNotification. The Save button posts that NSNotification. Done. :)
The Middle Way
You could set the whole chain up in your storyboard using a popover segue, and do the dismissal through an Unwind segue matched by an unwind method back in the main view controller. I never think of this initially, because I don't like popover segues very much. But it does work.
This is how I solved my problem (sorry for the bad english):
First, Create a property of UIStoryboardPopoverSegue in the VcA and set it from the main view controller.
Nav -> VcA_ViewController
#property (strong, nonatomic) UIStoryboardPopoverSegue *popupSegue;
Then, in the Main View Controller prepareForSegue set the property:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"your segue from the mainview to the navigation"]) {
UINavigationController *navigationController = (UINavigationController *)c;
VcA_ViewController *vcA = (VRPointOfInterestsFiltersViewController *) navigationController.topViewController;
vcA.popupSegue = (UIStoryboardPopoverSegue*)segue;
} }
Now, from the VcA controller you can have the dismiss button
- (IBAction)dismissPopoup:(id)sender {
[self.popupSegue.popoverController dismissPopoverAnimated:YES]; }
Don't forget to link the popOverSegue from the MainViewController to the NavController.
Hope it helps!

Resources