I have UINavigationController and UITableViewController as root.
myViewController = [[MyViewController alloc] init];
UINavigationController * navContr = [[UINavigationController alloc] initWithRootViewController:myViewController];
[self.window addSubview:[navContr view]];
I need bottom toolbar with buttons for MyViewController view. Found in google that I should set:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
[self.navigationController setToolbarHidden:NO];
}
return self;
}
But there is no bottom toolbar in my TableView. What should I do???
Google said that?
Try starting over and make a Tab Bar Controller. http://www.panolee.com/antonimasso/tutorials/tab-bar-application
Then add a TableViewController to it.
Related
I am creating a sample empty application and added UIViewController class,
Below is the code in the App delgate
TestViewController* viewController = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.window setRootViewController:navController];
Now i am rotating the App but in the UPsidedown Orientation Navigation bar is not Rotating I am attaching a screen shot
Then Added a the Below Method in App delegate
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskAll;
}
And in the Class i have added following methods
-(BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskAll);
}
In the Summary i have activated all the orientations can any One tell me the Solution for it
1) just You created a category for UINavigationController class and I defined these methods
In CustomViewController.h
#import <UIKit/UIKit.h>
#interface CustomViewController : UINavigationController
#end
In CustomViewController.m
#import "CustomViewController.h"
#interface CustomViewController ()
#end
#implementation CustomViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL) shouldAutorotate{
return YES;
}
-(NSUInteger) supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
}
And AppDelgate.m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
CustomViewController *navController = [[CustomViewController alloc] initWithRootViewController:self.viewController];
// Override point for customization after application launch.
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
2) And check Orientation In Target
3) Change the class from UINavigationController to the CustomViewController in my XIB in IB
4) Reset the Simulator and run the code, I think your problem will be solve.
i think you need to use
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return TRUE;
}
instead of shouldAutorotate
Upside Down button to ON in Supported interface orientations on Xcode
http://www.appcoda.com/ios-game-tutorial-maze-part-1/
`TestViewController* viewController = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
[self.window setRootViewController:viewController];`
remove the navigatioan bar from appdelegate and then check
I want to put a tab bar for one place in my app. From there i can load some other view controllers, but that's it. I don't want it for my whole application. All examples I read through are making the tab bar controller as the rootviewcontroller of the window.
So how do i do that?
Thank you!
his may help you, Consider your view controller is HomeView where from you are going to push a tab bar controller, The HomeView is loaded in view at below:
#import "AppDelegate.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HomeView *homeview = [[HomeView alloc] initWithNibName:#"HomeView" bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:homeview];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;
}
#end
then the .h, .m and .xib files of HomeView are going to be as follows:
In HomeView.h
#import <UIKit/UIKit.h>
#interface HomeView : UIViewController
{
IBOutlet UITabBarController *tabBarController;
}
-(IBAction)loadTabBar:(id)sender;
#end
and the HomeView.m is:
#implementation HomeView
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(IBAction)loadTabBar:(id)sender
{
[self.navigationController pushViewController:tabBarController animated:YES];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
and the .xib file must be as follows:
The tabBarController IBOutlet must be connected to the UITabBarController that on the .xib file. And that UITabBarController with two view controllers named FirstViewController, SecondViewController. Moreover that the HomeView must be inside a UINavigationController.
If not clarified with this answer, I'll update with detailed explanation.
The above is a way of loading tab bar controller using XIB method.
You can do this by coding like below by changing something in IBAction(button action) , In HomeView.m file:
#import "HomeView.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#implementation HomeView
-(IBAction)loadTabBar:(id)sender
{
FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:firstView, secondView, nil];
[self.navigationController pushViewController:tabBarController animated:YES];
}
#end
You have to make tabBar in app delegate and then add when ever you want using this
-(void)addTabBarController
{
AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate];
//[[[appdelegte navigationController] view]removeFromSuperview];
[[appdelegte window]addSubview:[[appdelegte tabcontroller]view]];
[[appdelegte tabcontroller]setSelectedIndex:0];
}
Did you notice that UINavigationBar is not set anymore when creating a UITableView, even after giving it a title or a button?
Now i'm going mad on how to put a navigation bar over my UITableView. It seems really impossible. I tried to add to my tableView a subview with the Navigation Bar, but seems worthless, because when I scroll down, the navigation bar scrolls down as wellm and it shouldn't.
Any ideas on how to implement it?
EDIT
Well, as always I went on File -> New -> File.. -> UITableView. Then i set a bit of code and when I wrote
self.navigationItem.title = #"MyTitle";
self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
and tried to test on Simulator, no Navigation Bar appeared.
My init code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"TabTitle";
self.tabBarItem.image = [UIImage imageNamed:#"img.png"];
self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
}
return self;
}
I can't explain why it doesn't appear anymore. I also tried to create a new project and import my classes from a project where the navigation bar appeared, but same result there too.
EDIT2*
The app is a tabBased application.
Here is the code took from the App delegate used to set up the tabBar.
UIViewController *viewController1, *viewController4;
UITableViewController *viewController2, *viewController3;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[FirstViewController alloc] initWithNibName:#"First_iPhone" bundle:nil];
viewController2 = [[Tips alloc] initWithNibName:#"Table" bundle:nil];
viewController3 = [[Favorites alloc] initWithNibName:#"Test_iPhone" bundle:nil];
viewController4 = [[SecondViewController alloc] initWithNibName:#"Second_iPhone" bundle:nil];
}
You are initing an UITabBarController and set 4 UIViewControllers as the corresponding UITabbarViewControllers. Since two of them are normal UIViewControntroller and two are UITableViewController there can not be a navigation bar. You have to load the viewController where you'd like the navbar form a UINavigationController. The correct way would be (assuming vc3 is the one where you'd like the navbar):
UIViewController *viewController1, *viewController4;
UITableViewController *viewController2, viewController3;
UINavigationController *vc3NavController;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[FirstViewController alloc] initWithNibName:#"First_iPhone" bundle:nil];
viewController2 = [[Tips alloc] initWithNibName:#"Table" bundle:nil];
viewController3 = [[Favorites alloc] initWithNibName:#"Test_iPhone" bundle:nil];
vc3NavController = [[UINavigationController alloc] initWithRootViewController:viewController3];
viewController4 = [[SecondViewController alloc] initWithNibName:#"Second_iPhone" bundle:nil];
}
Then load the vc3NavController instead of viewController3 as the corresponding tab.
So you have: UITabBarController -> UINavigationController -> YourViewController
Maybe Creating a Navigation Interface will help you too.
I implemented a TabBar Controller, it is initalised like this
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
firstView = [[FirstView alloc]init];
secondView = [[SecondView alloc]init];
firstItem = [[UITabBarItem alloc]initWithTitle:#"First" image:nil tag:0];
secondItem = [[UITabBarItem alloc]initWithTitle:#"Second" image:nil tag:1];
firstView.tabBarItem = firstItem;
secondView.tabBarItem = secondItem;
NSArray *viewArray = [[NSArray alloc]initWithObjects:firstView, secondView, nil];
[self setViewControllers:viewArray];
}
return self;
}
In the AppDidFinishedLoading I just did this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
mainbarController = [[MainBarController alloc]initWithNibName:#"MainBarController" bundle:nil];
[self.window setRootViewController:mainbarController];
[self.window makeKeyAndVisible];
return YES;
}
My problem is, I want to place the tabBar somewhere else, not at the bottom, as it is when I run the project. But I can´t find a xib File to my code. The mainwindow.xib is just an empty window. I know it´s very basic but I´m confused about this.
thanks anyway
Is the initWithNibName always called whenever programmatically or IB?
Example:
I have two viewController, one named PhotoViewController, the other named ViewController.
The PhotoViewController creates view programmatically, but with initWithNibName uncommented.
Here is my PhotoViewController.m:
- (void)loadView
{
NSLog(#"loadView in PhotoView");
UIView *view = [[UIView alloc] initWithFrame:[UIScreen
mainScreen].applicationFrame];
[view setBackgroundColor:[UIColor redColor]];
self.view = view;
[view release];
}
// Loading views from a nib file.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSLog(#"PhotoViewController initWithNibName=%#",nibNameOrNil);
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
Here is my call in ViewController.m
- (void) doSearchFlickr
{
PhotoViewController *pController = [[PhotoViewController alloc]init];
[self.view addSubview:pController.view];
[pController release];
}
Then I see something confusing me in the log:
2011-10-23 10:52:52.151 TableViewPG[1192:b303] PhotoViewController initWithNibName=(null)
2011-10-23 10:52:52.153 TableViewPG[1192:b303] loadView in PhotoView
According to the ViewControllerPGforiPhoneOS on page 30, if I override loadView programmatically, initWithNibName should not be called.
Is there any flaw(s) in my logic?
-[UIViewController init] just executes [self initWithNibName:nil bundle:nil]. You can easily check this by putting a breakpoint in your -[PhotoViewController initWithNibName:bundle:] and looking at the call stack.
Your -[PhotoViewController loadView] is fine.
The View Controller Programming Guide for iOS doesn't say anything about initWithNibName:bundle: on page 30.