I have a preference that, when set, forces my application to perform some synchronization on startup.
Can I use IB to display a different initial view based on this setting?
Is there a standard way to enable this behavior?
Assuming you have a property on your app delegate that is set during your synchronization, in the initial view controller's initWithNibNamed: method check the value synced by the app delegate and load the appropriate nib by calling [super initWithNibNamed:#"thisNibInsteadOfThatNib"];
EDIT: Show code to launch a different view depending on some condition at launch
// AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
UIViewController *firstViewController;
}
#property {nonatomic, retain} UIWindow *window;
#end
// AppDelegate.m
#import AppDelegate.h
#import ViewControllerOne.h
#import ViewControllerTwo.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
BOOL shouldLoadViewOne = \\ some value from preferences
if (shouldLoadViewOne) {
firstViewController = [[ViewOneController alloc] initWithNibName:#"ViewOneController" bundle:nil];
} else {
firstViewController = [[ViewTwoController alloc] initWithNibName:#"ViewTwoController" bundle:nil];
}
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[window addSubView:[navController view]];
[window makeKeyAndVisible];
return YES;
}
EDIT 2:
Make use of NSClassFromSting() and save the name of the firstViewController to load in the preferences.
// AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
id firstViewController;
}
#property {nonatomic, retain} UIWindow *window;
- (NSString *)firstViewControllerName;
#end
// AppDelegate.m
#import AppDelegate.h
#import ViewControllerOne.h
#import ViewControllerTwo.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSString *viewControllerName = [self firstViewControllerName];
firstViewController = [[NSClassFromString(viewControllerName) alloc] initWithNibName:viewControllerName bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[window addSubView:[navController view]];
[window makeKeyAndVisible];
return YES;
}
- (NSString *)firstViewControllerName
{
NSString *defaultViewController = #"ViewOneController";
NSString *savedFirstViewController = // string retrieved from preferences or other persistent store
if (!savedFirstViewController)
return defaultViewController;
return savedFirstViewController;
}
Related
I have a working project, one of the screen is for example ViewController 1 - tableView. i am trying to pass data and navigate to ViewController 2 after selecting a row in ViewController 1. I added this code:
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:[NSBundle mainBundle]];
dvController.selectedItem = selectedItem;
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
But its not passing to ViewController 2. I think the problem is with navigatorController.
Where should I add it? to xib file of ViewController 1? or to the mainAppDelegate - I dont want to touch it because its already working with its settings...
I declared UINavigationController *navigationController in my appDelegate
What should I check?
After some changes to to code (thanks to #Vakul Saini)
appDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.View1 = [[startSearching alloc] initWithNibName:#"StartSearching" bundle:nil];
self.currentNC = [[UINavigationController alloc] initWithRootViewController:self.View1];
//this line start the startSearching at the begining and its passing to //secondViewController
self.window.rootViewController = self.currentNC;
// but with this line its start from the original start page but the transfer of views //doesn't wroks
[self.window setRootViewController: self.viewController];
appDelegate.h
#property (strong, nonatomic) startSearching *View1;
#property (strong, nonatomic) UINavigationController *currentNC;
startSearching.m
detailCon *detailCo =[[detailCon alloc] initWithNibName: #"detailView" bundle: [NSBundle mainBundle]];
[detailCon release];
UINavigation *currentNC1 = [[AppDelagate sharedInstance] currentNC];
[currentVC1 pushViewController:detailCo animated:YES];
Check you have a UINavigationController in your Appdelegate or not. You need to make your app rootViewController is UINavigationController
Add your UINavigationController in appdelegate
Appdelegate.h file
#import <UIKit/UIKit.h>
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#property (strong, nonatomic) UINavigationController *navCon;
#end
Appdelegate.m file
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
#synthesize navCon = _navCon;
- (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];
self.navCon = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = self.navCon;
[self.window makeKeyAndVisible];
return YES;
}
Where ViewController is your firstView of app.
I am starting an iPhone application, and I can't seem to get the main screen started. Here is my delegate.h file:
#import <UIKit/UIKit.h>
#class MainViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (retain, nonatomic) MainViewController *mainViewController;
#end
and here is my implementation file(at least the beginning):
#import "AppDelegate.h"
#import "MainViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize mainViewController = _mainViewController;
- (void)dealloc
{
[_window release];
[_mainViewController];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MainViewController *aViewController = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
[self setMainViewController:aViewController];
[aViewController release];
self.window.rootViewController = self.mainViewController;
[self.window makeKeyAndVisible];
return YES;
}
When I try to run the app, just a black screen appears, and even after waiting a long time, the screen won't show the contents of my MainViewController.xib file...
Any ideas?
Main XIB of your application is MainWindow.xib. (It can be selected in project settings).
It contains app delegate, window and mainViewController (BTW, it should be better connected from XIB, not created manually in application:didFinishLaunching) connected to app delegate.
Check the connections.
I want to create a app with only one view(TestViewController.h TestViewController.m). (no Tabbar, no Navigation Bar) Don't know why after i launch the app, the screen is totally black. It seems that the app did not load the view successfully? Since if the view is loaded, the screen should be white. Am I right or not?
Here's AppDelegate.h
#import <UIKit/UIKit.h>
#class TestViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UIWindow *window;
TestViewController *testrViewController;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) TestViewController *testViewController;
#end
Here's AppDelegate.m
#import "AppDelegate.h"
#import "TestViewController.h"
#implementation AppDelegate
#synthesize window = window;
#synthesize testViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window addSubview:testViewController.view];
[self.window makeKeyAndVisible];
return YES;
}
It seems to me that you're not instatiating the class
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// add this line
testViewController = [[TestViewController alloc] init];
[.....]
}
Hope it helps
if you are creating it programmatically, then you should also instantiate window
UIWindow *aWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = aWindow;
[aWindow release];
then your ViewController
testViewController = [[TestViewController alloc] init];
and then make it visible
[self.window addSubview:testViewController.view];
[self.window makeKeyAndVisible];
did you create .xib for testViewController. If not then you have to add a subview over your testViewController. and then try. I hope it will work.
UIView *testView=[[UIView alloc]initwithFrame:CGRectMake(0,0,320,480)];
[testViewController addsubview:testView];
[self.window addSubview:testViewController.view];
[self.window makeKeyAndVisible];
After upgrading to xCode 4.2 I am getting the following warning...
Applications are expected to have a root view controller at the end of application launch
After reading as much as I could find on line about the RootViewController I am not sure whether I have created my root view controller properly. I created it a long time ago when I was first learning to program in xCode.
One question I have is it ok to name the root view controller something other than RootViewController. Every example I see now has it named RootViewController. I also see it synthesized in the app delegate like this...
#synthesize rootViewController = _rootViewController;
I do not understand what this is doing. Why not just...
#synthesize rootViewController;
In any event I changed the name of my root view controller to RootViewController and followed the example I found at cupsofcocoa.com. But even after the changes I am still getting the "...expected to have a root controller..." warning.
If someone has the time to take a look and let me know what I am missing, I have listed the the significant portions of my initialization code below.
Thanks,
John
//RootViewController.h
#import <UIKit/UIKit.h>
#interface RootViewController : UIViewController {
}
#end
.
//RootViewController.m
#import "RootViewController.h"
#import "JetLoggerAppDelegate.h"
#implementation RootViewController
#end
.
//JetLoggerAppDelegate.h my app delegate
#import <UIKit/UIKit.h>
#class RootViewController;
#interface JetLoggerAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *rootViewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
#end
.
//.m app delegate
#import "JetLoggerAppDelegate.h"
#import "RootViewController.h" //I don't think I need this here
#implementation JetLoggerAppDelegate
#synthesize window;
#synthesize rootViewController = _rootViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([launchOptions count] == 0) {
_rootViewController = [[RootViewController alloc] init];
self.window.rootViewController = self.rootViewController;
[window makeKeyAndVisible];
return YES;
}else{
[JLHelper showAlertWithTitle:#"" message:[NSString stringWithFormat:#"launchOptions: %#", launchOptions]];
}
return NO;
}
.
//main.m
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, #"JetLoggerAppDelegate");
[pool release];
return retVal;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([launchOptions count] == 0) {
_rootViewController = [[RootViewController alloc] init];
self.window.rootViewController = self.rootViewController;
**[window addSubview:_rootViewController.view];**
[window makeKeyAndVisible];
return YES;
}else{
[JLHelper showAlertWithTitle:#"" message:[NSString stringWithFormat:#"launchOptions: %#", launchOptions]];
return NO;
}
return nil;
}
Put return NO inside else statement and on the end put return nil; Hope this help.
Applications are expected to have a root view controller
Replace in AppDelegate
[window addSubview:[someController view]];
to
[self.window setRootViewController:someController];
A newbie Objective C question. Im trying to programmaticly change view from another view. But I only succeed in activating the tab button in the tabbarcontroller.
In my ViewDidLoad I have a condition that if thats not met, load the second view instead.
Is there any kind soul that can help out a Objective-C beginner? I have googled and search stackoverflow for the answer but with no luck.
FirstViewController.h
#interface FirstViewController : UIViewController {
some variables
}
some #properties
FirstViewController.m
#import "FirstViewController.h"
#implementation FirstViewController
- (void)viewDidLoad
{
if(condition is met) {
[self.tabBarController setSelectedIndex:1];
}
}
In my AppDelegate.h
#import <UIKit/UIKit.h>
#interface otpAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
AppDelegate.m
#implementation otpAppDelegate
#synthesize window;
#synthesize tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
tabBarController = [[UITabBarController alloc] init];
[tabBarController setDelegate:self];
return YES;
}
Edit!
I found a solution to my problem.
int selectedindex = 1;
self.tabBarController.selectedIndex = selectedindex;
UIViewController *tempvc = [[self.tabBarController viewControllers] objectAtIndex:selectedindex];
[self.view removeFromSuperview];
[self.view addSubview:tempvc.view];
Did the trick.
Try setting the index for the tabBarController instance of the app delegate like yourAppDelegate.tabBarController
If you are using tabBarController as IBOutlet , there is no need to allocate it in your app delegate. Please remove these code from your appDelegate method (application: didFinishLaunchingWithOptions)..
tabBarController = [[UITabBarController alloc] init];
[tabBarController setDelegate:self];