Switch views in empty application without storyboard - ios

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.

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

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

How do I start another view controller with no storyboard?

I'm not using a storyboard or anything. I'm just creating the cocoa classes and linking them up individually. I can get to load up the default View Controller which is SplashViewController but i can't get past there.
I have experience in php, android programming and python, but i'm totally clueless on how Obj-C and how the iOS framework works :(
SplashViewController.m
-(void)initializeInterface
{
//Initialize start button
[self.startButton addTarget:self action:#selector(startActivity) forControlEvents:UIControlEventTouchDown];
//Initialize fading backgrounds
[self animateImages];
}
-(void)startActivity
{
PhoneViewController *phoneView = [[PhoneViewController alloc] initWithNibName:#"PhoneViewController" bundle:nil];
[self.navigationController pushViewController:phoneView animated:YES];
}
SplashViewController.h
#import <UIKit/UIKit.h>
#import "PhoneViewController.m"
#class PhoneViewController;
#interface SplashViewController : UIViewController
#property (strong, nonatomic) PhoneViewController * phoneViewController;
#property UIImage *splashbg1;
#property UIImage *splashbg2;
#property (nonatomic, retain) IBOutlet UIImageView *splashbg;
#property (nonatomic, retain) IBOutlet UIButton *startButton;
-(void)initializeInterface;
-(void)animateImages;
-(void)startActivity;
#end
EDIT
classAppDelegate.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];
//Move from delegate view controller to root view controller
self.window.rootViewController=[SplashViewController new];
[self.window makeKeyAndVisible];
return YES;
}
Wrap your splash view controller in a navigation controller.
Otherwise, the navigationController property of your splash view controller is nil and pushViewController has no effect.
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: splashViewController];
To move from one UIViewController to other UIViewController, you can try the following things
If SecondViewController *secondViewController is the UIViewController you want to move in to, then your can do the following:
[self presentViewController: secondViewController animated:YES completion:nil];
This is when you UIViewController is not embedded inside a UINavigationController.
It is possible to create your view controllers entirely in code without using Storyboards or XIB files, but it's not recommended. It's like trying to write a complex user application in assembler. The state of the art has evolved since the days when that was necessary. There are better tools. Use them.
Creating everything yourself is both quite complex and not very well documented. You are setting yourself up for a very frustrating, error-prone process. I've been doing iOS development pretty much full time since 2009, and I would not attempt this.
That being said, if you are a masochist, you would create your view controller using initWithNibName:bundle:, passing in nil for both parameters, and then implement the loadView method. In loadView you're create your view hierarchy and install it.
If you are new to iOS/Objective-C, DO NOT DO THIS. It is like trying to write a kernel device driver in machine code as your first foray into UNIX.
Change you AppDelegate method as below -
- (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];
UINavigationController *navcon = [[UINavigationController alloc]initWithRootViewController:[SplashViewController new]];
//Move from delegate view controller to root view controller
self.window.rootViewController=navcon;
[self.window makeKeyAndVisible];
return YES;
}
Problem in your code, you have not taken any navigationController, that enables you push or pop UIViewController. Doing above you can use your method -(void)startActivity to Start a new ViewController.

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

Set up View Controllers in App Delegate Xcode 5

I wanted to know how to set up the app delegate in Xcode 5 since it's different than it was in previous versions. I want the generic view controller files (ViewController.h and .m) to be the files that control the rootViewController I set in my app delegate. Does this happen automatically or do you need to do something in the code? This is how I set up my appDelegate.m
*(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect viewRect = [[UIScreen mainScreen]bounds];
self.window = [[UIWindow alloc]initWithFrame:viewRect];
UIViewController *viewController = [[UIViewController alloc]init];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
If I wanted my rootView Controller to be a table view controller or something else, would I need to embed it in a basic VC first?
To do it programmatically you can set it to be a UITableViewController since it is a subclass of UIViewController. If you want to use the already create ViewController just change the subclass in the .h file from UIViewController to UITableViewController and add the tableview delegates and datasources into the .m.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *mainViewController = [storyboard instantiateInitialViewControllerWithIdentifier:#"myViewController"];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = mainViewController;
[self.window makeKeyAndVisible];
or if you want to just draw the view in the view controller create a class and do this
MyViewController *viewController = [[MyViewController alloc]init];
self.window.rootViewController = viewController;
If you are wanting to set your root view via storyboards you can just check the __Is Initial view controller` option
If I wanted my rootView Controller to be a table view controller or something else, would I need to embed it in a basic VC first?
No.
You can set any UIViewController class to be the rootViewController. I am assuming you are building your viewController hierarchy in code, and you are not using InterfaceBuidler. Although also with Interface builder, you can use any viewContorller as the rootviewcontroller, either using storyboards or plain xibs.

Resources