How do I start another view controller with no storyboard? - ios

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.

Related

navigation controller pushViewController not working

I'm creating an application that has 2 main view controllers at the moment. The app loads into the initial viewController, and clicking a button inside should bring up the second viewController. Here's what I have:
AppDelegate.h
#import <UIKit/UIKit.h>
#import "ViewController1.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController1 *mainViewCtr;
#property (strong, nonatomic) UINavigationController *navigationController;
#end
AppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application {
_mainViewCtr = [[ViewController1 alloc] initWithNibName:#"mainViewCtr" bundle:nil];
_navigationController = [[UINavigationController alloc] initWithRootViewController:_mainViewCtr];
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_window.rootViewController = _navigationController;
_navigationController.delegate = self;
_navigationController.navigationBarHidden = YES;
[_window addSubview:_navigationController.view];
[self.window makeKeyAndVisible];
}
and my button method inside viewcontroller1:
- (IBAction)SessionNickNameSubmit:(id)sender {
ViewController2 *secondViewCtrl = [[ViewController2 alloc] initWithNibName:#"secondViewCtrl" bundle:nil];
[self.navigationController pushViewController:secondViewCtrl animated:YES];
}
but when I click the button the view doesn't change. I tried debugging and the code is hit, but nothing happens.
am I missing a setting somewhere?
UPDATE
I've updated all viewController variable names:
instead of ViewController1/2 I'm using mainViewCtrl and secondViewCtrl
but still no use :(
You made a typo:
it's
_window.rootViewController = _navigationController;
not
_window.rootViewController = _joinViewController;
And NeverHopeless's suggestion is also spot on. It's probably the typo AND the fact that you add your second viewcontroller as ViewController2 and not using a proper variable name.
Another suggestion is making a storyboard (if you are not using one) and adding a segue for the transition. Simply assign the segue processing to the button. Like this:
-(IBAction)SessionNicknameSubmit:(id)sender
{
[self performSegueWithIdentifier:#"identifier" sender:self ];
}
Here is a nice description of how it works and how to use it plus some useful pointers!
Obj-C is a case sensitive language, class name and instance name should not be the same like ViewController2. Try like this:
- (IBAction)SessionNickNameSubmit:(id)sender {
ViewController2 *viewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
[self.navigationController pushViewController:viewController2 animated:YES];
}
The reason is that you have set the window's rootViewController to ViewController1.
You need to set you navigation controller to the window's rootViewController.
So that when you try to access the self.navigationController on the press of the button, it will access the navigation controller in which the self resides i.e. your window's rootViewController now.
Then it will push the next view controller properly.
After looking at almost every tutorial and every stack overflow answer, I finally found a solution that worked. I had to make an instance of the storyboard in the app delegate and use that to create my first view controller instance.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.joinViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController1"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:joinViewController];
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
navigationController.navigationBarHidden = YES;
_window.rootViewController = navigationController;
[_window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
I think the problem was that when I was creating an instance of ViewController, it was creating a new instance and binding the navigation controller to it (independent of the view controller that was showing up in the simulator). So when I was using the push method it wasn't recognizing self.NavigationController (that's why NSLog(self.NavigationController == nil) was logging 1

Not understanding how the NavigationController and UIViewControllers are working in iOS

I have a project which I don't really understand the views and navigation behind. I start out in the AppDelegate (MAAppDelegate), where I define properties:
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UIViewController *detailViewController;
Then in the MAAppDelegate.m, I create a navigationController, and
#implementation MAAppDelegate
#synthesize detailViewController;
#synthesize window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Init the navController for the Master Detail View of the grade cells
UINavigationController *navController = [[UINavigationController alloc] init];
detailViewController = [[UIViewController alloc] init];
UIViewController *viewController = [[MAController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
So at this point, I think I have a working naviationController, I've setup an instance of a custom UIViewController (custom class MAController) and I've set it up as the rootViewController.
Then, in my MAController class, the class where I do all of my UI stuff (the entire UI is done programmatically, no nibs or storyboards). Here is a bit of the viewDidLoad of MAController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationController setNavigationBarHidden:YES]; // I commented this line out and realized it does nothing
I go on (in viewDidLoad) to add a bunch of subviews to self.view, like this
[self.view addSubview:self.backgroundImageView];
Earlier, I created a viewController in the AppDelegate class and it was called view, so I assumed it was refereeing to that but now since I've changed it (in AppDelegate) to viewController, I guess I was thinking wrong?
And then finally, I create a UIView in 'viewDidLoad`:
UIView *header = [[UIView alloc] initWithFrame:headerFrame];
header.backgroundColor = [UIColor clearColor];
self.tableView.tableHeaderView = header;
and start adding a bunch of subviews to this new UIView I created header.
So, in short, I have no idea what is happening. Later, when I tried telling (from a method inside MAController) self.navigationController (which I assumed to be navigationController in charge of everything in my project - created at the beginning in the AppDelegate) to pushViewController a new viewController that I was going to use as a detailView for a table, it got weird.
So I'm just trying to understand what has control, and what the rootViewController is, and just what is happening.
The main window root is set to a view controller and not the navigation controller
Change:
self.window.rootViewController = viewController;
to:
self.window.rootViewController = navController;
EDIT:
You can access the navigationController from anywhere by asking your appDelegate. It is normally not considered a good practice:
MAAppDelegate *delegate = (MAAppDelegate *)[[UIApplication sharedApplication] delegate];
UINavigationController *nav = delegate.navigationController;
Don't forget to:
#import "MAAppDelegate.h"
First, take a little time and read through how navigation controllers work. The documentation is really helpful:
https://developer.apple.com/library/ios/documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
Second, your problem is that your window's root view controller is not the navigation controller you created. Rather it is an instance of MAController. This is what you're doing:
UIViewController *viewController = [[MAController alloc] init];
// some other code ...
self.window.rootViewController = viewController;
I think you meant to add MAController as the root view controller of the navigation controller and make the navigation controller your window's root. If so, you'll want to set your view controllers up like this:
UIViewController *viewController = [[MAController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navController;
Another potential problem is that you don't seem to be doing anything with your detailViewController. Maybe that's confusing you too.

viewDidLoad is not called for rootViewController

This is my first iOS project. I'm just following the tutorial from Try iOS over at codeschool in my XCode Application.
I've added a button in my viewDidLoad method. I don't think this is where it would be added normally, but it should still work. The problem is, the method is never called. Here's my code so far:
AppDelegate.m:
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Set window size & background color
CGRect viewRect = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame:viewRect];
self.viewController = [[ViewController alloc] init];
UIView *view = [[UIView alloc] initWithFrame:viewRect];
view.backgroundColor = [UIColor yellowColor];
self.viewController.view = view;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
NSLog(#"Screen is %f tall and %f wide", viewRect.size.height, viewRect.size.width);
return YES;
}...
AppDelegate.h:
#import <UIKit/UIKit.h>
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
firstButton.frame = CGRectMake(100, 100, 100, 44);
[firstButton setTitle:#"Don't Click!" forState:UIControlStateNormal];
[self.view addSubview:firstButton];
}...
When the app is up and running, my background remains yellow, and there is no button visible.
Stepping through the application, didFinishLaunchingWithOptions: hits as I expected. When the view is initializes, I expect the viewDidLoad to run. I may just be misunderstanding the way C "sends messages".
If any additional information might help please let me know. Thanks for any help :)
EDIT
Does this have something to do with storyboarding? I'm in storyboard mode but I never added any button using the GUI
viewDidLoad is not being called because of the non-standard way that you're creating its view. Usually, the view is loaded from a xib or storyboard, or created in code in the loadView method of the view controller -- in all these cases, viewDidLoad will be called. If you move your view creation to loadView in the controller's .m file, it will work, and viewDidLoad will be called.
After Edit:
If you're using a storyboard, your view should be created there, and you shouldn't be doing what you're doing in the app delegate. In fact, when you use a storyboard, you don't normally have any code in the application:didFinishLaunchingWithOptions: method.
...
self.viewController.view = view;
...
You shouldn't set viewController's view before viewDidLoad, which cause viewDidLoad not called. View should be set in viewDidLoad, and the viewController's will manage its view automatically for you.
Looking at the code, this is exactly what should be happening. When you create a viewController, it automatically creates a view so you do not want to create a new one for it. Your button is not showing up because it is on the original view created with the viewController but you overwrote it with your yellow view.
Instead what you want to do is go into ViewController.m's viewDidLoad:(BOOL)animated and add:
[self.view setBackgroundColor:[UIColor yellowColor]];
Also, make sure that you do not set viewController.view in appDelegate. If you do not set that, a view is automatically created and you can add your button or any other views to it in the viewDidLoad.
I had this issue, and the initWithCoder function was called when I added some code in the AppDelegate's didFinishLaunchingWithOptions function.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];(NSDictionary *)launchOptions
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"MyStoryboardName" bundle:nil];
UIViewController *initViewController = [storyboard instantiateInitialViewController];
[self.window setRootViewController:initViewController];

ViewController programmatically?

I would like to know what steps are needed once a fresh "Single view" project was created in xcode, in order to achieve:
1. a viewController that initializes without a NIB, but rather programmatically loads it's own controls in its view.
2. How to get that viewcontroller's view to load and call viewDidLoad?
3. make the view for that controller visible on the screen with all of the controls.
How do I go about this from this function:
-(BOOL)application:(UIApplication*)application didFinishLoadingWithOptions:(NSDictionary *)launchOptions
I am trying to modify a new xcode project but all I get is a black screeen, viewDidLoad doesn't get called
That's your app delegate's application loading method.
In there, you would probably want to create an instance of your custom view controller and assign that as the rootViewController to your app delegate didFinishLoading. There should be a line like:
// app delegate .h file
#import "CustomViewController.h"
#interface
{
...
CustomViewController *myCustomVC;
...
}
#property (nonatomic, retain) CustomViewController *myCustomVC;
// app delegate .m file
#implementation AppDelegate
#synthesize myCustomVC;
-(BOOL)application:(UIApplication*)application didFinishLoadingWithOptions:(NSDictionary *)launchOptions
{
...
myCustomerVC = [[CustomViewController alloc] init];
[self.window setRootViewController:myCustomVC];
...
}
Then inside your custom view controller's viewDidLoad method, you can do this as a test:
// custom view controller .m file
-(void)viewDidLoad
{
self.view.backgroundColor = [UIColor redColor];
}
UIViewController *myViewController = [[UIViewController alloc] init];
[myViewController.view setFrame:self.view.bounds];
[self.view addSubview:myViewController.view]; // if you want to add it in another viewcontroller
// For testing, set the background color to something other than white (default)
[myViewController.view setBackgroundColor:[UIColor greenColor]];
And off you go !
You need to create a subclass of UIViewController, and setup your view hierarchy either in loadView, or viewDidLoad (depending on the level of customisation)
By subclassing UIViewController the loading method calls will be made for you so you don't have to worry about getting getting viewDidLoad etc.
To make it visible on the screen the simplest way is to set it as the rootViewController of the apps window
inside didFinishLaunchingWithOptions: in your app delegate
self.window.rootViewController = [[MyViewControllerSubclass alloc] init];
Try This :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
HomeViewController *homeVC = [[HomeViewController alloc]init];
[self.window setRootViewController:homeVC];
[self.window makeKeyAndVisible];
return YES;
}
Remove Main(storyboard reference) from Main interface of general Setting :
Add Launch Image :
And select iOS-7 and later in your left corner setting

IOS How to alter an existing UI to use a new navigation controller

Ok, I'm having trouble with this.
I started an Application with a UITableViewController custom class called MainView.
In interface builder I modified the view so that there is a text box and a button on top and a UITableView. Connected the data source and delegate to the MainView class, and programmed in all the code to use the text field as a search field, the button as a search trigger, and then programmed in the proper delegate methods to populate the UITableView. This works perfectly.
Now the next step was to use the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
method to be able to show a webview with a link related to the selected row. So reading I see that I need to add a UINavigationController to the mix ( so as to get the toolbar and navigation bar etc..).
Now I tried this:
add a navigation controller from the Interface builder, and connect to the appdelegate, and an outlet
#import
#interface ComparateurAppDelegate : NSObject {
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
then in the implementation - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
}
this launches ok, same as before, and I don't have the top bar I should have. plus when I try to push a new view in it does nothing.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WebViewController *wvc = [[WebViewController alloc] initWithNibName:#"WebViewController.nib" bundle:nil];
[self.navigationController pushViewController:wvc animated:YES];
[wvc release];
}
I know the code is running because I nslog it. but nothing is happening. WebViewController.nib is the nib file I created ( empty right now) so that I populate it with a UIwebview.
My guess at this moment is that the view hierarchy is not set ok.
So I tried a few things I can't remember exactly and got the toolbar on top of the window I already have, but with all the content inaccessible ( like if the toolbar was a modal dialog and I could not use the bottom one , or like a transparent view and you could see underneath to the first window that was stacked).
I also tried creating programatically the UINavigation controller in the appDelegate like so
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIViewController *rootController = [[MainView alloc]init];
navigationController = [[UINavigationController alloc]initWithRootViewController:rootController];
[rootController release];
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
And all I got is an empty UITableView for an interface, and not the MainView.nib ( whose class is set as MainView).
TL;DR:
Have an app that is working, want to add detailed view, and need to modify to a UINavigationController but can't seem to find the proper way to do it.
thanks in advance and sorry for the long post.
Try changing this:
UIViewController *rootController = [[MainView alloc]init];
To this:
UIViewController *rootController = [[MainView alloc]initWithNibName:#"MainView" bundle:nil];

Resources