I'm trying to programatically create a UITabBar from a UIViewController. I am currently using this algorithm. However, I met up with a problem. May anyone tell me what I am lacking?
-(void)loadView{
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
TabControllerHelper *tabControllerHelper = [[TabControllerHelper alloc]initWithNibName:#"TabControllerHelper" bundle:[NSBundle mainBundle]];
tabControllerHelper.title = #"First";
TabControllerHelper *tabControllerHelper1 = [[TabControllerHelper alloc]initWithNibName:#"TabControllerHelper" bundle:nil];
tabControllerHelper1.title = #"Second";
//dvdInfoController.tabBarItem.image = [UIImage imageNamed:#"dvdicon.png"];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
// Set each tab to show an appropriate view controller
[tabBarController setViewControllers:[NSArray arrayWithObjects:tabControllerHelper, tabControllerHelper1, nil]];
[self.view addSubview:tabBarController.view];
}
The error I received:
2012-12-11 01:34:27.581 SampleUITabView[7134:c07] -[__NSCFType _tabBarItemClicked:]: unrecognized selector sent to instance 0x7574230
2012-12-11 01:34:27.582 SampleUITabView[7134:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType _tabBarItemClicked:]: unrecognized selector sent to instance 0x7574230'
*** First throw call stack:
You are doing some things which is "ViewController Containment". One approach could be to leave loadView alone and just let it do the basic stuff.
And in viewDidLoad do the view controller containment like
self addChildController ...
self.view addSubview:...
childController didMoveToParent ...
Because by just adding the tab bar controllers view as a subview, you are not necessarily getting the tabbar controllers behavior.
Related
Hey together,
I am calling a void with some parameters from the AppDelegate on my main view.
This is done if a push notification is received:
MainViewController *mainView = [[MainViewController alloc] init];
[mainView showPushView:pushDataObject];
The called void # the MainView doing some data operating stuff and after that it should load the pushView:
- (void)showPushView: (PFObject *)pushDataObject {
NSLog(#"Push Data object transfered %#", pushDataObject);
pushItem = pushDataObject;
//All working fine to this point
[self performSegueWithIdentifier:#"showPushObject" sender:self];
}
Now the problem is that the app is crashing at [self performSegueWithIdentifier:#"showPushObject" sender:self]; with this Error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'Receiver (<MainViewController: 0x145b80e0>) has no segue with identifier 'showPushObject''
*** First throw call stack:
(0x2e51fe83 0x3887c6c7 0x30f656d9 0xbeb11 0xb2a23 0x1745f7 0x38d610c3 0x38d610af 0x38d639a9 0x2e4ea5b1 0x2e4e8e7d 0x2e453471 0x2e453253 0x3318d2eb 0x30d08845 0xafecd 0x38d75ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
I think that there is a problem because I call the void from the AppDelegate, am I right?
Those anyone know a fix for that problem?
Thanks a lot!
Best regards from Germany :)
P.S. If I call [self performSegueWithIdentifier:#"showPushObject" sender:self]; with a button or something on the MainViewController all working fine... :/
In order for the segue to work, you need to have the storyboard loaded in the mainView when you start it that way. Try instead something like this:
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *firstViewController = [storyboard instantiateViewControllerWithIdentifier:#"kYourMainViewControllerIdentifier"];
self.window.rootViewController = firstViewController;
[self.window makeKeyAndVisible];
Remember to give an identifier to your root view controller and change it in this piece of code.
Your problem is this line:
MainViewController *mainView = [[MainViewController alloc] init];
because it means that the mainView instance doesn't have a storyboard (so it can't have any segues).
When you run it from a button the controller instance must have been created from a storyboard.
So, to fix, load the storyboard and instantiate mainView from it. Then the segue will work.
UIStoryboard *storyboard4Inch = [UIStoryboard storyboardWithName:#"Storyboard4Inch" bundle:nil];
UIViewController *mainViewController = [storyboard4Inch instantiateViewControllerWithIdentifier:#"MainViewController"];
[mainViewController showPushView:pushDataObject];
I have to instantiate in one of the view controllers.
UISplitViewController *splitViewController = [kStoryBoard instantiateViewControllerWithIdentifier:#"splitController"];
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;
splitViewController.navigationController.navigationBarHidden = YES;
splitViewController.presentsWithGesture = NO;
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:navigationController.topViewController action:#selector(swipeDetected:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
[splitViewController.view addGestureRecognizer:swipeRecognizer];
CGRect frame = splitViewController.view.frame;
frame.origin.x = 0;
frame.size.height -=100;
frame.origin.y +=100;
splitViewController.view.frame = frame;
UINavigationController *masterNavigationController = [splitViewController.viewControllers objectAtIndex:0];
MasterViewController *masterVC = (MasterViewController*)[masterNavigationController topViewController];
masterVC.currentCategory = [categoriesArray objectAtIndex:sender.tag-1];
self.navigationController.navigationBarHidden = YES;
[self.navigationController setViewControllers:[NSArray arrayWithObject:splitViewController] animated:YES];
My app crashes on this line.
[self.navigationController setViewControllers:[NSArray arrayWithObject:splitViewController] animated:YES];
It is working fine for ios 5 and ios 6.
Crash occurs only in ios 7.
This is the error.
[UINavigationController _setViewControllers:transition:animated:], /SourceCache/UIKit/UIKit-2903.2/UINavigationController.m:768
2013-10-21 18:51:37.009 TruAirSync[1723:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UISplitViewControllers are not allowed in a navigation controller!'
Found the answer.
Hope it will help others also.
We just need to use a view controller.
UIViewController *containerVC = [[UIViewController alloc]init];
[containerVC.view setFrame:splitViewController.view.frame];
[containerVC.view addSubview:splitViewController.view];
[containerVC addChildViewController:splitViewController];
[splitViewController didMoveToParentViewController:containerVC];
[self.navigationController setViewControllers:[NSArray arrayWithObject:containerVC] animated:YES];
Just create another UIViewController and add you splitviewcontroller's view to it's subview than you can use it in a UINavigationController.
YourContainerController *containerController = [YourContainerController new];
[containerController.view addSubview:splitViewController.view];
[self.navigationController setViewControllers:#[containerController] animated:YES];
The UISplitViewController should be the root view of your application window. Not sure why this was working for you in iOS 5 and 6. You can not push a UISplitViewController into a UINavigationController.
From Apple's documentation:
A split view controller must always be the root of any interface you
create. In other words, you must always install the view from a
UISplitViewController object as the root view of your application’s
window.
Just created a new project and I have 4 view controllers which I add to UINavigationController like this:
WatchViewController *first = [[WatchViewController alloc] init];
BetViewController *second = [[BetViewController alloc] init];
Settings *third = [[Settings alloc] init];
Account *forth = [[Account alloc] init];
UINavigationController *navFirst = [[UINavigationController alloc]initWithRootViewController:first];
UINavigationController *navSecond = [[UINavigationController alloc]initWithRootViewController:second];
UINavigationController *navThird = [[UINavigationController alloc]initWithRootViewController:third];
UINavigationController *navForth = [[UINavigationController alloc]initWithRootViewController:forth];
Load them into an array:
NSArray *viewArray = [[NSArray alloc] initWithObjects:navFirst, navSecond, navThird, navForth, nil];
Load the tab bar and window:
self.tabController = [[UITabBarController alloc] init];
[self.tabController setViewControllers:viewArray animated:YES];
[self.window setRootViewController:self.tabController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
All the view are just standard views. When I try and run the app it responds with :
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'
I can't figure out what I've missed. Any help?
Don't create 4 navigation controllers. Controllers that are needed to navigate through should be assigned to viewControllers property in UINavigationController via setViewControllers:animated: method.
You should create 1 NavigationController and add array of 4 UIViewControllers.
A very good example is given here: example and don't forget to look here UINavigationClass
Try this one:
WatchViewController *first = [[WatchViewController alloc] initWithNibName:#"WatchViewController" bundle:Nil];
BetViewController *second = [[BetViewController alloc] initWithNibName:#"BetViewController" bundle:Nil];
Settings *third = [[Settings alloc] initWithNibName:#"Settings" bundle:Nil];
Account *forth = [[Account alloc] initWithNibName:#"Account" bundle:Nil];
/*Your View Navigation Stuff and your viewArray*/
self.tabController.viewControllers = viewArray;
Why don't you try UINavigationController inside UITabBarController by setting controllers in xib. It worked for me.
Sorry, I had annoyingly subclassed UINavigationController instead of UIViewController. I hadn't picked up on it because if I didn't use the NavControllers the App would run fine, add the nav controllers and it broke. :(
I am facing crash with following code. The scenario is
This is my app delegate method in which i load RTC_HomeVC using UINavigationController.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
RTC_HomeVC *obj_RTC_HomeVC=[[RTC_HomeVC alloc]init];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:obj_RTC_HomeVC];
// Override point for customization after application launch.
self.window.rootViewController=nav;
[obj_RTC_HomeVC release];
[nav release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Now I want to open UINavigationController inside a parent Navigation controller. So i use a following code. The method -(IBAction)call_SectionFlow is in RTC_HomeVC.
-(IBAction)call_SectionFlow{
RTC_1_StoreDetailsVC *obj_StoreDetailsVC=[[RTC_1_StoreDetailsVC alloc]initWithNibName:#"RTC_1_StoreDetailsVC" bundle:nil];
RTC_3_EnablingWorksVC *obj_EnablingWorksVC = [[RTC_3_EnablingWorksVC alloc]initWithNibName:#"RTC_3_EnablingWorksVC" bundle:nil];
UINavigationController *navController_Sections = [[UINavigationController alloc] init];
NSArray *array_ControllerArray=[[NSArray alloc]initWithObjects:obj_StoreDetailsVC,obj_EnablingWorksVC, nil];
[navController_Sections setViewControllers:array_ControllerArray animated:FALSE]
navController_Sections.view.frame=CGRectMake(14, 40, 996,636 );
[self.view addSubview:[[[navController_Sections viewControllers] objectAtIndex:0] view]];
}
When i called this method application is crashed. This is crash log.
Crash log:
* Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:< RTC_1_StoreDetailsVC: 0x71f53a0 > should have parent view controller:< RTC_HomeVC: 0x758b310 > but actual parent is:< UINavigationController: 0x71f55d0 >'
* First throw call stack:
(0x1c9c012 0x10d9e7e 0x1c9bdeb 0x6838a 0x68739 0x6f5a3 0x67eed 0x4fc3 0x10ed705 0x24920 0x248b8 0xe5671 0xe5bcf 0xe4d38 0x5433f 0x54552 0x323aa 0x23cf8 0x1bf7df9 0x1bf7ad0 0x1c11bf5 0x1c11962 0x1c42bb6 0x1c41f44 0x1c41e1b 0x1bf67e3 0x1bf6668 0x2165c 0x1f82 0x1c45)
libc++abi.dylib: terminate called throwing an exception
So any one can tell me
What is wrong with this code? And which approach i should follow for resolving this crash ?
How to open another UINavigationController in existing UINavigationController?
Thanks.
Do not add subviews to UIWindow manually. It is not supported (or at least it does not work OK).
Use this method:
[firstNavigationVC presentViewController:secondNavigationVC animated:YES completion:nil];
What is causing crash is, that you are adding obj_StoreDetailsVC to the new navigation controller and then its view to self.view. Once a VC is child of another VC, its view must be descendant of that VC's view. Maybe you can add secondNavigationVC's view to to the view of firstNavigationVC, but that isn't how UIKit is supposed to work. Use the above method.
I am writing an iOS app; no ARC and no storyboards.
I have a view controller inside Navigation controller. It supposed to have Table view and navigation button which pushes to second view.
This is the error I get:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "ViewController" nib but the view outlet was not set.'
*** First throw call stack:
(0x1c8d012 0x10cae7e 0x1c8cdeb 0xf2f18 0xf3418 0xf3648 0xf3882 0x42a25 0x42dbf 0x42f55 0x4bf67 0x2a88 0xf7b7 0xfda7 0x10fab 0x22315 0x2324b 0x14cf8 0x1be8df9 0x1be8ad0 0x1c02bf5 0x1c02962 0x1c33bb6 0x1c32f44 0x1c32e1b 0x107da 0x1265c 0x2442 0x2375)
libc++abi.dylib: terminate called throwing an exception
(lldb)
The code in AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewCon = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
self.navCon = [[UINavigationController alloc]initWithRootViewController:self.viewCon];
self.navCon.navigationBar.tintColor= [UIColor greenColor];
self.viewCon.title= #"First View";
self.tblView = [[UITableView alloc] init];
NSMutableArray *viewArr=[[NSMutableArray alloc] init];
[viewArr addObject:self.navCon];
self.navBar = [[UINavigationBar alloc] init];
self.viewCon.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Second View" style:UIBarButtonSystemItemAdd target:self action:(nil)];
[self.window addSubview:self.navBar];
self.window.rootViewController = self.viewCon;
[self.window makeKeyAndVisible];
return YES;
}
At this stage I cannot run even the app. What might be the prob?
Best regards
loaded the "ViewController" nib but the view outlet was not set.
It seems you forgot to connect the view outlet of the view controller to your actual view in ViewController.xib file.
Your xib file has got a file's owner. This you have presumably set to be a ViewController (which I imagine is a subclass of UIViewController).
Now, if you go to the right-hand pane and show the bindings pane (the last one, with a small arrow), you will see that the view controller has got an outlet called view: drag from the small circle on its right on to the view which you have created in the xib.