I have problems with displaying view as subview on navigationController.view on Ipad . I need to display view with transparent background on my viewController(with navBar),but while i change orientation my navBar become visible on foreGround of my view;
I created simple view based app.
Here code i added to project:
AppDelegate.h:
UINavigationController *_navController;
#property (strong, nonatomic) UINavigationController *navController;
AppDelegate.m:
_navController = [[[UINavigationController alloc] initWithRootViewController:self.viewController] autorelease];
self.window.rootViewController = _navController;
ViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *view = [[[UIView alloc] initWithFrame:self.view.frame] autorelease];
[view setBackgroundColor:[UIColor redColor]];
[self.navigationController.view addSubview:view];
}
try pushing view to Navigation Controller
YourAppDelegate *del = (YourAppDelegate *)[UIApplication sharedApplication].delegate;
[del.navigationController pushViewController:nextViewController animated:YES];
or
UINavigationController* navigation = [[UINavigationController alloc] init];
iVkViewController *overviewViewController = [[iVkViewController alloc] init];
overviewViewController.title = #"First";
[navigation pushViewController:overviewViewController animated:NO];
Related
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!
I am trying to achieve to two things right now:
1) Have a tabbar controller inside a navigation controller
2) Depending on the selected tab, some Navigation buttons change, and I want to push some views into the navigation controller, while staying inside the currently selected tab.
Right now, I push the Navigation Controller like this:
GenericAddViewController *vc = [[GenericAddViewController alloc]init];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:vc];
navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:navigationController animated:YES completion: nil];
Then, inside the GenericAddViewController.m I have:
#interface GenericAddViewController ()
#property (strong, nonatomic) UITextField *titleTextField;
#property (nonatomic, strong) UITabBarController *tabBarController;
#end
#implementation GenericAddViewController
-(void)loadView
{
[super loadView];
TabOneController *tabOneController = [[TabOneController alloc]init];
tabOneController.title = #"Steps";
TabTwoController *tabTwoController = [[TabTwoController alloc]init];
tabTwoController.title = #"More Information";
NSArray *controllers = [NSArray arrayWithObjects:tabOneController, tabTwoController, nil];
self.tabBarController = [[UITabBarController alloc]init];
self.tabBarController.view.frame = CGRectMake(0, 0, 768, 1004);
self.tabBarController.viewControllers = controllers;
self.tabBarController.delegate = self;
}
This works. The only problem is for example in the TabOneController, I can't do any of these:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(isDone:)];
[self.navigationItem setRightBarButtonItem:doneButton animated:NO];
Nor
[self.navigationController push...]
Shouldn't be that complicated no?!
Thanks a lot!
I'm using MGSplitViewController for my iPad application.
I have added a viewController to it's detailViewController this way:
my2ndVC *vc = [[my2ndVC alloc] init];
splitViewController.detailViewController = vc;
It works perfect, now when I'm adding a navigationBar to my2ndVC
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 448, 44)];
[self.view addSubview:navBar];
The bar isn't showing up.
I know I can add my2ndVC like this:
my2ndVC *vc = [[my2ndVC alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
splitViewController.detailViewController = navController;
But then I also have a condition in one of my view controller:
if ([splitViewController.detailViewController isKindOfClass:[my2ndVC class]])
What would be the suggested workaround?
Cant you change your condition as follows:
if ([splitViewController.detailViewController.topViewController isKindOfClass:[my2ndVC class]])
To Make it simpler :
UINavigationController *aNavigationController = (UINavigationController *)splitViewController.detailViewController;
if ([aNavigationController.topViewController isKindOfClass:[my2ndVC class]]) {
// TRUE...
}
There is a UITabBarController
- (void)getToMCGatherViewController
{
mCGatherViewController = [[MCGatherViewController alloc] initWithNibName:#"MCGatherViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mCGatherViewController];
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
}
In the .h file:
#interface MCGatherViewController : UITabBarController
In the .m file . I want to change the color of the view's navigationBar
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.tintColor=[UIColor greenColor];
}
and it does not work at all.
Help me with this problem , thank you in advance!
just add
[navigationController.navigationBar setTintColor:[UIColor greenColor]];
after
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mCGatherViewController];
in your getToMCGatherViewController method.
Just edit your code like this and try I think this will work for you.
mCGatherViewController = [[MCGatherViewController alloc] initWithNibName:#"MCGatherViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mCGatherViewController];
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.view addSubview:nav.view];
Then you change your Navigation Bar tint color.
I want to create a UIView for a UITabBarController
Here is my code for the .h file :
#interface TE : UIViewController <UITabBarControllerDelegate>{
UITabBarController *tabBarController;
}
#property (nonatomic,retain) UITabBarController *tabBarController;
#end
The viewDidLoad method:
UIViewController *testVC = [[T1 alloc] init];
UIViewController *otherVC = [[T2 alloc] init];
NSMutableArray *topLevelControllers = [[NSMutableArray alloc] init];
[topLevelControllers addObject: testVC];
[topLevelControllers addObject: otherVC];
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
[tabBarController setViewControllers:topLevelControllers animated:NO];
tabBarController.selectedIndex = 0;
self.view = tabBarController.view;
This creates the tab bar controller, but when I click on a tab bar item, I get an error:
Thread1:Program receive signal: SIGABRT
Edit: I solved the problem by downloading and modifying the version of http://www.iphonedevcentral.com/create-uitabbarcontroller/
You say above that you don't want to create the tabBarController in the appDelegate. Why not? Where else would you create it? The tabBarController has to be the root view controller and cannot be a child of any other view controller.
Btw, make sure you implement:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSUInteger tabIndex = [tabBarController.viewControllers indexOfObject:viewController];
if (viewController == [tabBarController.viewControllers objectAtIndex:tabIndex] ) {
return YES;
}
return NO;
}
Subclass UITabBarController
Override the - (void) loadView method and include the following code
MyCustomViewControllerOne* ctrl1 = [[[MyCustomViewControllerOne alloc] initWithNibName#"MyViewControllerOne" bundle: nil] autorelease];
UIViewController* ctrl2 = [[[UIViewController alloc] init] autorelease];
MyCustomControllerTwo* ctrl3 = [[[UIViewController alloc] initWithObject: myObj] autorelease];
ctrl1.title = #"First tab";
ctrl2.title = #"Second tab";
ctrl3.title = #"Third tab";
ctrl1.tabBarItem.image = [UIImage imageNamed:#"tab_image1.png"];
ctrl2.tabBarItem.image = [UIImage imageNamed:#"tab_image2.png"];
ctrl3.tabBarItem.image = [UIImage imageNamed:#"tab_image3.png"];
[self setViewControllers: #[ctrl1, ctrl2, ctrl3]];
That's pretty much it.
Change self.view = tabBarController.view; to
[self.view addSubview:tabBarController.view]; And it works correctly
Trying changing
self.view = tabBarController.view;
to
[self.view addSubview:tabBarController.view];
See if that helps.
Also try placing this in your -(void)loadView method
- (void)loadView {
UIView *mv = [[UIView alloc] initWithFrame:CGRectMake(0.0, 100.0, 320.0, 480.0)];
self.view = mv;
[mv release];
}
The reason you probably are experiencing a black screen is because you haven't initialized your UIView properly.
#Mehdi, just make your TE a UITabBarController instead of a UIViewController which then has a TabBarController in it. Makes it all the more easy to manage your TabBarController. To respond to some others who have indicated that you can have only one TabBarController as the window's rootViewController. That is not the case. A UITabBarController can be instantiated in multiple places where you need a second level menu navigation. Have a TabBar within a TabBar would not make sense, but having a left Navigation Menu and then having a TabBar on each menu item would make sense.