I am using xib instead of storyboard.
AppDelegate.m
ViewController1 *ViewController1 = [[ViewController1 alloc] init];
ViewController1.title = NSLocalizedString(#"1", #"1");
ViewController2 *ViewController2 = [[ViewController2 alloc] init];
ViewController2.title = NSLocalizedString(#"2", #"2");
BannerViewController *_bannerViewController1;
_bannerViewController1 = [[BannerViewController alloc] initWithContentViewController:ViewController1];
BannerViewController *_bannerViewController2;
_bannerViewController2 = [[BannerViewController alloc] initWithContentViewController:ViewController2];
_tabBarController = [[UITabBarController alloc] init];
_tabBarController.viewControllers = #[_bannerViewController1,_bannerViewController2];
self.window.rootViewController = _tabBarController;
[self.window makeKeyAndVisible];
ViewController1.m
#import "ViewController1.h"
#import "ViewController2.h"
ViewController2 *cvc = [[ViewController2 alloc] initWithNibName:nil bundle:nil];
cvc.strReceiveValue= "Testing";
ViewController2.h
#property (strong, retain) NSString *strReceiveValue;
ViewController2.m
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"strMortgageAmount: %#", strMortgageAmount);
Any idea why I failed to get the value? I was thinking this could be related to initWithContentViewController.
**
See update below
**
If I understand the question correctly, you are creating a tab bar controller (in AppDelegate?) and wish to pass information to the view controllers associated with the tab bar controller?
If so, take a look at this:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
[tabBarController setDelegate:self];
UINavigationController *dashTVCnav = [[tabBarController viewControllers] objectAtIndex:0];
Then:
DashTVC *dashTVC = [[dashTVCnav viewControllers] objectAtIndex:0];
dashTVC.managedObjectContext = self.managedObjectContext;
dashTVC.uuid=uuid;
Then in DashTVC.h:
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (strong, nonatomic) NSString *uuid;
With this, you pass the managedObjectContect and the uuid to the dashTVC controller.
I hope I understood what you are asking...
+++++++++++++++ UPDATE +++++++++++++++++++++
The original poster was kind enough to give me his sample code. I will post parts of it here with my fix for those who find this through searches in the future
There were several problems, but in short, nothing was being passed to vc2:
1st, nothing was being passed from the appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect bounds = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame:bounds];
self.window.backgroundColor = [UIColor whiteColor];
ViewController1 *vc1 = [[ViewController1 alloc] init];
vc1.title = NSLocalizedString(#"VC1", #"VC1");
ViewController2 *vc2 = [[ViewController2 alloc] init];
vc2.title = NSLocalizedString(#"VC2", #"VC2");
//--- I added this as a test
vc2.strReceiveValue=#"foo";
// and now foo shows up in the debug console when vc2 is fixed
_tabBarController = [[UITabBarController alloc] init];
_tabBarController.viewControllers = #[
[[BannerViewController alloc] initWithContentViewController:vc1],
[[BannerViewController alloc] initWithContentViewController:vc2],
];
self.window.rootViewController = _tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Next, it was necessary to modify viewWillAppear in viewController2.m
- (void)viewWillAppear:(BOOL)animated {
// line below missing
[super viewWillAppear:animated];
NSLog(#"output: %#", strReceiveValue);
}
Hope this helps, and enjoy developing for IOS!
Related
i just created tab bar controller with side menu i got output with black screen but i dont know how to assign two view controllers in app delegate please tell me how to make it work.i need that specific code to make it work.
//AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
{
UINavigationController *navigationController;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong,nonatomic)UITabBarController *tabBarController;
//AppDelegate.m
#interface AppDelegate ()<SWRevealViewControllerDelegate>
#end
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
self.tabBarController =[[UITabBarController alloc]init];
//Initialize View controller and speciality
UIViewController *viewcontroller1=[[HomeView alloc]init];
UIViewController *viewcontroller2=[[Speciality alloc]init];
UIViewController *viewcontroller3=[[Activity alloc]init];
UIViewController *viewcontroller4 =[[Notification alloc]init];
UIViewController *viewcontroller5 =[[Profile alloc]init];
self.tabBarController.viewControllers=[NSArray arrayWithObjects:viewcontroller1,viewcontroller2,viewcontroller3,viewcontroller4,viewcontroller5, nil];
self.tabBarController.tabBar.barTintColor = [UIColor colorWithRed:0.376 green:0.729 blue:0.318 alpha:1.000];
self.window.backgroundColor = [UIColor whiteColor];
HomeView *frontViewController = [[HomeView alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
mainRevealController.delegate = self;
self.viewController = mainRevealController;
self.window.backgroundColor= [UIColor whiteColor];
self.window.rootViewController =self.tabBarController;
self.window.rootViewController=self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Arun, I'm going to hook you up big time, if you change your library to JASidpanels, this is going to be easier for you and I already just made the solution. Download JASidePanels, use this library instead, it does the same thing as the one you have, but it's better, download here:
https://github.com/gotosleep/JASidePanels
and if you don't want to worry about doing this yourself, here's the GISTs of the App delegate files:
https://gist.github.com/anonymous/e85536b17296287ec34f
https://gist.github.com/anonymous/93b620135418ddc8f1ed
Start the demo project, and then all you need to do is change the AppDelegate.h and AppDelegate.m files and you have exactly what you want, and more:
Here's the new AppDelegate.m:
#import "JAAppDelegate.h"
#import "JASidePanelController.h"
#import "JACenterViewController.h"
#import "JALeftViewController.h"
#import "JARightViewController.h"
#implementation JAAppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[JASidePanelController alloc] init];
self.viewController.shouldDelegateAutorotateToVisiblePanel = NO;
self.viewController.leftPanel = [[JALeftViewController alloc] init];
UIViewController *viewcontroller1=[[UIViewController alloc]init];
UIViewController *viewcontroller2=[[UIViewController alloc]init];
UIViewController *viewcontroller3=[[UIViewController alloc]init];
UIViewController *viewcontroller4 =[[UIViewController alloc]init];
UIViewController *viewcontroller5 =[[UIViewController alloc]init];
viewcontroller1.view.backgroundColor = [UIColor redColor];
viewcontroller2.view.backgroundColor = [UIColor blueColor];
viewcontroller3.view.backgroundColor = [UIColor yellowColor];
viewcontroller4.view.backgroundColor = [UIColor greenColor];
viewcontroller5.view.backgroundColor = [UIColor purpleColor];
UINavigationController *navcontroller1=[[UINavigationController alloc] initWithRootViewController:viewcontroller1];
UINavigationController *navcontroller2=[[UINavigationController alloc] initWithRootViewController:viewcontroller2];
UINavigationController *navcontroller3=[[UINavigationController alloc] initWithRootViewController:viewcontroller3];
UINavigationController *navcontroller4 =[[UINavigationController alloc] initWithRootViewController:viewcontroller4];
UINavigationController *navcontroller5 =[[UINavigationController alloc] initWithRootViewController:viewcontroller5];
viewcontroller1.title = #"one";
viewcontroller2.title = #"two";
viewcontroller3.title = #"three";
viewcontroller4.title = #"four";
viewcontroller5.title = #"five";
navcontroller1.tabBarItem.image = [UIImage imageNamed:#"cam"];
navcontroller2.tabBarItem.image = [UIImage imageNamed:#"cam"];
navcontroller3.tabBarItem.image = [UIImage imageNamed:#"cam"];
navcontroller4.tabBarItem.image = [UIImage imageNamed:#"cam"];
navcontroller5.tabBarItem.image = [UIImage imageNamed:#"cam"];
self.this = [[UITabBarController alloc] init];
self.this.viewControllers=[NSArray arrayWithObjects:navcontroller1,navcontroller2,navcontroller3,navcontroller4,navcontroller5, nil];
//self.this.tabBar.barTintColor = [UIColor orangeColor];
self.this.tabBar.backgroundColor = [UIColor orangeColor];
self.viewController.centerPanel = _this;
self.viewController.rightPanel = [[JARightViewController alloc] init];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
#end
Here's the new AppDelegate.h:
#import <UIKit/UIKit.h>
#class JASidePanelController;
#interface JAAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) JASidePanelController *viewController;
#property (strong, nonatomic) UITabBarController *this;
#end
This works, here's proof:
this all works, you can customize the sides a very easily, and this doesn't break nearly as much as the other sidepanel controllers. Ask questions if you have them!
ALSO, make sure you add your own custom images to the tabs. This works without a hitch and you will now have yourself a very very robust navigation system that works prostyle. In fact, this JASidePanels is probably one of THE most popular amongst production apps because it doesn't cut corners and doesn't break the guy who made it is a very very good at what he does. Also, this took me about 10 minutes to make and this is just the start, you can do a lot more with this little side panel library than the others. This library literally requires 4 files, that's it, no mess, no fuss, no stupidity.
I don't have privilege to comment on your code.
So I am posting it in your answer.
Why did you set the window's root view controller twice in your code, it will always be one.
Either this,
self.window.rootViewController =self.tabBarController;
Or this,
self.window.rootViewController=self.viewController;
I have successfully created and implemented a custom UITabBarController with a custom UITabBar following this tutorial. It works fine until I have to hide it.
I'm not using Storyboards or IB and I have to get a reference to my existing UITabBarController which is on screen to hide a custom UIView in it. I'm trying to do it this way but it's only creating a new instance of that UITabBarController and not pointing me to the original instance I see onscreen:
SGTabBarController *tabBarController = [[SGTabBarController alloc] init];
[tabBarController hideCustomTabBar];
SGTabBarController.h
#interface SGTabBarController : UITabBarController
#property (nonatomic) int tabBarHeight;
-(void)hideCustomTabBar;
-(void)showCustomTabBar;
#end
SGTabBarController.m
-(void)hideCustomTabBar{
customTabBarView.hidden = YES;
NSLog(#"HIDDEN!!!");
}
-(void)showCustomTabBar{
customTabBarView.hidden = NO;
}
Any ideas on how to get to it? Thanks in advance!
How I am able to access a custom UITabBarController anywhere in the app.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Set up the Dashboard
//
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[_window makeKeyAndVisible];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSMutableArray *tabBarItems = [#[] mutableCopy];
// Repeat this for any amount of ViewControllers
UITableViewController *tableViewController = [UITableViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *navController = [UINavigationController alloc] initWithRootViewController:tableViewController];
[tabBarItems addObject:navController];
tabBarController.viewControllers = tabBarItems;
self.window.rootViewController = tabBarController;
return YES;
}
As I mentioned in the title, I want to add Navigation Controller to my application which already has a Tab Controller. So trying to do the staff something like on this page. Anyway, something is wrong. UINavigationController is looking a blank page, even if has a view and some libraries.
Let me begin from the stracht:
In my AppDelegate, I'm setting tab bar controllers like this:
#interface MYAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UITabBarController *tabBarController;
#end
And here is .m file:
#implementation MYAppDelegate
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
application.applicationSupportsShakeToEdit = YES;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *viewController1 = [[[MYMainViewController alloc] init] initWithNibName: #"MYMainViewController" bundle:nil];
UIViewController *viewController2 = [[[MYPageViewController alloc] init] initWithNibName:#"MYPageViewController" bundle:nil];
UIViewController *viewController3 = [[[MYSearchViewController alloc] init] initWithNibName:#"MYSearchViewController" bundle:nil];
UIViewController *viewController4 = [[[MYPersonViewController alloc] init] initWithNibName:#"MYPersonViewController" bundle:nil];
// Initialize tabBarController and add ViewControllers
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;
}
Then, here is MYMainViewController implementaion which is a UINavigationController:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"%#", [self navigationController]); // Logging null
}
My .xib file has a UINavigationController and and there is a view in it. Althought it, when I worked the app, there is blank page and untitled navigation bar. What am I doing wrong?
If I could see the content of my view, I want to navigate between two view controllers by using back button.
Any help or approach would be great for me.
Try to remove navigation controller from xib, so it only have view controller, then initialize navigation controller programatically:
UIViewController *tmpViewController1 = [[[YourViewController alloc] init] initWithNibName:#"YourViewController" bundle:nil];
UINavigationController *viewController1 = [[UINavigationController alloc] initWithRootViewController:tmpViewController1];
I've got an app where I am pushing a modal view controller. It is working fine, but I am concerned I haven't coded it in the most correct fashion. I have instanstiated two navigation controllers, which seems a bit dodgy to me.
Basically I've created a tab bar controller with 3 tabs, then made one of those tabs / view controllers the root. Later I am (using some home-grown markup on core text) popping a view controller when the user touches a particular word in a paragraph. The pushed view controller has a back button which works fine and the app seems to be OK.
Like I said it all works, but it seems I am coding in circles here. Is this correct?
AppDelegate.h
#import <Foundation/Foundation.h>
#interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
{
UIWindow *window;
UITabBarController *tabBarController;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UITabBarController *tabBarController;
#end
From AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
ViewController2 *viewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
ViewController3 *viewController3 = [[ViewController3 alloc] initWithNibName:#"ViewController3" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav, viewController2, viewController3, nil];
self.tabBarController.delegate = self;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
ViewController3.h
#import <UIKit/UIKit.h>
#import "JSCoreTextView.h"
#import "PopupViewController.h"
#class JSTwitterCoreTextView;
#interface ReadingViewController : UIViewController <JSCoreTextViewDelegate>
{
JSTwitterCoreTextView *_textView;
UIScrollView *_scrollView;
}
#end
From ViewController3.m
Here I am instantiating another navigation controller. Is this a good idea?
- (void)textView:(JSCoreTextView *)textView linkTapped:(AHMarkedHyperlink *)link
{
PopupViewController *popupVC = [[PopupViewController alloc] initWithNibName:#"PopupViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:popupVC];
[nav setModalPresentationStyle:UIModalPresentationFullScreen];
[nav setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:nav animated:YES];
}
From PopupViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(done:)]];
}
- (void)done:(id)sender
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
It appears the answer is "yes". I was under the impression there is a single Navigation Controller for the app, but it's more like one per tab, depending on if there are going to be further pushes from that tab.
Im implementing a combined tab bar and navigation programatically, using the apple documentation,
it is not working when calling the initWithFrame,[goes black screen]; but if left as below code it works for showing main screen, with out the tab bar, and when using the tab bar goes black screen
here the code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions {
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
StartViewController *startViewControllerView = [[[StartViewController alloc] init] autorelease]; //ojo recomendado por apple!!!
VideosViewController* VideosViewController_ = [[[VideosViewController alloc] init] autorelease];
PhotosViewController* PhotosViewController_ = [[[PhotosViewController alloc] init] autorelease];
SocialViewController* SocialViewController_ = [[[SocialViewController alloc] init] autorelease];
self.pagesNavigation = [[[UINavigationController alloc] initWithRootViewController:startViewControllerView] autorelease];
self.pagesNavigation.navigationBarHidden = NO;
NSArray* controllers = [NSArray arrayWithObjects:VideosViewController_, PhotosViewController_, SocialViewController_, startViewControllerView, nil];
self.tabBarController.viewControllers = controllers;
[self.window addSubview:startViewControllerView.view];
//self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[self.window makeKeyAndVisible];
return YES;
}
so if left as shown above, it works but if I comment the addSubview and uncomment the initWithFrame, it doesnt work,,
//[self.window addSubview:startViewControllerView.view];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
So, what Im i missing?,
what would be the right way to call the initWithFrame?
thanks a lot!
Why are all your viewcontrollers auto released? You should probably be retaining them and releasing them only when you're done with them.
As for your structure, I've found that building a single navigation controller for each tab in the tabbarcontroller, adding those to the controller, then adding the tabbarcontroller to the window works like this...
AppDelegate.h
property (nonatomic, retain) UITabBarController *tabBarController;
property (nonatomic, retain) UINavigationController *firstNavController;
property (nonatomic, retain) UINavigationController *secondNavController;
property (nonatomic, retain) FirstViewController *firstViewController;
property (nonatomic, retain) SecondViewController *secondViewController;
AppDelegate.m
firstViewController = [[FirstViewController alloc] someInitMethod:someArg];
firstNavController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
secondViewController = [[SecondViewController alloc] someInitMethod:someArg];
secondNavController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
tabBarController = [[UITabbarController alloc] init];
NSArray *tabs = [NSArray arrayWithObjects:firstNavController, secondNavController, nil];
[tabBarController setViewControllers:tabs animated:NO];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];