UINavigationItem doesn't show up - uibarbuttonitem

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.

Related

side menu with UITabBarController and storyboards ios

I would like to implement a side menu bar in my application, I have UITabBarController and Storyboard, I have tried to integrate the menu bar MFSideMenu , but there is not a menu with UITabBarController for storyboard, just for nib files, so I need your help.
Many thanks
You simply do the following:
Create a UIStoryboard instance of the the relevant storyboard you need to set as the center controller, in case you have multiple storyboards. Use HomeStoryboard if you have just the standard storyboard in use.
Instantiate the view controller that is the initial view controller of your app, based on it's storyboard ID. You will have to set this in Storyboard (utilities pane).
Instantiate the view controller you want to use as the left menu.
Create an instance of MFSideMenuContainerViewController, with the center and left(or right, or both) menu controller you just created.
Set the instance of your MFSideMenuContainerViewController as the rootViewController of your app window.
Sample code:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"HomeStoryboard" bundle:nil];
UIViewController *homeViewController = [sb instantiateViewControllerWithIdentifier:#"homeViewController"];
UIViewController* leftMenuViewController = ......//Instantiate your left menu controller
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:homeViewController
leftMenuViewController:leftMenuViewController
rightMenuViewController:nil];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];

Push from UIButton to Navigation Controller

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

Xcode addSubview: (View Controller) - iPad

Hy,
so i have a question about adding a subview. I'm just created an iPad app which has a home screen i which i should add a subview at some point . But when i use :
NotesMainViewController *openNotes = [[NotesMainViewController alloc]initWithNibName:#"NotesMainViewController" bundle:nil];
[self.view addSubview:openNotes.view];
the main view imports that another view, but the controls on it like UITextField or anything else don't work. So, can please anybody tell me how can I add a view from ViewController to a another view (in that case homeViewController). I have been trying with the addsubview method but it doesn't work.

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)

ios4 - Load new view on clicking a button

I am currently using XCode 3.2.3 and iOS4. I'm working on an app that simply starts with one screen and on a button click moves to the next.
I have gone thorugh the ViewController programming guide and a post here.
What I am doing is very similar to whats happening on the post. So let me explain the steps, I followed:
In IB, drag and drop, a View from the library into the editor. I renamed the new UIView to myView.
In my AppControllerDelegate, I added the new view "myView" as a property of the view controller (File's Owner). I synthesized it as well in the implementation.
Now, in the implementation of the ViewController, within the button pressed action handler, I wrote the following lines of code:
[self.view addSubView: myView];
On clicking the button however, I do not see a new screen or my new view. However if I do this, I get a new screen or new view:
UIView *anotherView = [[UIView alloc] initWithFrame:self.view.frame];
[self.view addSubView: anotherView];
I do know that the best way is to do it with separate NIBs for each UIView. However, I am new to iPhone development and have not explored that path as yet.
My question: What am I missing upto step 3?
One way you could be able to do it is by trying it this way
myView = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle: [NSBundle mainBundle]];
[self.view addSubview: myView.view];
Try that and see if it is working.. if yours is a view based project then instead of [self.view addSubview: myView.view], just give [self.view addSubview : myView];

Resources