Nib Load Failed - iphone

In my application, I made custom tab bar having 5 tabs, each tab shows different UIViewController.
Application is for iPhone only, so i made 2 NIBs for each UIViewController (if class name is DayView, NIBs are DayView_iPhone and DayView_iPhone5). Everything is working fine for up to 10 mins in the device as well as in simulator.
After that app is crashing showing this in console :
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/kalyanasadinagarajugari/Library/Application Support/iPhone Simulator/6.1/Applications/0DEBB118-BA67-440F-BA70-79ED41AC9134/CalendarBlender.app> (loaded)' with name 'DayView_iPhone''
I checked the NIB names also, every NIB file name is correct.
And my code is
NSString *nibName = [AppDelegate fetchNibWithViewControllerName:#"DayView"];
dayView = [[DayView alloc] initWithNibName:nibName bundle:nil];
if (IS_IPHONE_5)
dayView.view.frame = CGRectMake(0, 44, 320, 463);
else
dayView.view.frame = CGRectMake(0, 44, 320, 375); dayView.view.tag=2; [self.view
addSubview:dayView.view];

Try to check the Nibname (Case Sensitive) and clean the project,re run it . Did you changed the class name for all the nibs?

Use this:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
tabBarController = [[UITabBarController alloc] init];
MyViewController* vc1 = [[MyViewController alloc] initWithNibName:#"nibName" bundle:nil];
MyOtherViewController* vc2 = [[MyOtherViewController alloc] initWithNibName:#"nibName" bundle:nil];
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
tabBarController.viewControllers = controllers;
window.rootViewController = tabBarController;
}
And you can add controllers dynamically too by adding controllers in tabBarController's array of viewControllers.

Related

Multiple ViewControllers onscreen

I would like to place two UIViewControllers on a window (like you can see below). Is it possible to set this up to each view is controlled by it's own controller respectively?
In the app delegate I'm adding the subview the following way
TabBarViewController *tabBarVC = [[TabBarViewController alloc] init];
CGRect frame = tabBarVC.view.frame;
frame.origin.y = self.window.bounds.size.height - frame.size.height;
tabBarVC.view.frame = frame;
[self.window insertSubview:tabBarVC.view aboveSubview:tabVC.view];
It loads, but when I click on a button (IBAction), or do anything which needs the controller, the app crashes. What am I doing wrong?
Thanks!
Create a strong reference for an iVar or property for tabBarVC and change this line:
TabBarViewController *tabBarVC = [[TabBarViewController alloc] init];
To:
tabBarVC = [[TabBarViewController alloc] init];
Or
self.tabBarVC = [[TabBarViewController alloc] init];
Yes you can..! have the controllers you want to display as iVars of the view controller you want to display in. Then just use,
[MainController addSubview:desiredConroller1.view];
[MainController addSubview:desiredConroller2.view];
You can load the controller from the storyboard using,
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboard_name" bundle:nil];
desiredController1 = [membershipStoryboard instantiateInitialViewController];
you can also use instantiateViewControllerWithIdentifier:

iOS5 setHidden UITabBarItem causing crash

I'm using Storyboards in xcode with iOS5. I have a TabBarController with 6 tabs. Prior to the TabController a user selects a type of account A oR B, if type B is selected I would like to hide one of the tabs.
I have a subclass of UITabBarController and this piece of code works but its not quite what I want.
if (accountType == 2) {
[[[[self tabBar] items] objectAtIndex:1] setEnabled:NO];
}
This makes my second tab dark and unusable which is ok, but I really wanted this to work...
[[[[self tabBar] items] objectAtIndex:1] setHidden:YES];
But it causes this error: -[UITabBarItem setHidden:]: unrecognized selector sent to instance 0x856f490
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarItem setHidden:]: unrecognized selector sent to instance 0x856f490'
Is there another way of achieving this?
Why not waiting with the initialization of the tabBar viewControllers until you know which type of Account your user selects? To do so use the setViewControllers:animated:method for e.g. as followed:
if (accountType == 1) {
NSArray* controllersForTabBar = [NSArray arrayWithObjects:myVC1,myVC2,myVC3,myVC4,myVC5,myVC6 nil];
[[[self tabBar] setViewControllers:controllersForTabBar] animated:YES];
}
if (accountType == 2) {
NSArray* controllersForTabBar = [NSArray arrayWithObjects:myVC1,myVC2,myVC3,myVC4,myVC5, nil];
[[[self tabBar] setViewControllers:controllersForTabBar] animated:YES];
}
The apple doc for this method says:
When you assign a new set of view controllers runtime, the tab bar
controller removes all of the old view controllers before installing
the new ones. When changing the view controllers, the tab bar
controller remembers the view controller object that was previously
selected and attempts to reselect it. If the selected view controller
is no longer present, it attempts to select the view controller at the
same index in the array as the previous selection. If that index is
invalid, it selects the view controller at index 0.
Regarding your error message: You get that error because the tabBar doesn't implement a method setHidden:.
d.ennis answer pointed me in the right direction. Have to tweak it slightly for ios5 with Storyboards...
// load the storyboard by name
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
if (accountType == 1) {
UIViewController *fvc = [storyboard instantiateViewControllerWithIdentifier:#"First"];
UIViewController *svc = [storyboard instantiateViewControllerWithIdentifier:#"Second"];
} else {
UIViewController *fvc = [storyboard instantiateViewControllerWithIdentifier:#"First"];
UIViewController *svc = [storyboard instantiateViewControllerWithIdentifier:#"Second"];
UIViewController *tvc = [storyboard instantiateViewControllerWithIdentifier:#"Third"];
}
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
NSArray *controllersForTabBar = [NSArray arrayWithObjects: fvc, svc, nil];
[tabBarController setViewControllers:controllersForTabBar animated:NO];
[self.view addSubview:tabBarController.view];

How can i load nib files of two different classes in the splitviecontroller?

I was stared with Viewbased application.
In my app i need to use splitview controller.
for this purpose i dragged a splitviewcontroller in to xib.
for the two views of splitview controller i was creaed two new
classes
1.DetailsViewController & 2.RootViewController .
Now i want to load the xib files to the two views of splitview controller.
my code as follows
DetailsViewController *details = [[DetailsViewController alloc] init];
RootViewController *root = [[RootViewController alloc] init];
splitViewController.viewControllers = [NSArray arrayWithObjects:details, root, nil];
// self.view = splitViewController.view;
[self.view addSubview:splitViewController.view];
[details release];
[root release];
My previous question regarding this problem:
how can i add splitview to my viewbased app in ipad coding
Instead of
[[MyVC alloc] init]
use
[[MyVC alloc] initWithNibName:#"MyNibNameWithoutExtension" bundleOrNil:nil];

rootViewController property does not work on iOS 5 project with xcode 4.2

Hi I just started experimenting on iOS 5. I created a project without storyboard and trying to add views programmatically (no use of interface builder at all). I have following code but rootViewController property of the window does not seem to work. I did NSLog on self.tabController and it shows me value(not null) but on the other side when after self.window.rootViewController = self.tabController, i output self.window.rootViewController it gives me null in console.
I have been struggling with this issue for a long time now. Any help would be appreciated.
Following is my didFinishLaunching method:
self.dataSource = [[[ADJWebDataSource alloc] init] autorelease];
ADJBrowseListingsViewController *browseListingsVC = [[ADJBrowseListingsViewController alloc] init];
ADJSecondViewController *secondVC = [[ADJSecondViewController alloc] init];
tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
navController = [[UINavigationController alloc] initWithRootViewController:browseListingsVC];
NSMutableArray* viewControllers = [[NSMutableArray alloc] initWithCapacity:2];
[viewControllers addObject:browseListingsVC];
[viewControllers addObject:secondVC];
[navController release];
[browseListingsVC release];
[secondVC release];
tabBarController.viewControllers = viewControllers;
[viewControllers release];
browseListingsVC.dataSource = self.dataSource;
NSLog(#"controller %#", self.tabBarController);
self.window.rootViewController = self.tabBarController;
NSLog(#"controller1 %#", self.window.rootViewController);
[self.window makeKeyAndVisible];
return YES;
Thanks
Vik
When you are using story board, why are you still creating objects for view controllers?
You can directly prepare the flow of your views in story board, add necessary segues etc.
If your view controller is floating (without any segues), you have to use the method "instantiateViewControllerWithIdentifier" in story board class.
For a view controller if you want to add navigation in story board, select the view controller, go to menu "Editor"->"Embed in" and select navigation controller. It will add navigation controller to your view controller.
Figured it out with the help of Firoze. Actually, I had to allocate and initialize self.window programmatically. I was confused as I never had to do that in iOS 4 or earlier. But then I just realized prior to iOS5, every project has a MainWindow.xib which had self.window allocated and initialized, now if I am not using storyboard in iOS5, there is no .xib file, I needed to allocate and initialize it myself in the code

Right design pattern for tabbed navigation views?

I've been stuck trying to puzzle this out for a couple days now, and I'll admit I need help.
The root view controller of my application is a tab bar controller. I want to have each tab bar a different navigation controller. These navigation controllers have completely different behavior.
So how do I set this up in terms of classes? Per Apple's documentation, I'm not supposed to subclass UINavigationViewController. So where do I put the code that drives each of these navigation controllers? Does it all get thrown in App Delegate? That would create an impossible mess.
This app should run on iOS 4.0 or later. (Realistically, I can probably require iOS 4.2.)
This is taken from one of my applications. As you say, you are not supposed to subclass UINavigationController, instead you use them as they are and you add viewcontroller on the UINavigationController's. Then after setting the root viewcontroller in each UINavigationController, you add the UINavigationController to the UITabBarController (phew!).
So each tab will "point" to a UINavigationController which has a regular viewcontroller as root viewcontroller, and it is the root viewcontroller (the one you add) that will be shown when a tab is pressed with a (optional) navigationbar at top.
UITabBarController *tvc = [[UITabBarController alloc] init];
self.tabBarController = tvc;
[tvc release];
// Instantiates three view-controllers which will be attached to the tabbar.
// Each view-controller is attached as rootviewcontroller in a navigationcontroller.
MainScreenViewController *vc1 = [[MainScreenViewController alloc] init];
PracticalMainViewController *vc2 = [[PracticalMainViewController alloc] init];
ExerciseViewController *vc3 = [[ExerciseViewController alloc] init];
UINavigationController *nvc1 = [[UINavigationController alloc] initWithRootViewController:vc1];
UINavigationController *nvc2 = [[UINavigationController alloc] initWithRootViewController:vc2];
UINavigationController *nvc3 = [[UINavigationController alloc] initWithRootViewController:vc3];
[vc1 release];
[vc2 release];
[vc3 release];
nvc1.navigationBar.barStyle = UIBarStyleBlack;
nvc2.navigationBar.barStyle = UIBarStyleBlack;
nvc3.navigationBar.barStyle = UIBarStyleBlack;
NSArray *controllers = [[NSArray alloc] initWithObjects:nvc1, nvc2, nvc3, nil];
[nvc1 release];
[nvc2 release];
[nvc3 release];
self.tabBarController.viewControllers = controllers;
[controllers release];
This is how I go from one viewcontroller to another one (this is done by tapping a cell in a tableview but as you see the pushViewController method can be used wherever you want).
(this is taken from another part of the app)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.detailedAnswerViewController == nil) {
TestAnsweredViewController *vc = [[TestAnsweredViewController alloc] init];
self.detailedAnswerViewController = vc;
[vc release];
}
[self.navigationController pushViewController:self.detailedAnswerViewController animated:YES];
}
The self.navigationcontroller property is of course set on each viewcontroller which are pushed on the UINavigationController hierachy.