iOS - Second View is not displaying over tab view - ios

I have a tabBarView and a GWDNativeViewController. GWDNativeViewController is a main menu. I am trying to get that view to display first and overtop the tabBarView. I have this same code set within an IBAction and it works. When I put it into the Loaddidfinishwithoptions it doesn't work.
#implementation GWDNativeAppDelegate
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
#synthesize secondView = _secondView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[GWDNativeFirstViewController alloc] initWithNibName:#"GWDNativeFirstViewController" bundle:nil];
UIViewController *viewController2 = [[GWDNativeSecondViewController alloc] initWithNibName:#"GWDNativeSecondViewController" bundle:nil];
UIViewController *viewController3 = [[GWDNativeThirdViewController alloc] initWithNibName:#"GWDNativeThirdViewController" bundle:nil];
UIViewController *viewController4 = [[GWDNativeFourthViewController alloc] initWithNibName:#"GWDNativeFourthViewController" bundle:nil];
UIViewController *viewController5 = [[GWDNativeFifthViewController alloc] initWithNibName:#"GWDNativeFifthViewController" bundle:nil];
//tabBarController Stuff
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, viewController4, viewController5, nil];
self.window.rootViewController = self.tabBarController;
//Specify W`enter code here`hich tab to display (Number need to be set based on button selected)
//self.tabBarController.selectedIndex = 2;
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.tabBarController presentModalViewController:secondView animated:YES];
[self.window makeKeyAndVisible];
return YES;
}
Here is the the code in the GWDNativeFirstViewController.m
The code in the IBAction works.. Code in the viewDidLoad doesn't.?
-(IBAction)pressedButton {
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:secondView animated:YES];
//[secondView release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"First", #"First");
self.tabBarItem.image = [UIImage imageNamed:#"first"];
}
return self;
}
- (void)viewDidLoad
{
[contentWebView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://m.web.org/?iapp=1#tab1"]]];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.tabBarController presentModalViewController:secondView animated:YES];
}

I'm not sure why it doesn't work in viewDidLoad, but it can work in applicationDidFinishLaunchingWithOptions: if you put your [self.window makeKeyAndVisible] line before you define secondView.
[self.window makeKeyAndVisible];
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.tabBarController presentModalViewController:secondView animated:YES];
If you want it to appear immediately, change the animated option to NO, and delete the line defining the modalTransitionStyle.

The tabBarController's view hasn't loaded here yet. You could put that code into the viewDidLoad in one of your 5 view controllers.

Related

MMDrawerController with UITabBarController showing a black gap on top of the center view

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

how to use to uiviewcontroller without xib file

I have one question about transition.
we use this code when we want to transition to next UIViewController:
UIViewController* secondViewController = [[UIViewController alloc] initWithNibName:#"SecondViewControllerName" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:secondViewController animated:YES];
or
when we want to add NavigationController using this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
or
when we want add UIViewController to another UIViewController using this code:
UIViewController* secondViewController = [[UIViewController alloc] initWithNibName:#"SecondViewControllerName" bundle:[NSBundle mainBundle]];
[self.view addSubview:secondViewController.view];
now I want to know that if I don't use xib file in my project what to do???
You could use ViewController from Storyboard
ViewController *controller = (ViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"yourIdentifier"];
or create normally
// make Layout in viewController - viewDidLoad
ViewController *viewController = [[ViewController alloc] init];
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 64.0, 320.0, 24.0)];
label.textColor = [UIColor blackColor];
label.text = #"My text";
[self.view addSubview:label];
UIButton *button = ...;
[self.view addSubview:button];
}

How to pass tab index value from UITabBarController to UIViewController

I've created tab bar items in the first controller
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
StageViewController *stageViewRegular = [[StageViewController alloc] init];
[stageViewRegular setTitle:#"Regular"];
stageViewRegular.tabBarItem.tag = 1;
StageViewController *stageViewAdvanced = [[StageViewController alloc] init];
[stageViewAdvanced setTitle:#"Advanced"];
stageViewAdvanced.tabBarItem.tag = 2;
NSArray* controllersArray = [NSArray arrayWithObjects:stageViewRegular, stageViewAdvanced, nil];
tabBarController.viewControllers = controllersArray;
tabBarController.selectedIndex = 0;
[self.view addSubview:tabBarController.view];
and I want to pass tabBarItem.tag to the controllers as mentioned above
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
chooseLevel = viewController.tabBarItem.tag;
}
but in the second controller
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(#"%d", chooseLevel);
}
the chooseLevel always record old value. If I press the first tab and then press the second tab, the value in chooseLevel is 1 rather than 2.
Does anyone know how to solve it?
Do the following steps :
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
viewController1.tabBarItem.tag = 0;
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
viewController2.tabBarItem.tag = 1;
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = #[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
FirstViewController.m
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(#"%d",self.tabBarItem.tag);
}
SecondViewController.m
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(#"%d",self.tabBarItem.tag);
}
OutPut :
2013-06-27 18:13:03.913 sampletabs[3981:c07] 0
2013-06-27 18:13:05.282 sampletabs[3981:c07] 1
Thanks, Do let me know it works for you ?
Why don't you use selectedIndex property of the UITabBarController. I think it has the value you are trying to use.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html
You can use selected index property like this:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
chooseLevel = tabBarController.selected index;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
int selectedtag=self.tabBarController.selectedIndex;
NSLog(#"%d", selectedtag);
}
Do this :
RootViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
FirstViewController *stageViewRegular = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[stageViewRegular setTitle:#"First"];
stageViewRegular.tabBarItem.tag = 1;
SecondViewController *stageViewAdvanced = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[stageViewAdvanced setTitle:#"Second"];
stageViewAdvanced.tabBarItem.tag = 2;
NSArray* controllersArray = [NSArray arrayWithObjects:stageViewRegular, stageViewAdvanced, nil];
tabBarController.viewControllers = controllersArray;
tabBarController.selectedIndex = 0;
[self.view addSubview:tabBarController.view];
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(#"%d",viewController.tabBarItem.tag);
}
FirstViewController
-(void)viewWillAppear:(BOOL)animated
{
NSLog(#"viewWillAppear %d",self.tabBarItem.tag);
}
SecondViewController
-(void)viewWillAppear:(BOOL)animated
{
NSLog(#"viewWillAppear %d",self.tabBarItem.tag);
}
If you need Global value of selected tab Index, then create readWrite property in AppDelegate and set the value in either RootViewController or First/SecondViewController.you will get global value of ChooseLevel.
Thanks,

iOS selected tab

I'm trying to determine which tab has been selected by the user. I melded this together from a couple of tutorials on iOS tab bars. In my appDelegate I have this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//We need to implement the view controllers within the tab controller and make the tab controller the root controller of our app - note we are only using view 1-3 at first.
FirstViewController *fistView = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
ThirdViewController *thirdView = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
FourthViewController *fourthView = [[FourthViewController alloc] initWithNibName:#"FourthViewController" bundle:nil];
NSArray *viewControllersArray = [[NSArray alloc] initWithObjects:fistView, secondView, thirdView, fourthView, nil];
self.tabController = [[UITabBarController alloc] init];
[self.tabController setViewControllers:viewControllersArray animated:YES];
self.window.rootViewController = self.tabController;
//end custom code
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Is viewControllerArray the delegate for my tabController?
When I place this code on the page nothing happens:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 0) {
NSLog(#"ok");
}
}
In this case, your app delegate should be the delegate for the tabBarController.
You can simply add self.tabController.delegate = self and make sure that your AppDelegate conforms to the UITabBarControllerDelegate protocol.
I also suggest placing a log outside the if in your delegate method, to confirm that it is actually called.

TabBar application with NavigationBar

I'm building an App which is TabBar based. One of the tabs will display a TableView and I would like to have a NavigationBar at the top.
However I need to localize everything in this app to several languages so it needs to be done in code.
I have the tab bar set up in the AppDelegate;
#implementation AppDelegate
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[HomeViewController alloc] initWithNibName:#"HomeView" bundle:nil];
UIViewController *viewController2 = [[RecycleViewController alloc] initWithNibName:#"RecycleView" bundle:nil];
UIViewController *viewController3 = [[SettingsViewController alloc] initWithNibName:#"SettingsView" bundle:nil];
UIViewController *viewController4 = [[SettingsViewController alloc] initWithNibName:#"SettingsView" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2,viewController3, viewController4, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Each of the windows has the code for the information on the tab.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Home", #"Home");
self.tabBarItem.image = [UIImage imageNamed:#"home"];
}
return self;
}
So far so good, but the RecyclingView (TableView) need a navigation bar where I can set the title using NSLocalizedString.
I would be really grateful for some help.
you need to add a UINavigationController at the second index of your tabBarController's viewControllers instead of adding a view controller -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[HomeViewController alloc] initWithNibName:#"HomeView" bundle:nil];
UIViewController *viewController2 = [[RecycleViewController alloc] initWithNibName:#"RecycleView" bundle:nil];
UIViewController *viewController3 = [[SettingsViewController alloc] initWithNibName:#"SettingsView" bundle:nil];
UIViewController *viewController4 = [[SettingsViewController alloc] initWithNibName:#"SettingsView" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, navController,viewController3, viewController4, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
to set the title at navigation bar you can write this in your RecycleViewController's viewDidLoad
[self.navigationItem setTitle:#"title"];
You can localize the xibs with Interface Builder so you shouldn't have to do it programatically.
http://www.icanlocalize.com/site/tutorials/iphone-applications-localization-guide/
The answer for xcode 4.3.2 is:
alloc the UINavigationBar in RecycleViewController.m as this:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self];
self.view addSubview:navController.view;
But there is a bug, that navigation bar will display a blank at the top, about 10pixels.
So here is another solution:
use xib builder, add a navigation bar from libary in the top of view RecycleViewController.xib, press ctrl+"navigation Item", point to RecycleViewController.h to insert an IBOutlet,then you will have a good looking navigation bar to show.

Resources