Push from UIButton to Navigation Controller - view

So I have a Navigation Controller (see sketch below) that "houses" a Tab Bar Controller. The Tab bar controller has 4 tabs (views). Now, inside one of those views, I have a UIButton. I WANT to create a Push segue from this button (highlighted) to another view, but I need the view it's pushing to to have Navigation capabilities. That said, as we all know, you can't push to a Navigation Controller. How can I get around this, and push from the UIButton to a ViewController with a Navigation bar?
EDIT:
So far, this is the code used to push from the UIButton to the View Controller.
HomeViewController.h
- (IBAction)favoritesPressed;
HomeViewController.m
- (IBAction)favoritesPressed {
FavoritesViewController *favoritesView = [[FavoritesViewController alloc] initWithNibName:#"FavoritesViewController" bundle:nil];
[self.navigationController pushViewController:favoritesView animated:YES];
}

Related

UINavigationItem doesn't show up

In IB I've put an View Controller with a Table View and a Navigation bar. Because I want to change the actions (and text) on the buttons when the table is being edited, I decided to create the button programatically.
In the implementation file, I've set up the button (copied from another post on Stackoverflow):
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Title";
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStylePlain target:self action:#selector(EditTable:)];
self.navigationItem.rightBarButtonItem = editButton;
}
But when I run this in the simulator, I don't see the button. Is this the right approach for my goal? Or what am I doing wrong?
Later, I also want to add a Back, Add and Done button.
You probably did not created iboutlet for that navigation bar. Self.navigationItem is a property which is available on any viewcontroller and is applied only if there is a navigationcontroller which manages your viewcontrollers stack. There is no relation to manually created navigationItems..
I assume that either outlet for that navigationbar or going to storyboard, click your viewcontroller and then Editor->Embed In->navigation controller will help you to achieve disered results.

How do i set a modal segue (programmatically) to a push segue

i have an navigation controller in my storyboard, but for one reason i have to make one segue programmatically, but how do i make a push segue programmatically?
this is my code so far:
- (IBAction)nextviewButton:(id)sender {
HolesViewController *Holes = [self.storyboard instantiateViewControllerWithIdentifier:#"HolesViewController"];
Holes.nameString = self.NameField.text;
[self presentViewController:Holes animated:YES completion:nil];
}
Alternatively, I actually prefer to define a segue right in my storyboard, but rather than originating from the button, I have it originate from the view controller itself, e.g.:
Then, give that segue a "storyboard id", say "Details", and then you can invoke that segue programmatically via:
[self performSegueWithIdentifier:#"Details" sender: ...]; // you can specify either the button or `self` for the `sender
I like this because that way, the storyboard continues to visually represent the flow of the app (as opposed to possibly having scenes floating out there with no segues). And you can use the exact same construct for both push and modal segues (and the view controller that's presenting the next view controller doesn't care which one the storyboard uses).
[self.navigationController pushViewController:Holes animated:YES];
Got it

XCode Storyboard: how to create a segue when the segue trigger is not on storyboard

So I started playing with storyboards in XCode 4.3.2. I started with the Master-Detail application (split view application for iPad). In the detail view (named DetailVC) I have a view where I displayed an array of custom views.
The custom views (named GridView) have a tap gesture recognizer which is supposed to handle the tap event on each custom view. Tapping a GridView pushes a view controller show some search results (named SearchResultsVC).
With GridView created in a separate nib file, and the DetailVC and SearchResultsVC reside in storyboard, how can I create a push segue with destination SearchResultsVC? I just created a segue between DetailVC and SearchResultsVC? Is there someway I can trigger this segue programatically from inside the GridView class when tap gesture is recognized????
In the method where you handle the tap use:
[self performSegueWithIdentifier:#"yourSegueIdentifier" sender:self];
In your StoryBoard control drag from your DetailVC to your SearchResultVC and choose what type of segue you would like. Make sure to name your segue identifier the same as the one in the method above in attributes inspector.
I'm gonna try and improve my answer I messed it up I think:
1) In your DetailVC.h create an instance variable for your GridView like this
IBOutlet UIView * gridView;
also create a getter method and an IBAction for your grid view like this
-(UIView *)gridView;
-(IBAction)myGridGotPressed:(id)sender;
2)Now in your DetailVC.m implement your methods like this
-(UIView *)gridView{
if(!gridView){
[[NSBundle mainBundle] loadNibNamed:#"GridView" owner:self options:nil];
}
return gridView;
}
Also implement your IBAction like this
-(IBAction)myGridGotPressed:(id)sender{
[self performSegueWithIdentifier:#"yourSegueIdentifier" sender:self];
}
3) To make this work you need to change the filesOwner class of your GridView to DetailVC and then hook up the outlets and Actions as normal.
I hope that helps.

Navigation Bar Layout Issue on Tab Bar Controller + Navigation Controller

I've followed a how-to to create a simple Tab bar controller with a navigation controller in the first tab. Until here all is working correctly, expect a strange issue on the layout.
When the app starts the first time, the Navigation Bar on the top of the first loaded nib is a little outside of the view. I cannot figure out why this happen. In the first view there is a button "Add new System" that opens a modal view. If I press this button and the modal view appears and then I dismiss the modal going back to the initial view, then the Navigation bar at the top is placed/refreshed correctly. The same happens if I press the second TAB (it's a simple nib without Navigation controller for now) and then back to the first TAB, the Navigation bar is placed in the correct position.
Here a screenshot on the first startup:
And here when I press the modal view or the second TAB and then back to the first view:
The code is quit simple following one of the numerous tutorials on the net. I'm NOT using storyboard. Only customization was adding the buttons on the top of the Navigation Bar:
UIImage *editbuttonImage = [UIImage imageNamed:#"edit_pressed.png"];
UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
[editButton setBackgroundImage:editbuttonImage forState:UIControlStateNormal];
editButton.frame = CGRectMake(0, 0, editbuttonImage.size.width, editbuttonImage.size.height);
[editButton addTarget:self action:#selector(leaveEditMode)
forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:editButton];
[editButton release];
[editbuttonImage release];
No other modifications were made. The nib was used before in a single view. Then I've tried to insert it into a TAB Controller + Navigation Controller.
I could post the whole code in case it's needed. Under Select System there is a Table View, in these pictures empty, also not shown.
Thank's for the help!
Simon
I've solved the issue myself. On startup I've setup to hide the status bar and shown it again in the app delegate. The directive used :
[[UIApplication sharedApplication] setStatusBarHidden:NO];
was after adding the navController as subview. Also the Navigation controller BAR was not out of the view, simply under the status bar.
Hope this helps someone :)
Cheers, Simon

Hide/Unhide UINavigationbar when the screen is tapped

I'm very new with iOS Development and I have just created one of my first apps, in my .xib file I have a UINavigationBar that I want to hide/show when a part of the screen is tapped by the user (like in the Photo app). I've found some snippets online but I don't know where and how to use those.
I'd appreciate a lot if somebody could give me detailed informations about how to do this.
Add this toggle method anywhere in your UIViewController. This hides on first tap and shows again in second tap.
- (void)toggleNavBar:(UITapGestureRecognizer *)gesture {
BOOL barsHidden = self.navigationController.navigationBar.hidden;
[self.navigationController setNavigationBarHidden:!barsHidden animated:YES];
}
If there is no navigation controller, link the navigation bar with an IBOutlet and replace with
- (void)toggleNavBar:(UITapGestureRecognizer *)gesture {
BOOL barsHidden = self.navBar.hidden;
self.navBar.hidden = !barsHidden;
}
Then add the following in the method -(void)viewDidLoad {}
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(toggleNavBar:)];
[self.view addGestureRecognizer:gesture];
[gesture release];
If the view where you are going to tap is a UIWebViewController, you have to add the protocol to the view controller and set it as delegate gesture.delegate = self; then add the following:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
This is needed because the UIWebViewController already implements its own gesture recognizers.
Ultimately, you want to send the -setHidden: message to your navigation bar. The easiest way to do this is to make an Outlet and an Action in your in your view controller. Then, in your .xib file, connect the navigation bar to the outlet and some button (even a large, full screen one) to the action.
Outlets and Actions are basic techniques used over and over in iOS
(and Mac) programming, so if you don't understand them, best go read
up on them now. Every beginning iOS/Mac programming book covers this
topic as does Apple's own Getting Started guide (pay particular
attention to the Configuring the View section).
Inside your action, send a message to the outlet like so:
-(void)myButtonAction:(id)sender{
[[self myNavigationBarOutlet] setHidden:YES];
}
This will hide the navigation bar whenever your button is tapped.
(This assumes you have a UINavigationBar in your .xib like you say. These directions will be different if you're working with a UINavigationController that manages its own UINavigationBar)

Resources