Presenting NavigationalViewController in TabbarController - ios

I'm developing an iOS application and structure is tab bar based. My root view controller is TabBarController but I need to present a navigational view controller in the beginning of the application if app is launched for the first time. I implemented a FlowViewController to control flow of the application. In FlowViewController, I create TabBarController and assign it as a rootviewcontroller.
How can I present a navigational view controller in the beginning of application and then present TabBarController.
FlowViewController
TabBarViewController *rootVC = [[TabBarViewController alloc] init];
rootVC.delegate = self;
rootVC.tabBar.translucent = NO;
self.rootViewController = rootVC;
In AppDelegate, didFinishLaunchingWithOptions
self.flowController = [[FlowController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[ [UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = self.flowController.rootViewController;
[self.window makeKeyAndVisible];

If a setup screen is needed in an app normally I will use NSUserDefaults to check whether the app has been launched before in viewDidLoad(). If not then I will present the setup viewController. Once the user has completed the setup process add the something to NSUserDefaults so when the app is launched it knows the user has been through the process already.

In my application, I have set UINavigationController as root view controller. Then I have added the TabBarController as child of UINavigationController.
Design the view controllers in StoryBoard, as you want. Set Storyboard id for UITabBarController and call tab bar controller using that storyboard identifier.
-(IBAction)LoginAction:(id)sender{
UITabBarController *loadTabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"TabBarViewControllerID"];
[self.navigationController pushViewController:loadTabBar animated:YES];
}

Related

Add new viewcontroller at launch of app IOS Objective C

I am new to IOS development . i got a open source project and loaded in xcode and it is running good.
the project is based on webview and loading url.
what i want to do is add a new screen at the launch of application which takes three inputs from textfields and on submit of button it should send the data to next screen which is a webview and currently it is being loaded first .
1) how can i add new view controller which launches first at launch of app where i can input textfields and click on button to move to second screen
2) get the data from first screen and give to the load url to load the url.
i have taken project files from here :
https://github.com/paulirish/iOS-WebView-App
viewcontroller option on right side doesnt have the option is initial view
i am using xcode 6.2
I think you are loading viewcontroller from code like we did in older versions. I hope this helps you.
In this code you can simply change MainViewController to your own view.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MainViewController* viewC =[[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
UINavigationController* navC = [[UINavigationController alloc] initWithRootViewController:viewC];
self.window.rootViewController = navC;
[self.window makeKeyAndVisible];
You will have to create new UIViewController with your three textFields and then set this UIViewController as Initial View Controller from storyboard like this:
After doing this get data from your textfields like:
NSString *text1Data = self.yourtextField.text;//Repeat for all text fields.
After getting data make three variables in your webViewController and assign thse values to them.
Suppose WebVC is yoour WebViewController then you can pass data and go to web viw like this:
WebVC *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"your_WebVc_identifier"];
vc.text1DataFromFirstVC = text1Data; //repeat for all three variables
//go to web view controller
[self.navigationController pushViewController: vc animated:YES]
EDIT:For Non Storyboard use following code from your AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *Vc = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = Vc;
[self.window makeKeyAndVisible];
return YES;
}

obj c, xcode making a view transition

I am trying to make a button on a navigation controller that causes a transition to another view controller (that has a back button to the previous nav view).
I'm trying to do this programmatically.
I made a UINavigationController and a UITabBarController. The nav controller is the opening tab of the tab controller.
I made a subclass of the UIViewController called SubVieController. I haven't added anything to the Sub classes, only the auto generated material.
I've made all my edits to the didFInishLaunchingWithOptions method in Appdelegate.m.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
UITabBarController * tabBarController = [[UITabBarController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SubViewController * firstTab= [[SubViewController alloc] initWithNibName:#"SubViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:firstTab];
SecondViewController *secondTab = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
SubTableController *table = [[SubTableController alloc]initWithNibName:#"SubTableController" bundle:nil];
navigationController1.tabBarItem.title = #"First Nav";
secondTab.tabBarItem.title =#"Second";
table.title = #"Third Table";
tabBarController.viewControllers = #[navigationController1,secondTab, table];
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
return YES;
}
And I try to move a Button onto the SubViewController.xib file to connect it into the code so I can make a button action. But when I ctrl+drag, nothing inserts into the code like usual.
What do I do to have the button cause a transition from the starting view to a separate view that is still considered to be on the 1st tab, with a back button to the first view which is also in the first tab?
EDIT:
This code seems to work for the button action:
- (IBAction)transition:(UIButton *)sender{
SubViewController * view2 = [[SubViewController alloc]init];
[self.navigationController pushViewController:view2 animated:YES];
}
As you have correctly done, the UITabBarController should not push a UIViewController but a UINavigationController. That UINavigationController should have a rootViewController.
Not being able to Control-Drag in Interface Builder generally means that the Custom Class in the Identity Inspector is not set properly.
Connecting IBAction
In Xcode project, Select SubViewController.xib
Select your button
Xcode > View > Utilities > Show Connections Inspector
Xcode > View > Assistant Editor > Show Assistant Editor
Ensure that Automatic > SubViewController.m > No Selection is present. If not, you may not have the Custom Class set properly in IB
In Connections, click Touch Up Inside grommet and drag onto Assistant, right below #implementation SubViewController
Enter name, such as buttonTappedAction
Add code to push the UIViewController as needed:
Obj-C
UIViewController * vc = [[UIViewController alloc] initWithNibName:#"id" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
Swift
let vc = UIViewController(nibName: "id", bundle: nil)
self.navigationController?.pushViewController(vc, animated: true)
Storyboard
Architecture suggestion:
Start over using Storyboard
There is no need to write a single line of code to implement the above solution using a Storyboard!

Switch views in empty application without storyboard

I'm new to iOS development and am working on an app the interacts with a web service. I have a login view controller with it's respective NIB file, which controls the login page. And then I have a list view controller with it's respective NIB file, which displays a list of elements from the web service after logging in. When I created the application I created an empty application so right now I don't have a storyboard in my project.
I load the login view controller as the root view controller. After the login button is pressed and the credentials are validated I want to take users to the list view with the list view controller. After doing some research it seems that you need a storyboard to do this.
Do I actually need a storyboard to switch views and view controllers? If not how do I add a storyboard and use it with my current views, so I don't have to redo a whole view?
Any help would be greatly appreciated.
I am trying to discuss some case regarding your question.
Case 1: You have a navigation controller as the rootViewController of you window, and loginViewController as rootViewController of that navigation controller.
If login successful you can change the root view controller of navigation controller. For this you need to set a property in your AppDelegate. Like this:
#property (strong, nonatomic) UINavigationController *navControl;
In AppDelegate.m file
self.navControl = [[UINavigationController alloc] initWithRootViewController:login];
The assign a root view controller in navControl. And set it as a rootViewController of you window.
self.window.rootViewController = self.navControl;
And from your loginController if login successful then change the rootViewController of your navControl by creating an instance of AppDelegate.
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.navControl.rootViewController = anotherViewController;
Note: Login is a special case. You don't want to come back to login page by pushing back button. So change the rootViewController of you navigation controller. But for other view you can just push ViewControllers.
Case 2: You don't have navControl as rootViewController. And if you want to just change the rootViewController of your window then you can do this:
appDelegate.window.rootViewController = anotherViewController;
Hope this helps.
1:first of all create UiWindow property in app delegate.h
#property (strong, nonatomic) UIWindow *window;
2:In app delegate.m file create a universal variable of UINavigationController
UINavigationController *navigationController;
3:customise your app delegate method like this, please note that import viewcontroller.h file in app delegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//suppose view controller is your login page
ViewController *viewController= [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[navigationController.navigationBar setHidden:TRUE];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];
self.window.backgroundColor = [UIColor whiteColor];
return YES;
}
I hope it will work.

iOS initial log in view before navigational Controller

I have been playing around with this for a couple of days and I cannot figure this out.
-> Basically I want to implement a simple login view that has a button when clicked, goes to go to the navigation controller ( in my case is "viewController" with buttons that link to mini math games which are other views).
-> Login screen should be displayed first, than navigation controller's root view when a button is clicked on the login screen
-> I have tried to declare the navigation controller when I click the button of the login screen but that seems to not work
-> Is it safe to say that a navigation controller can only be initialized in the apple delegate?
Currently I have this in my apple delegate declaring and setting my navigational controller:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navigationViewController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; // self.viewController is the main screen
self.window.rootViewController = navigationViewController; // set root to navigationViewController
[self.window makeKeyAndVisible];
return YES;
}
Any ideas will be appreciated. Thank you for your time !
Your code in the app delegate looks ok. NavigationController does not need to be declared in the AppDelegate. In your case, it is definitely ok to declare it upon login button pressed.
Try this at the login event:
UIViewController *nextVC = [[UIViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:nextVC];
[self presentViewController:navController animated:YES completion:^{
}];
What I would do would be set the loginViewController as the rootViewController of the navigation. And after check if login was done successfully, you could implement [self performSegueWithIdentifier:#"identifier"] setting the game viewController as destination. (Using Storyboard would make your life much easier). Well, that's my opinion :)

add a tabbed view as a main view to a navigation-based iphone app

I am relatively new to iOS, hence I apologize for any inconsistency in my question. I need help with the following issue with an app I'm trying to build. My issue is this: The app i am working has a navigation based functionality with a tableview(daily filled by user) and a detailed tableview listing the inputs of the user, but this is just one functionality of the app.
I want to have a main tab based view where one of the tabs(each tab representing a functionality) points to this module.
I wanted to ask for steps and changes i need to make to for example app delegate or rootviewcontroller(I can post the code if it helps better) to make is so that the app starts with a mutli-tabbed bar view where one tab refers to view linked to the rootviewontroller of the navigation-based app.
For summary: Need a main tab bar view where one tab points to the rootviewcontroller highlighted in the screenshot(link below)
If helpful here is a relevant function code i have in app delegate :
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
RootViewController *rootViewController = (RootViewController *)[[navigationController viewControllers] objectAtIndex:0];
rootViewController.managedObjectContext = self.managedObjectContext;
//Next TWO LINES FOR COLOR BACKGROUND
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]];
}
PS:Here is the screenshot for the storyboard: where i would like to have one tab refer to the view(highlighted in the screenshot) which is linked class rootviewcontroller.m/h
The screenshot: http://i.stack.imgur.com/G9AXI.png
edit: The actual question can be seen as: How and what do i need to do to have a tabbarviewcontroller which i would add with storyboard become my rootviewcontroller instead of the navigationcontroller(highlighted in black in the screenshot: http://i.stack.imgur.com/G9AXI.png).
My current rootviewcontroller.m manages anything related to the tableview of the current navigationviewcontroller, do i need to change that also?.
I apologize for excessiv details, I am really new to iOS dev.
From this one http://i.stack.imgur.com/suLBm.png I tried to embedd in tab barviewcontrol only with storyboard to this one http://i.stack.imgur.com/TZxLo.png I tried to embedd in a tab controller just by story but i get an error :'NSInvalidArgumentException', reason: '-[UIViewController setManagedObjectContext:]: unrecognized selector sent to instance 0x8184e30'
classes related to this are(especially rootviewcontroller.m which is a navigationcontroller for now:
AppDelegate.{h,m}
Configures the Core Data stack and the first view controllers.
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
RootViewController *rootViewController = (RootViewController *)[[navigationController viewControllers] objectAtIndex:0];
rootViewController.managedObjectContext = self.managedObjectContext;
}
RootViewController.{h,m}
Manages a table view for listing all values entered. Provides controls for adding and removing these values.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
DetailViewController.{h,m}
Manages a detail display for display details of each entered value.
My initial guess is that i need to change the rootviewcontroller appdidfinishlaunching.
Any suggestions ?
In fact now you have:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
RootViewController *rootViewController = (RootViewController *)[[[[tabController viewControllers] objectAtIndex:0] viewControllers] objectAtIndex:0];
rootViewController.managedObjectContext = self.managedObjectContext;
}
So you actually need a UITabBarViewController in the Storyboard and you can point to the UINavigationController if you want the ability to push other controllers.
You don't need other UINavigationControllers as I saw in your screenshot, as long as the rootviewcontroller is an UINavigationController.
You can add the UINavigationController as first of the tabs and then you can go and fill the other tabs with the viewcontrollers that you need displayed.
SO basically you need to create UITabBarController as rootviewcontroller.
Let me know if I understood your question correctly.
Here is an example of UITabBarController :
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
//Here you set your controller
UIViewController* centerController = [[UIViewController alloc]init];
UINavigationController *navCenter = [[[UINavigationController alloc] initWithRootViewController:centerController] autorelease];
UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];
tabBarController.viewControllers = [NSArray arrayWithObjects:navCenter,nil];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.rootViewController = tabBarController;
return YES;
}
Let me know if it worked.
You should have something like this :

Resources