I updated my version of XCode and now I get the following error:
*application windows are expected to have a root view controller*
I read that the error is because I have no RootViewController assigned .
The problem is that I have multiple subviews and any is a main view.
Example:
// Add the view controller's view to the window and display.
[window addSubview:infoTabViewController.view];
[window addSubview:shareTabViewController.view];
[window addSubview:tabViewController.view];
[window addSubview:mainTabViewController.view];
How you could assign these subviews to a RootViewController?
Thanks.
Firstly you Write in appdelegate.m to set root view controller
- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.logviewController = [[LoginViewController alloc]init];
self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.logviewController];
[self.window setRootViewController: self.navigationController];
[self.window makeKeyAndVisible];
return YES;
}
after that add tabbar controller and write code in view did load
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;
self.view.backgroundColor = [UIColor whiteColor];
self.title = #"Welcome To MyConnect";
_friendviewcontroller = [FriendsViewController new];
_homeviewcontroller = [HomePageViewController new];
_notifviewcontroller = [NotificationViewController new];
_messageviewcontroller = [MessagesViewController new];
self.tabbarcontroller = [[UITabBarController alloc]init];
self.viewControllers = [NSArray arrayWithObjects:_homeviewcontroller,_messageviewcontroller,_friendviewcontroller,_notifviewcontroller, nil];
//UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [self.tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [self.tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [self.tabBar.items objectAtIndex:3];
[item0 setFinishedSelectedImage:[UIImage imageNamed:#"blue-home-icon.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"home_2.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:#"email-icon-env.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"message_2.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:#"friendreq_icon.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"friendreq_2.jpg"]];
[item3 setFinishedSelectedImage:[UIImage imageNamed:#"notification_icon.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"bell-512.png"]];
UIImage *img1=[UIImage imageNamed:#"menu-2-128.png"];
CGRect frameimg1 = CGRectMake(340, 0, 35,35);
UIButton *signOut=[[UIButton alloc]initWithFrame:frameimg1];
[signOut setBackgroundImage:img1 forState:UIControlStateNormal];
[signOut addTarget:self action:#selector(collectionmenutwo:)forControlEvents:UIControlEventTouchUpInside];
[signOut setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithCustomView:signOut];
self.navigationItem.rightBarButtonItem=barButton;
item0.title = #"Home";
item1.title = #"Messages";
item2.title = #"Friends Requests";
item3.title = #"Notification";
}
take a UITabBarController and make this rootViewController.
and add all of your subview on this TabBarController.
code sample
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
UITabBarController *tabBarController = [[UITabBarController alloc]init];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
FirstViewController *firstViewController = [storyboard instantiateViewControllerWithIdentifier:#"FirstViewController"];
SecondViewController *secondViewController = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
tabBarController.viewControllers = #[firstViewController,secondViewController];
[self.window setRootViewController: tabBarController];
[self.window makeKeyAndVisible];
But don't add NavigationController
Related
I am opening a UITabBarController with MMDrawerController on the left side.
So from my delegate didFinishLaunchingWithOptions I am making a MMDrawerController with centerNav is a TabBarController and leftNav is a ViewController with Navigation controller. So as expected the tabbar is coming with the mmdrawer but there is a black gap on top of the LandingTabBarController.If I open the LandingTabBarController directly then it is coming from the top. Whats going wrong?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LandingTabBarController *landingTabBarController = [[LandingTabBarController alloc]initWithManagedObjectContext:self.managedObjectContext];
UIViewController * drawerNavController = [storyboard instantiateViewControllerWithIdentifier:#"DrawerViewController"];
UINavigationController *centerNav = [[UINavigationController alloc]initWithRootViewController:landingTabBarController];
UINavigationController *leftNav = [[UINavigationController alloc]initWithRootViewController:drawerNavController];
centerNav.navigationBar.barStyle = UIStatusBarStyleLightContent;
leftNav.navigationBar.barStyle = UIStatusBarStyleLightContent;
_drawerController = [[MMDrawerController alloc] initWithCenterViewController:centerNav leftDrawerViewController:leftNav];
_drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModePanningCenterView;
_drawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModePanningCenterView;
_drawerController.showsShadow = NO;
//[_drawerController setDrawerVisualStateBlock:[MMDrawerVisualState slideVisualStateBlock]];
[_drawerController setDrawerVisualStateBlock:MMDrawerSideNone];
self.window.rootViewController = _drawerController;
[self.window makeKeyAndVisible];
return YES;
}
This is my LandingTabBarController viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
UIViewController *firstViewController = [[UIViewController alloc]init];
[firstViewController.view setBackgroundColor:[UIColor redColor]];
UIViewController *secondViewController = [[UIViewController alloc]init];
[secondViewController.view setBackgroundColor:[UIColor greenColor]];
[tabViewControllers addObject:firstViewController];
[tabViewControllers addObject:secondViewController];
[self setViewControllers:tabViewControllers];
firstViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"one" image:[UIImage imageNamed:#"icon_feed"] tag:1];
secondViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"two" image:[UIImage imageNamed:#"icon_apps"] tag:2];
}
Screenshot of the debugger
I'm trying to change views within an app I am editing the code for. But the navigation bar doesn't seem to update in specific instances when presenting a new viewcontroller. Right now the behavior looks like this:
I navigate to a tableviewcontroller by clicking on a tab in the tabbar,
then I navigate to a viewcontroller from this tableviewcontroller using:
settingsController = [SettingsController create];
[self.navigationController pushViewController: settingsController animated: YES];
which I believe calls this code:
+(id)create
{
SettingsController*settings = [[SettingsController alloc] init];
NSBundle*bundle = [NSBundle bundleForClass: [SettingsController class]];
[bundle loadNibNamed: #"SettingsController" owner: settings options: NULL];
settings.view.frame = CGRectMake(0, 0, 320, 411);
settings.title = #"Settings";
return settings;
}
Then from there I navigate to another view controller using:
SearchViewController*searchView;
searchView = [[SearchViewController alloc] initWithNibName:#"SearchView" bundle: [NSBundle mainBundle]];
[self presentViewController:searchView animated:YES completion:nil];
and this is where the behavior starts getting buggy, the navigation bar doesnt update to the change in the view controller. I didn't write this code but it has been giving me a headache trying to clean it up.
If you are using a navigationController then you shouldn't call
[self presentViewController:searchView animated:YES completion:nil];.
You should use
[self.navigationController pushViewController:searchView animated:YES];
Also, it would be better to use the standard in-built function to initialize a new view controller.
SettingsController*settings = [[SettingsController alloc] initWithNibName:#"SettingsController" bundle:nil];
[self.navigationController pushViewController:settings animated:YES];
and then use the default method in the view controller.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Settings";
//other view changes.
}
return self;
}
See this example for the way of initializing tabbarcontroller with navigation controller
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *viewController1, *viewController2,*viewController3;
viewController1 = [[ViewController alloc] init];
viewController2 = [[FormStatusViewController alloc] initWithNibName:#"FormStatusViewController" bundle:nil];
viewController3 = [[DocumentsViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *nav1 =[[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController *nav2 =[[UINavigationController alloc] initWithRootViewController:viewController3];
nav.navigationBarHidden=YES;
nav1.navigationBarHidden=YES;
nav2.navigationBarHidden=YES;
NSArray *viewsArray = [[NSArray alloc] initWithObjects:nav,nav1,nav2, nil];
self.formTabBar= [[UITabBarController alloc] init];
[self.formTabBar setViewControllers:viewsArray];
UITabBar *tabBar = self.formTabBar.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
tabBarItem1.title = #"FORM";
tabBarItem2.title = #"STATUS";
tabBarItem3.title = #"DOCUMENTS";
UIImage *tabBackground = [[UIImage imageNamed:#"tab_bg.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setBackgroundImage:tabBackground];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"form.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"form_h.png"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"status.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"status_h.png"]];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:#"documents.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"documents_h.png"]];
} else {
tabBarItem1.selectedImage = [[UIImage imageNamed:#"form_h.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.image = [[UIImage imageNamed:#"form.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.selectedImage = [[UIImage imageNamed:#"status_h.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.image = [[UIImage imageNamed:#"status.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.selectedImage = [[UIImage imageNamed:#"documents_h.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.image = [[UIImage imageNamed:#"documents.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
}
[[UITabBar appearance] setSelectionIndicatorImage:
[UIImage imageNamed:#"tab_select_indicator.png"]];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil] forState:UIControlStateHighlighted];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = self.formTabBar;
[self.window makeKeyAndVisible];
return YES;
}
Now in your view controller suppose the firstviewcontroller in first tab you want to navigate to another view controller in that tab then use below code
SearchViewcontroller *searchView =[[SearchViewcontroller alloc]init];
[self.navigationController pushViewController:searchView animated:YES];
I am trying to create an application with a UITabBarController. I have followed several tutorials online and it seems like I'm missing something since my UITabBarController does not have text but you can switch from one View to another.
Here is my didFinishWithLaunchingOptions function:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
ListViewController *listVC = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];
self.tabController.viewControllers = [[NSArray alloc] initWithObjects:viewController1, listVC, nil];
self.window.rootViewController = self.tabController;
[self.window makeKeyAndVisible];
[self createCopyOfDatabaseIfNeeded];
return YES;
}
What am I missing out on?
You need to set the tab bar image and title of each view controller:
self.tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
ListViewController *listVC = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];
viewController1.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"VC1" image:[UIImage imageNamed:#"tab_ yourViewController.png"] tag:0];
listVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"listVC" image:[UIImage imageNamed:#"tab_listvc.png"] tag:1];
self.tabController.viewControllers = [[NSArray alloc] initWithObjects:viewController1, listVC, nil];
It's also possible (and often preferred) to set the tabBarItem for each view controller in viewDidLoad, I.e.:
- (void)viewDidLoad
{
[super viewDidLoad]; // Be a good citizen!
self.tabBarItem = [[UITabBarItem alloc] init...];
...
You can set tabbar item for each view with code
UITabBar *tabBar = self. tabController.tabBar;
UITabBarItem *tabBarItem0 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:1];
[tabBarItem0 setTitle:#"title1"];
[tabBarItem0 setImage:[UIImage imageNamed:#"img1"]];
[tabBarItem1 setTitle:#"title2"];
[tabBarItem1 setImage:[UIImage imageNamed:#"img2"]];
self.window.rootViewController = self.tabController;
[self.window makeKeyAndVisible];
I create basic TabBarController with 2 ViewControllers.
It worked but it have bug and i want something about it.
Problem1.
When I run (default ViewController is FirstViewController) name of SecondViewController in TabBar is not appear.
I want following this picture.
Problem2.
In my code default view is FirstViewController.
If I want to set default view is SecondViewController
by Same Tab Bar(FirstView Tab : Left ,and SecondView Tab : Right)
following this picture. How to resolve it.
This is my example code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FirstViewController *firstVC = [[FirstViewController alloc] init];
UINavigationController *firstNVC = [[UINavigationController alloc] initWithRootViewController:firstVC];
firstNVC.navigationBar.barStyle = UIBarStyleBlack;
SecondViewController *secondVC = [[SecondViewController alloc] init];
UINavigationController *secondNVC = [[UINavigationController alloc] initWithRootViewController:secondVC];
secondNVC.navigationBar.barStyle = UIBarStyleBlack;
UITabBarController *tabController = [[UITabBarController alloc]init];
tabController.navigationItem.hidesBackButton = NO;
tabController.viewControllers = [NSArray arrayWithObjects:firstNVC, secondNVC, nil];
tabController.navigationController.navigationBarHidden = NO;
self.window.rootViewController = tabController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Thank You. ^^
UIViewController *viewController1 = [[[viewController1 alloc] initWithNibName:#"viewController1" bundle:nil] autorelease];
UIViewController *viewController2 = [[[viewController2 alloc] initWithNibName:#"viewController2" bundle:nil] autorelease];
UIViewController *viewController3 = [[[viewController3 alloc] initWithNibName:#"viewController3" bundle:nil] autorelease];
navControl1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navControl2=[[UINavigationController alloc]initWithRootViewController:viewController2];
navControl3=[[UINavigationController alloc]initWithRootViewController:viewController3];
navControl1.navigationBar.tintColor=[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
navControl2.navigationBar.tintColor=[UIColor blackColor];
navControl3.navigationBar.tintColor=[UIColor blackColor];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.delegate=self;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navControl2,navControl1,navControl3, nil ];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:#""] withFinishedUnselectedImage:[UIImage imageNamed:#""]];
[[[[self.tabBarController tabBar] items] objectAtIndex:0] setTitle:#"1"];
[[[[self.tabBarController tabBar] items] objectAtIndex:0] setImage:[UIImage imageNamed:#"tab2.png"]];
[[[[self.tabBarController tabBar] items] objectAtIndex:1] setTitle:#"2"];
[[[[self.tabBarController tabBar] items] objectAtIndex:1] setImage:[UIImage imageNamed:#"tab11.png"]];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setTitle:#"3"];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setImage:[UIImage imageNamed:#"tab5.png"]];
[self.tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:#"trans.png"]];
UIImage* tabBarBackground = [UIImage imageNamed:#""];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"trans.png"]];
Problem2. If I want to
set default view is SecondViewController by Same Tab Bar(FirstView Tab
: Left ,and SecondView Tab : Right). How to
resolve it.
Solution:
[self.tabBarController setSelectedIndex:1];
Problem 1:
try to set a title for the view controllers.
Problem 2:
You just set selected index of the tabBarController to 1, that should do it.
Problem1
You haven't set tabbarItem for each navigation controller, I think you should set up a UITabBarItem with title first, then assign it to tabbarItem property to navigation controller.
Problem2
Just as #Kumar says.
I want to Push my viewController on PresentViewController
When i click on button initially i am loading PresentViewController, here is my code.
- (IBAction)JoinClicked:(id)sender{
JoinWithViewController *detail_view_controller = [[JoinWithViewController alloc] initWithNibName:#"JoinWithViewController" bundle:nil];
[self.view.window.rootViewController presentViewController:detail_view_controller
animated:YES
completion:NULL];
}
this works fine, but when i click on button which is there on PresentViewController on click i want to Push my view, but in does not pushes.
Please help, Thanks in advance.
JoinWithViewController *detail_view_controller = [[JoinWithViewController alloc] initWithNibName:#"JoinWithViewController" bundle:nil];
[self.navigationController pushViewController:detail_view_controller animated:YES];
Try like this to push viewController. If you use TabBar do like this in AppDelegate. If you create TabBar Drag and drop means leave that. Create TabBar Programatically like below.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UITabBarController *tabController = [[UITabBarController alloc]init];
self.viewController = [[CalculatorViewController alloc] initWithNibName:#"CalculatorViewController" bundle:nil];
UITabBarItem *tab_item1 = [[UITabBarItem alloc]init];
//tab_item1.title = #"Calculator";
tab_item1.image = [UIImage imageNamed:#"Calculator.png"];
[self.viewController setTabBarItem:tab_item1];
UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:self.viewController];
ShowPhoneViewController *phone = [[ShowPhoneViewController alloc]init];
UITabBarItem *tab_item2 = [[UITabBarItem alloc]init];
tab_item2.title = #"Latest Mobiles";
tab_item2.image = [UIImage imageNamed:#"Mobile.png"];
[phone setTabBarItem:tab_item2];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:phone];
CurrencyConvertorViewController *currency = [[CurrencyConvertorViewController alloc]init];
UITabBarItem *tab_item3 = [[UITabBarItem alloc]init];
tab_item3.title = #"Units Convertor";
tab_item3.image = [UIImage imageNamed:#"Dollar.png"];
[currency setTabBarItem:tab_item3];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:currency];
SettingsPageViewController *setting = [[SettingsPageViewController alloc]init];
UITabBarItem *tab_item4 = [[UITabBarItem alloc]init];
tab_item4.title = #"Settings";
tab_item4.image = [UIImage imageNamed:#"Settings.png"];
[setting setTabBarItem:tab_item4];
UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:setting];
tabController.viewControllers = [NSArray arrayWithObjects:nav1, nav2, nav3, nav4, nil];
self.window.rootViewController = tabController;
[self.window makeKeyAndVisible];
return YES;
}
I hope you got now
pushviewcontroller is the feature of UINavigationController where you can push one viewcontroller on another vierw controller. Here you are using a single viewcontroller as a rootviewcontroller so either you must change your rootviewcontroller to UINavigationcontroller or you can use "addSubview method" to add new viewController on the current viewcontroller.
but the better option is to add uinavigationcontroller as a rootviewcontroller.
AppDelegate.m
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FirstViewController *first = [[FirstViewController alloc]
initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navController =[[UINavigationController alloc]
initWithRootViewController:first];
[self.window setRootViewController:navController];
}
Now when as you want to switch from FirstViewController to SecondViewController on button clicked than you have to use pushviewcontroller
FirstViewController.h
-(void) nextBtnPressed {
SecondViewController *second = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:second animated:TRUE];
}