Add new viewcontroller at launch of app IOS Objective C - ios

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;
}

Related

Presenting NavigationalViewController in TabbarController

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];
}

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 :)

Hiding the master view in a Split View Controller

I am new to iPad development (or iPhone for that matter :) ) and I am building an app with a Split View Controller.
The thing is the first screen should be a single screen. I want the user to write user/pass data to connect to a remote server, and there's not really anything to show in the master view.
I know I can return YES in the splitViewController:shouldHideViewController:inOrientation:, and that works fine for the first screen, but I'd like to get the two views when the user taps the login button and the credentials are validated.
I can put some condition in shouldHideViewController and that shows the two views in the second screen and only the detail in the first, but shouldHideViewController is only called if I rotate my iPad, not when I perform the segue.
Do you have any tips to do this? Should I change my UI approach to something else? Any suggestions are welcome.
starting from scratch is a good idea. You have more control over whats going on IMO.
At your starting point you want to add your LoginViewController.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.loginController = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Later when the authentication process has finished you want to continue with SplitView, right?
- (void)continueWithSplitView {
UINavigationController *leftNav = [[UINavigationController alloc] initWithRootViewController:self.masterViewController];
UINavigationController *rightNav = [[UINavigationController alloc] initWithRootViewController:self.detailViewController];
self.splitViewController.viewControllers = [NSArray arrayWithObjects:leftNav, rightNav, nil];
self.view.window.rootViewController = self.splitViewController;
}
Notice that in both methods your desired viewController is set as the rootViewController property of UIWindow. This will automatically add the view of your controller as the top level view in UIWindow.
Furthermore make sure that you are following the MVC pattern e.g. a model object for your credentials organisation.

Resources