How to create a splitviewcontroller fron a view? - ios

I am new in Xcode. I have viewcontroller with a navigation tabbar with items. I need that one on this menu item calls a splitview with master/detail. Is possible create a splitview in a view and not necessary in appdelegate? In appdelegate I have create one splitview, but if an option on menu.
Is possible to put this code in a the view that i clic the barbutton? The code used is:
detallContactesAccions = [[[CrmContactesAccionesDetallVC alloc] init] initWithNibName:#"CrmContactesAccionesDetallVC" bundle:nil];
masterContactesAccions = [[CrmContactesAccionesMasterTVC alloc] initWithStyle:UITableViewStylePlain];
masterContactesAccions.detailViewController = detallContactesAccions;
masterContactesAccions.navigationItem.title = NSLocalizedString(#"Acciones",#"");
UINavigationController *navMaster = [[[UINavigationController alloc] initWithRootViewController:masterContactesAccions] autorelease];
UINavigationController *navDetail = [[[UINavigationController alloc] initWithRootViewController:detallContactesAccions] autorelease];
splitViewController = [[UISplitViewController alloc] init];
//splitViewController.tabBarItem = controller.tabBarItem;
[splitViewController setValue:[NSNumber numberWithFloat:300.0] forKey:#"_masterColumnWidth"]; //Dóna més amplada a l'split view
splitViewController.viewControllers = [NSArray arrayWithObjects:navMaster, navDetail, nil];
splitViewController.delegate = detallContactesAccions;

Related

Present a View Controller modally when selected in Tab Bar Controller

I have this pretty standard Tab Bar Controller setup:
UIViewController *homeViewController = [[PLOTHomeViewController alloc] init];
UIViewController *upcomingViewController = [[PLOTUpcomingViewController alloc] init];
UIViewController *checkInViewController = [[PLOTCheckInViewController alloc] init];
UIViewController *watchlistViewController = [[PLOTWatchlistViewController alloc] init];
UIViewController *profileViewController = [[PLOTProfileViewController alloc] init];
PLOTNavigationController *homeNavVC = [[PLOTNavigationController alloc] initWithRootViewController:homeViewController];
PLOTNavigationController *upcomingNavVC = [[PLOTNavigationController alloc] initWithRootViewController:upcomingViewController];
PLOTNavigationController *checkInNavVC = [[PLOTNavigationController alloc] initWithRootViewController:checkInViewController];
PLOTNavigationController *watchlistNavVC = [[PLOTNavigationController alloc] initWithRootViewController:watchlistViewController];
PLOTNavigationController *profileNavVC = [[PLOTNavigationController alloc] initWithRootViewController:profileViewController];
self.tabBarController = [[PLOTTabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:homeNavVC, upcomingNavVC, checkInNavVC, watchlistNavVC, profileNavVC, nil];
However, I'm trying to work out how, when the user selects the middle tab (checkInViewController), I can present that View Controller modally (fullscreen)? I'd maybe imagine something in the viewDidAppear method in that VC, but I'm not sure if you can present yourself modally, if you're a VC? What's the best approach for this?
You can use
[self presentViewController:checkInViewController animated:YES completion:nil];
Then hide the tab and navigation bars in the destination view controller's viewWillAppear:
-(void)viewWillAppear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self.tabBarController.tabBar setHidden:YES];
}

iOS 7: different navigation items for tab bar controller

I am relative new with the iOS app development. Currently I am developing a small app with a tab bar. The problem I am facing is that I would like to have different navigation items foreach tab. I tried a lot of things, but things aren't working. I am programming in the native iOS language.
In my app I've got a AppDelegate. In my AppDelegate there is a little piece of code for setting up my mainViewController:
- (void)setupOverlordViewController
{
MainViewController *rootVC = [[MainViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = navVC;
}
I am setting up my tabs in my MainViewController:
- (void)viewDidLoad
{
UIViewController *tabView1 = [[Tab1ViewController alloc] init];
UIViewController *tabView2 = [[Tab2ViewController alloc] init];
UIViewController *tabView3 = [[Tab3ViewController alloc] init];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
[tabViewControllers addObject:tabView1];
[tabViewControllers addObject:tabView2];
[tabViewControllers addObject:tabView3];
[self setViewControllers:tabViewControllers];
tabView1.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"TabView1", nil)
image:[UIImage imageNamed:#"tabView1.png"]
tag:1];
tabView2.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"TabView2", nil)
image:[UIImage imageNamed:#"tabView2.png"]
tag:2];
tabView3.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"TabView3", nil)
image:[UIImage imageNamed:#"tabView3.png"]
tag:3];
}
Each View (tabView1, tabView2, tabView3) has there own layout, which is set in the ViewDidLoad method of the View. When I would like to add navigation buttons in the navigation bar by adding them in the ViewDidLoad method, but it seems impossible to add the buttons. The only way to add them is directly in my MainViewController, but then I can't set the navigation bar buttons different foreach tab.
The code for adding the buttons to my navigation bar is as follows:
UIBarButtonItem *btnNewRecord = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(btnNewRecord)];
NSArray *rightItems = [NSArray arrayWithObjects:btnNewRecord, nil];
[self.navigationItem setRightBarButtonItems:rightItems];
Could somebody explain me what I am doing wrong?
I have created a example for you by using xib files. I have created three View Controllers and added them to navigation controllers. Following the appdelegate code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:#"FirstView" bundle:nil];
UINavigationController *firstNavVC = [[UINavigationController alloc] initWithRootViewController: firstVC];
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondView" bundle:nil];
UINavigationController *secondNavVC = [[UINavigationController alloc] initWithRootViewController: secondVC];
ThirdViewController *thirdVC = [[ThirdViewController alloc] initWithNibName:#"ThirdView" bundle:nil];
UINavigationController *thirdNavVC = [[UINavigationController alloc] initWithRootViewController: thirdVC];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
[tabViewControllers addObject:firstNavVC];
[tabViewControllers addObject:secondNavVC];
[tabViewControllers addObject:thirdNavVC];
firstNavVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"First", nil)
image:nil
tag:1];
secondNavVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"Second", nil)
image:nil
tag:2];
thirdNavVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"Third", nil)
image:nil
tag:3];
UITabBarController *tabbarController = [[UITabBarController alloc] init];
tabbarController.viewControllers = tabViewControllers;
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = tabbarController;
[self.window makeKeyAndVisible];
return YES;
}
Following is the output :
You can download code example here
you need separate navigation controller for each tab bar view controller & then you can add UIBarButtonItem on each navigation controller.

how to add tab bar controller from the second view controller [duplicate]

This question already has an answer here:
Showing login view controller before main tab bar controller
(1 answer)
Closed 9 years ago.
Im beginner to IOS app development learning.
I have a login screen as my first view controller and i need the second view controller to be a tab bar view controller .with 4 different tabs and i have 4 different XIB's for them.
some one help me to go ahead.
Best way you can do is Present the login screen modally when the app starts from your tab bar controller first screen, add code for presenting login screen in viewWillAppear and after login dismiss the screen. You can create TabBarController in appDelegate like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController tabBarController=[[UITabBarController alloc] init];
FirstViewController *firstVC = [[UIViewController alloc] initWithNibName:#"FirstVC" bundle:nil];
UINavigationController *firstNavController = [[UINavigationController alloc] initWithRootViewController: firstVC];
SecondViewController *secondVC = [[UIViewController alloc] initWithNibName:#"secondVC" bundle:nil];
UINavigationController *secondNavController = [[UINavigationController alloc] initWithRootViewController:secondVC];
tabBarController.viewControllers = [NSArray arrayWithObjects: firstNavController, secondNavController, nil];
tabBarController.selectedIndex=0;
tabBarController.delegate = self;
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:#"Movies" image:[UIImage imageNamed:#"MoviesTAB.png"] tag:1];
[firstVC setTabBarItem:item1];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:#"Music" image:[UIImage imageNamed:#"musicTAB.png"] tag:2];
[seconfVC setTabBarItem:item2];
tabController.tabBar.translucent = NO;
tabController.tabBar.barStyle = UIBarStyleBlack;
tabBarController.tintColor = [UIColor whiteColor];
self.window.rootViewController = tabController;
return YES;
}
Best way is use storyboard and there just have one initial UIViewController and from that make segue to UITabBarViewController.
http://youtu.be/a_DCTSTv1Mw
If you want to make it through xib make a UITabBarViewController and add viewControllers to the array of object of that UITabBarViewController's object.
Sample code :
NSMutableArray *arrViewControllers = [[NSMutableArray alloc] init];
UIViewController *tabController;
UIImage *tabImage ;
NSString *tabTitle;
for (int i= 0; i < 3; i++) {
switch (i) {
case 0:
tabController = [[ViewController alloc] init];
tabImage = [UIImage imageNamed:#"icon1.png"];
tabTitle = #"Text";
break;
case 1:
tabController = [[ImageDemoViewController alloc] init];
tabImage = [UIImage imageNamed:#"icon2.png"];
tabTitle = #"Image";
break;
case 2:
tabController = [[TableDemoViewController alloc] init];
tabImage = [UIImage imageNamed:#"icon3.png"];
tabTitle = #"Table";
break;
}
// set the image and title using some properties of UIViewController
tabController.tabBarItem.image = tabImage;
tabController.tabBarItem.title = tabTitle;
//add objects to array
[arrViewControllers addObject:tabController];
[tabController release];
}
_baseController = [[UITabBarController alloc] init];
_baseController.viewControllers = arrViewControllers;
[arrViewControllers release];
go to your appDelegate
1.create a viewController for login screen.
LoginViewController *viewController1 = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
2.create a navigationController with root view your login ViewController.
UINavigationController *nVC = [[UINavigationController alloc] initWithRootViewController:viewController1];
3.make navigationController to root view of window.
self.window.rootViewController = self.nVC;
[self.window makeKeyAndVisible];
Now go to Touch-Up-Inside method of login button in LoginViewController.
1.After validation of password and userId initialise your viewControllers for tabbar and TabbarViewController.
UiViewController ...*yourViewControllers,..,
UITabBarController *YourtabBarController = [[UITabBarController alloc] init];
2.Now add these viewControllers to your tabbarController.
YourtabBarController.viewControllers = #[ YourViewController1,YourViewController2,YourViewController3,......];
3.Finally push this tabbarController to navigationControllere.
[self.navigationController pushViewController:YourtabBarController animated:NO];

Change the selected TabBar index on button click

In my app I have 4 tabs, on the first Tab : I have 4 views. on second view,there is one button which will open the third tab view. But my selected tab Index is One. How can I chage it to Third.
My code for tab bar is in APPDelegate as follow:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *nav1 = [[UINavigationController alloc] init];
UIViewController *viewController1 = [[[CarAccidentAppViewController alloc]
initWithNibName:#"CarAccidentAppViewController_iPhone" bundle:nil]
autorelease];
nav1.viewControllers = [NSArray arrayWithObjects:viewController1, nil];
//for steps tab...
UINavigationController *nav2 = [[UINavigationController alloc] init];
UIViewController *viewController2 = [[[FirstSteps alloc] initWithNibName:#"FirstSteps" bundle:nil] autorelease];
nav2.viewControllers = [NSArray arrayWithObjects:viewController2, nil];
//for profiles tab...
UINavigationController *nav3 = [[UINavigationController alloc] init];
UIViewController *viewController3 = [[[Profiles alloc] initWithNibName:#"Profiles" bundle:nil] autorelease];
nav3.viewControllers = [NSArray arrayWithObjects:viewController3, nil];
//for contact us tab...
UINavigationController *nav4 = [[UINavigationController alloc] init];
UIViewController *viewController4 = [[[ContactUs alloc] initWithNibName:#"ContactUs" bundle:nil] autorelease];
nav4.viewControllers = [NSArray arrayWithObjects:viewController4, nil]; self.tabBarController = [[[UITabBarController alloc] init]
autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nav4 ,nil];
self.window.rootViewController=self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
In Swift 2, you can just call:
self.tabBarController?.selectedIndex = 0
The simplest way to programatically switch tabs within the app is by doing this ..
[self.tabBarController setSelectedIndex:3];
I use this in one of my apps and it works extremely well.
Plasma
On Button Click
[self.navigationController pushViewController:YourView animated:YES];
[self.tabBarController setSelectedIndex:index];
self.tabBarController.selectedViewController=[self.tabBarController.viewControllers objectAtIndex:3];
Try this. This will helpful for you.

iPad Navigation controller in splitview popover frame issue

I put a navigation controller as Split-view root-view controller.
When the iPad is in Portrait mode the root view controller is displayed inside a popover controller. Its by default I know....
But My problem is The top of root-view controller is seems hide by popover. See the attched image. In it the home bar-button seems hide a small portion in top.
RootViewController *objRootViewController = [[RootViewController alloc] init];
DetailViewController *objDetailViewController = [[DetailViewController alloc] init];
UINavigationController *rootNavigationController = [[UINavigationController alloc] initWithRootViewController:objRootViewController];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:objDetailViewController];
UISplitViewController *objSplitViewController = [[UISplitViewController alloc] init];
[objSplitViewController setDelegate:objDetailViewController];
[objSplitViewController setViewControllers:[NSArray arrayWithObjects:rootNavigationController,detailNavigationController, nil]];
self.splitViewController = objSplitViewController;
[self.window addSubview:self.splitViewController.view];
[objSplitViewController release];
objSplitViewController = nil;
[rootNavigationController release];
rootNavigationController = nil;
[detailNavigationController release];
detailNavigationController = nil;
[objDetailViewController release];
objDetailViewController = nil;
[objRootViewController release];
objRootViewController = nil;

Resources