Load the view when app starts - iphone

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

Related

Cant pass to another viewController,problems with navigation

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.

Build Error - Receiver type "ViewController" for instance messages is a forward declaration

I am getting 2 errors when building, they are located in the AppDelegatem file
- (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.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
line with two errors:
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
Error1:
Receiver type "ViewController" for instance messages is a forward declaration
Error2:
Receiver "ViewController" for class messages is a forward declaration
line with Alert:
self.window.rootViewController = self.viewController;
Alert:
Incompatible pointer types assigning to 'UIViewController *' from 'ViewController*'
If needed you can find text files of
ViewControllerm
ViewControllerh
AppDelegatem
Here http://ninjabreakbot.com/stack/
Project is for iOS5, I am very new to this. Please let me know what is useful with questions such as this. Or if enough has been provided, your solutions!
Thanks!
The error message: instance messages is a forward declaration typically means that you the compiler didn't know about the declaration of the class, i.e. you haven't included the proper header.
In your case write #import <ViewController.h> at the beginning of AppDelegate.m should address this compiler problem.
Check initWithNibName. Is the nib file name ViewController or another name?
write #import "ViewController.h" and
#property (strong, nonatomic) ViewController *viewController; in AppDelegate.h file
Write #synthesize viewController ; in AppDelegate.m file
.h file ::
#import <UIKit/UIKit.h>
#import "ViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#end
.m file::
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize window;
#synthesize viewController ;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
#end

screen not starting on iphone application

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.

Go to another view (UIViewController) from within UIViewController

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

Load different initial view based on application preferences?

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