How to load and unload a UIViewController from the window - ios

I have create some logic when the app is loaded that I can load from 3 different views depending on some values I set in my plist.
This is what my code looks like
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//sets context for coredata
CoreDataController *coreDataController = [CoreDataController sharedManager];
coreDataController.managedObjectContext = self.managedObjectContext;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
PrefsController *prefsController = [[PrefsController alloc] init];
NSDictionary *prefsDictionary = [prefsController readPrefs];
NSLog(#"%#", prefsDictionary);
NSString *projectListBoolString = [prefsDictionary objectForKey:#"ProjectListAvailable"];
NSString *installsBoolString = [prefsDictionary objectForKey:#"InstallsAvailable"];
NSString *finishinBoolString = [prefsDictionary objectForKey:#"FinishingAvailable"];
if (([projectListBoolString isEqualToString:#"T"]) && ([installsBoolString isEqualToString:#"F"]) && ([finishinBoolString isEqualToString:#"F"])) {
self.getProjectListViewController = [[GetProjectListViewController alloc] initWithNibName:#"GetProjectListViewController" bundle:nil];
self.window.rootViewController = self.getProjectListViewController;
[self.window makeKeyAndVisible];
}
else if (([projectListBoolString isEqualToString:#"T"]) && ([installsBoolString isEqualToString:#"T"]) && ([finishinBoolString isEqualToString:#"T"])) {
self.currentProjectListViewController = [[CurrentProjectListViewController alloc] initWithNibName:#"CurrentProjectListViewController" bundle:nil];
self.window.rootViewController = self.currentProjectListViewController;
[self.window makeKeyAndVisible];
}
else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
}
return YES;
}
I would like to be able to then load and unload UIViewControllers (including removing from memory by using buttons presses etc.
I dont want to use a navigaiton based controller as I want the views to be static or individual if that makes more sense.
If someone could show me some example code to load a new UIViewController to the window and remove the old UIViewController that would be greatly apprecaited.
However I am not sure of the correct was to handle this, or even how the code looks.
any help would be greatly appreciated.

For me, is not a good way to load your view like this.
It would be better to load a rootViewController in your AppDelegate and add your current view depending of your plist inside the RootViewController :
AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
After in your RootViewController add a subview of the current view depending on your plist :
RootViewController.m
#pragma mark - View management
- (void)viewDidLoad
{
[super viewDidLoad];
NSDictionary *prefsDictionary = [prefsController readPrefs];
NSLog(#"%#", prefsDictionary);
NSString *projectListBoolString = [prefsDictionary objectForKey:#"ProjectListAvailable"];
NSString *installsBoolString = [prefsDictionary objectForKey:#"InstallsAvailable"];
NSString *finishinBoolString = [prefsDictionary objectForKey:#"FinishingAvailable"];
if (([projectListBoolString isEqualToString:#"T"]) && ([installsBoolString isEqualToString:#"F"]) && ([finishinBoolString isEqualToString:#"F"])) {
self.getProjectListViewController = [[GetProjectListViewController alloc] initWithNibName:#"GetProjectListViewController" bundle:nil];
// Add View Controller
[self.view addSubview:self.getProjectListViewController.view
}
else if (([projectListBoolString isEqualToString:#"T"]) && ([installsBoolString isEqualToString:#"T"]) && ([finishinBoolString isEqualToString:#"T"])) {
self.currentProjectListViewController = [[CurrentProjectListViewController alloc] initWithNibName:#"CurrentProjectListViewController" bundle:nil];
// Add View Controller
[self.view addSubview:self.currentProjectListViewController.view];
}
else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
// Add View Controller
[self.view addSubview:self.viewController.view];
}
}

Related

Setting NavigationController as an initial controller

I have two navigation controllers. How can I set the second one - stopwatch as an initial navigation controller?
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.swipeBetweenVC = [Swipe new];
[self setupRootViewControllerForWindow];
self.window.rootViewController = self.swipeBetweenVC;
[self.window makeKeyAndVisible];
return YES;
}
- (void)setupRootViewControllerForWindow
{
UIStoryboard *storyboard1 = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
Stopwatch *controller1 = (Stopwatch *)[storyboard1 instantiateViewControllerWithIdentifier:#"Stopwatch"];
UINavigationController *stopwatch = [[UINavigationController alloc] initWithRootViewController:controller1];
NamesController *controller2 = (NamesController *)[storyboard1 instantiateViewControllerWithIdentifier:#"NamesController"];
UINavigationController *namesList = [[UINavigationController alloc] initWithRootViewController:controller2];
self.swipeBetweenVC.viewControllers = #[namesList, stopwatch];
}
Just put the stopwatch view first
self.swipeBetweenVC.viewControllers = #[stopwatch, namesList];
Currently you have some Swipe type set up as a root VC, change to self.window.rootViewController = stopwatch;

Cant load ViewController when add local notification scripts to "didFinishLaunchingWithOptions"

after adding local notification script in "didFinishLaunchingWithOptions" it said
[1627:60b] Application windows are expected to have a root view
controller at the end of application launch
and this is the code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
mainStoryboard = nil;
if (IS_IPHONE_5) {
mainStoryboard = [UIStoryboard storyboardWithName:#"MainIPhone5" bundle:nil];
}
else {
mainStoryboard = [UIStoryboard storyboardWithName:#"MainIPhone" bundle:nil];
}
UILocalNotification *localNotification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
SelectedTask = [localNotification.userInfo objectForKey:#"FullTaskName"];
TaskViewController *TaskViewControllerVar;
TaskViewControllerVar = [mainStoryboard instantiateViewControllerWithIdentifier:#"TaskViewController"];
TaskViewControllerVar.SelectedTask = SelectedTask;
self.window.rootViewController = TaskViewControllerVar;
NSLog(#"1");
}else{
RecordingViewController *RecordingViewControllerVar;
RecordingViewControllerVar = [mainStoryboard instantiateViewControllerWithIdentifier:#"RecordingViewController"];
self.window.rootViewController = RecordingViewControllerVar;
NSLog(#"2");
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
return YES;
}
remove this line
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
you also need to add the views. Like this:
self.window.rootViewController = TaskViewControllerVar;
[self.window addSubview:TaskViewControllerVar.view];
and
self.window.rootViewController = RecordingViewControllerVar;
[self.window addSubview:RecordingViewControllerVar.view];

Can't add NavigationController on TableView using UISplitView

I am having a little hard time here, forgive me if you think my problem is so easy for you.
I am trying to create app using UISplitView. The 1st View on the left is a TableView and the other one on the right is just a normal view.
This is my Code for in AppDelegate.m for UISplitView.
#import "AppDelegate.h"
#import "MasterViewController.h"
#import "DetailViewController.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MasterViewController *masterVC = [[MasterViewController alloc]init];
DetailViewController *detailVC = [[DetailViewController alloc]init];
UISplitViewController *splitVC = [[UISplitViewController alloc]init];
[splitVC setViewControllers:[NSArray arrayWithObjects:masterVC,detailVC,nil]];
[self.window setRootViewController:splitVC];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Now, I want to add a Navigation Bar on the TableView, I just don't know how to add if I am using SplitView,but I can when I am using a single TableView.
This is my Code in AppDelegate.m using a single View Application that uses TableView. (This is working)
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
//create UINavigationController
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
Hope you can understand what I'm trying to say. I can't post images since i don't have enough reputation. AGAIN.. The question is "How can I add Navigation Controller in my TableView if I used UISplitView?"
Do you think it will be easy for me if I use storyboards instead of using two XIB files?Hope you can help me.
Thanks in advanced!
try this code
in AppDelegate.h file
UINavigationController *detailNavigationController;
UINavigationController *masterNavigationController;
UISplitViewController *HomeSpilitView;
HomeSpilitViewController *HomeMster;
HomeDetailsViewController *HomeDetailsViewControllers;
in AppDelegate.m file
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSMutableArray *array = [NSMutableArray array];
HomeSpilitView = [[[UISplitViewController alloc] init]autorelease];
HomeMster = [[HomeSpilitViewController alloc] initWithNibName:#"HomeSpilitViewController" bundle:nil];
masterNavigationController = [[[UINavigationController alloc] initWithRootViewController:HomeMster] autorelease];
HomeMster.title=#"Title home";
masterNavigationController.navigationBar.tintColor =[UIColor colorWithRed:255/255.0 green:108/255.0 blue:61/255.0 alpha:0.1];
[array addObject:masterNavigationController];
HomeDetailsViewController *HomeDetailsViewControllers = [[HomeDetailsViewController alloc] initWithNibName:#"HomeDetailsViewController" bundle:nil];
detailNavigationController = [[[UINavigationController alloc] initWithRootViewController:HomeDetailsViewControllers] autorelease];
detailNavigationController.navigationBar.tintColor =[UIColor colorWithRed:255/255.0 green:108/255.0 blue:61/255.0 alpha:0.1];
HomeDetailsViewControllers.title=#"details title";
HomeMster.objHomeDetailsViewcontroller=HomeDetailsViewControllers;
HomeSpilitView.delegate = HomeDetailsViewControllers;
[array addObject:detailNavigationController];
[HomeSpilitView setViewControllers:array];
[self.window setRootViewController:HomeSpilitView];
[self.window makeKeyAndVisible];
return YES;
}

iOS unable to load views

I am trying to navigate from one view to another on touch of a button using iOS 5.0.
The code I am using
- (IBAction)Actionlogin:(id)sender {
NSLog(#" login button has been pressed");
NSLog(#"In init");
test_details *aSecondPageController=[[test_details alloc]initWithNibName:#"test_details" bundle:nil];
[self.navigationController pushViewController:aSecondPageController animated:NO];
}
I have two xib files test_details_iPhone.xib and test_details_iPad.xib
Inside my testdetails.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSLog(#"it is coming here in second view");
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
NSLog(#"it has been returned ");
return self;
}
LOGs
2013-04-25 11:06:17.191 app_gototest[3067:207] login button has been pressed
2013-04-25 11:06:17.194 app_gototest[3067:207] In init
2013-04-25 11:06:17.195 app_gototest[3067:207] it is coming here in second view
2013-04-25 11:06:17.196 app_gototest[3067:207] it has been returned
The view is not getting loaded onto the view. I suppose I am missing something.
application:didFinishLaunchingWithOptions: in appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
** I am trying Jasper Blue's approach**
argc int 1
argv char ** 0xbfffed2c
*argv char * 0xbfffee44
In appDeleagte.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController* navigationController;
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
} else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
}
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
You code looks OK, so it must be that self.navigationController is nil. . . Have you set up a navigation controller?
For your code to work, your current UIViewController needs to be contained within a UINavigationController. . . you can set up the UINavigationController as the root view controller in your application delegate as follows:
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController* navigationController;
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
navigationController = [UINavigationController alloc] initWithRootViewController:viewController];
} else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
navigationController = [UINavigationController alloc] initWithRootViewController:viewController];
}
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
NB: You could clean the above code up a little, but you get the picture, which is that you have to set the root view controller to be a navigation controller, like this:
self.window.rootViewController = navigationController;
You could also make a custom root view controller if you like, but the above will let you achieve what you're trying to do - UINavigationController will be used as the navigation system throughout your app.
Your navigation controller is nil. So replace your didFinishLaunchingWithOptions method with
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
}
UINavigationController *navcontroller = navigationController = [UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navcontroller;
[self.window makeKeyAndVisible];
return YES;
}
Now you have a navigation controller so the call to push method on the self.navigationController should push the second controller.
Please try
- (IBAction)Actionlogin:(id)sender {
NSLog(#" login button has been pressed");
NSLog(#"In init");
test_details *aSecondPageController=[[test_details alloc]initWithNibName:#"test_details_iPhone" bunndle:nil];
[self.navigationController pushViewController:aSecondPageController animated:NO];
}
// test_details_iPhone.xib and test_details_iPad.xib
//Change initWithNibName#"test_details" with test_details_iPhone or test_details_iPad

login view does not load before main ViewController in Xcode 4

Here is the layout of my app.
ApplicationName
LoginViewController.h
LoginViewController.m
LoginView.xib
AppDelegate.h
AppDelegate.m
ViewController.h
ViewController.m
ViewController_iPhone.xib
ViewController_iPad.xib
Currently in my AppDelegate.m I have:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
LoginViewController *_loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginView" bundle:[NSBundle mainBundle]];
self.loginViewController = _loginViewController;
[_loginViewController release];
[_window addSubview:[loginViewController view]];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
} else {
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil] autorelease];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
My LoginView.xib has it's File's Owner defined as LoginViewController.
I was at first getting an error stating: reason: '-[UITableViewController loadView] loaded the "LoginView" nib but didn't get a UITableView.'"
I changed UITableViewController to UIViewController and I was able to run the app without an error. The only problem now is that my LoginViewController does not load. I see the blank grey ViewController_iPad.xib loading.
What am I missing here?
I can post up any other code that would be useful.
Thanks in advance!
You should be setting your window's root view controller to self.loginViewController.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginView" bundle:[NSBundle mainBundle]];
[self.loginViewController release];
self.window.rootViewController = self.loginViewController;
[self.window makeKeyAndVisible];
return YES;
}

Resources