AppDelegate and .xib are not implementing properly, but build is successful? - ios

I really don't know how to explain this without pasting all my code, but ill give it a shot. "Assuming" my .hs and .ms are accurate, i have a feeling my .xib is not set correctly, but i cant really paste the code from that. Instead i've zipped the files and uploaded the source code. (if you are brave enough, it's here: http://bit.ly/ZtDkGi ) Im getting a successful build, but my emulator's screen is just black after the app launches.
Essentially, i had to manually add an appDelegate object. i set the class to the appropriate class - but its still not pulling. If someone would be kind enough to help, that would be great.
here's my Test_TableViewAppDelegate.h
#import <UIKit/UIKit.h>
#interface Test_TableViewAppDelegate : NSObject <UIApplicationDelegate>
{
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
#end
here's my new Test_TableViewAppDelegate.m
#import "Test_TableViewAppDelegate.h"
#implementation Test_TableViewAppDelegate
#synthesize window=_window;
#synthesize navController=_navController;
- (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];
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.backgroundColor = [UIColor greenColor];
self.window = window;
UIViewController *fvc = [[UIViewController alloc] init];
UIViewController *rootController = [[UIViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:rootController];
//UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:fvc];
self.navController = nc;
//[self.window addSubview: nc.view];
//[self.window makeKeyAndVisible];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
return YES;
}
RootViewController.h
#import <UIKit/UIKit.h>
#interface RootViewController : UITableViewController {
NSMutableArray *petsArray;
}
#end
RootViewController.m
#import "RootViewController.h"
#interface RootViewController ()
#end
#implementation RootViewController
and last but not least, main.m ( i think this might be an issue too)
#import "Test_TableViewAppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([Test_TableViewAppDelegate class]));
}
}
thanks in advance. i'd appreciate it :D

in your delegate Test_TableViewAppDelegate
why you adding views two times to the window?
// you could remove these two lines
[self.window addSubview: nc.view];
[self.window makeKeyAndVisible];
//keep these two lines
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
And this view you are adding to the navigationController it is not initalized with any nib name
UIViewController *fvc = [[UIViewController alloc] init];
initialization should be like this instead in your delegate
RootViewController *rootController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:rootController];

I believe the reason that you're getting a black screen is that you are not properly allocating and initializing your navigation controller!
Instead, you should try this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// create the base window
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.backgroundColor = [UIColor greenColor];
self.window = window;
[window release];
// this is the home page from the user's perspective
FirstViewController *fvc = [[FirstViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:fvc];
self.navigationController = nc;
[fvc release];
[nc release];
// show them
[self.window addSubview: nc.view];
[self.window makeKeyAndVisible];
return YES;
}
Hope this works!

Related

appDelegate set rootViewController and viewWillAppear is not called?

this is demo ,I set RootViewController in didFinishLaunchingWithOptions,but viewController's viewWillAppear is not called! I guess it's because the NavigationController added the ViewController that the response chain of the message failed?
I know it should be set to
self.window.rootViewController = navigationController
But I want to know what I set to
self.window.rootViewController = mainViewController
Why not call mainViewController's viewWillAppear? Thank you.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
ViewController *mainViewController = [[ViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
self.window.rootViewController = mainViewController;
[self.window makeKeyAndVisible];
return YES;
}
viewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(#"viewWillAppear");
}
#end
Hm... interesting.
Keeping the following:
[[UINavigationController alloc] initWithRootViewController:mainViewController];
even though it's not used, seems to be stealing the ViewController's viewDidAppear.
If you comment that line then it works as you want.
i.e:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
ViewController *mainViewController = [[ViewController alloc] init];
//Comment the navigationController instantiation
//UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
self.window.rootViewController = mainViewController;
[self.window makeKeyAndVisible];
return YES;
}
Maybe the navigationController was set to own the ViewController but since it's not presented on the screen, neither the viewDidLoad nor viewDidAppear occurs until later, but... when you reassign the ViewController to the window, the internal view loading logic feels viewDidAppear is no longer needed to be called.
Maybe a bug or intentional, I don't know.
Note: I don't really have a reason for this behavior. I am just shedding light on what caused it.

Changes made to Navigation Controller are not shown

I copied the example from View Controller Catalog for iOS made some changes to the colour and expected to see them reflected on the simulator. Nothing happens ???
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIViewController *myViewController = [[MyViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
[navigationController setNavigationBarHidden:NO animated:YES];
navigationController.title = #"Hello";
navigationController.navigationBar.barStyle = UIBarStyleBlack ;
navigationController.navigationBar.translucent = NO;
navigationController.navigationBar.tintColor = [UIColor blackColor];
navigationController.navigationBar.barTintColor = [UIColor greenColor];
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = navigationController;
[window makeKeyAndVisible];
return YES; }
What do I do wrong?
Several things: First: be sure than in the General information of your target app, in Main Interface field is blank. (If you donĀ“t find it, delete all *.storyboard files that you have).
Second: In your AppDelegate.h should be this property:
#property (strong, nonatomic) UIWindow *window;
And last: Change this in your code:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
In order to know changes is better step by step. (.title is in ViewController).

EXC_BAD_ACCESS when adding UINavigationController in code

I have inherited a large code that keeps crashing during applicationDidFinishLaunchingWithOptions on [self.window makeKeyAndVisible] with EXC_BAD_ACCESS (code=2, address...), so I have no useful information on console. Through elimination I have separated a simple example below that will crash when adding navigationController view onto the baseVC.view. Can anybody please help and explain why is it crashing and how to fix it?
#interface ViewController () <UINavigationControllerDelegate>
#property (nonatomic, strong) UIViewController *baseVC;
#property (nonatomic, strong) UINavigationController *customNC;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.baseVC = [[UIViewController alloc] init];
CGSize size = self.view.bounds.size;
self.baseVC.view.frame = CGRectMake(0.0, 0.0, size.width, size.height );
[self.view addSubview:self.baseVC.view];
self.customNC = [[UINavigationController alloc] initWithRootViewController:self.baseVC];
[self addChildViewController:self.customNC];
[self.customNC setNavigationBarHidden:YES animated:NO];
self.customNC.delegate = self;
self.customNC.view.frame = self.baseVC.view.frame;
[self.baseVC.view addSubview:self.customNC.view];
}
#end
The actual code is I have is more complex, but the behaviour of this sample is the same. Thank you.
Edit:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[RootVC alloc] initWithNibName:#"RootVC" bundle:nil];
[self.window makeKeyAndVisible];
return YES;
}
self.customNC = [[UINavigationController alloc] initWithRootViewController:self.baseVC];
[self.baseVC.view addSubview:self.customNC.view];
The problem is in this code lines. So, self.baseVC.view is in self.customNC.view, and vice versa. It causes the crash.
EXC_BAD_ACCESS
This exception occurs when you have problem with memory of the app probably something is already released and you try to use it. To debug this use iOS instruments.
Try to add this line to if you do not have this didFinishLaunchingWithOptions:
[self.window addSubview:[self.navigationController view]];
[self.window makeKeyAndVisible];

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 Xcode 4 UINavigationController

My program was working perfectly in iOS 4/Xcode 3. I recently upgraded to the newest version Xcode 4/iOS 5. I get a "SIGABRT" on the following line:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
This line is in the application did finish launching in the delegate. Here is some sample code:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
rootViewController = [[MyCustomViewController alloc] initWithStyle:UITableViewStylePlain];
rootViewController.window = window;
window.rootViewController = rootViewController;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
Any help is appreciated.
It's quite strange how do use your applicationDidFinishLaunching method.
If you wanto to add a UINavigationController as a rootViewController for your window and then initialize that navigation controller with an instance of MyCustomViewController do the following:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// code for creating a window
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
MyCustomViewController* myCustomViewController = [[[MyCustomViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:myCustomViewController] autorelease];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
}
window within your application delegate .h is like
#property (nonatomic, strong) UIWindow* window; // using ARC
or
#property (nonatomic, retain) UIWindow* window; // using not ARC
The property is also synthesised in your application delegate .m like
#synthesize window;
Some notes:
When you use window.rootViewController you don't need to call [window addSubView:someview]. It is already handled for you by iOS 4.
Are you sure that your code works in older sdks?
Hope it helps.
The "normal" way to initialize window is like this:
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = [Myclass alloc] init...
You're doing it other way round with
rootViewController.window = window;
and then
window.rootViewController = rootViewController; ???
Did that really work with the old xcode?

Resources