Getting the tabBarController from root view navigation stack. When I fetch the selectedcontroller from tabBar controller, app getting crash.
TabController *TabBar = (TabController *)viewController.navigationController.presentedViewController;
UINavigationController *selectedNCinTab = (UINavigationController *)TabBar.selectedViewController;
When execute the above line getting crash. With below reason.
-[UINavigationController selectedViewController]: unrecognized selector sent to instance 0xf4b0be0
You probably need to access navigationController.topViewController instead of presentedViewController in 1st line
Related
I have the below Objective-C code to launch my app with Home Screen Quick Actions:
- (void)applyShortcutItem:(UIApplicationShortcutItem *)shortcutItem
{
ViewController *rootViewController = (ViewController *)[self.window rootViewController];
NSLog(#"Here %# - %#", rootViewController, shortcutItem);
if([shortcutItem.type isEqualToString:#"LaunchMode0"])
{
[rootViewController setShortcutAction:LaunchMode0];
}
else if([shortcutItem.type isEqualToString:#"LaunchMode1"])
{
[rootViewController setShortcutAction:LaunchMode1];
}
}
However, I keep getting runtime errors (unrecognized selected sent to instance) when I try to launch with the quick actions. Notably, it's these two lines where the app seems to trip up:
[rootViewController setShortcutAction:LaunchMode0]; and [rootViewController setShortcutAction:LaunchMode1];
It doesn't seem to like the rootViewController. Technically, the initial View Controller is the Navigation Controller, however the app launches to my main view controller (the app only has two views, the main one, and an about page).
There is more code above that code in my project, but this is the part that's giving me the error-- but let me know if seeing the other code would help (I didn't want the post to be too code heavy).
Thanks so much!
EDIT: as requested, here is the complete NSLog.
2019-09-23 18:18:01.317271-0400 AppName[1719:200993] Here <UINavigationController: 0x108802200> - <UIApplicationShortcutItem: 0x282bd1500; type: Mode1Shortcut, title: Mode 1>
2019-09-23 18:18:01.321819-0400 AppName[1719:200993] -[UINavigationController setShortcutAction:]: unrecognized selector sent to instance 0x108802200
2019-09-23 18:18:01.323462-0400 AppName[1719:200993] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setShortcutAction:]: unrecognized selector sent to instance 0x108802200'
*** First throw call stack:
(0x2045aa98c 0x2037839f8 0x2044c71c8 0x231033220 0x2045b01d4 0x2045b1e6c 0x102c14af0 0x102c148ec 0x230ffdf90 0x2308cc468 0x2308cd150 0x2308cc224 0x2308d0f24 0x230c012b0 0x206f275d8 0x102fecc78 0x102ff0840 0x206f61040 0x206f60cdc 0x206f61294 0x20453c728 0x20453c6a8 0x20453bf90 0x204536ecc 0x2045367c0 0x20673779c 0x231007c38 0x102c076d0 0x203ffa8e0)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
The problem is this:
ViewController *rootViewController = (ViewController *)[self.window rootViewController];
But you have stated that the root view controller is a UINavigationController. You need to get your ViewController from the nav controller.
UINavigationController *rootViewController = (UINavigationController *)self.window.rootViewController;
ViewController *viewController = (ViewController *)rootViewController.topViewController;
Then update the rest of the code to work on viewController.
I have the following method which functions properly when the app is in foreground. The SecondViewController gets pushed to the top of the UINavigationController's view controller stack.
I have two UIViewController's FirstViewController and SecondViewController in a UINavigationController and FirstViewController is the rootViewController of the UINavigationController
1. UINavigationController *navigationController =
(UINavigationController*) self.navigationController;
2. [self.navigationController
performSegueWithIdentifier:#"showSecondView" sender:self];
3. SecondViewController * secondController = (SecondViewController*)
navigationController.viewControllers[1];
4. [secondController performSomeAction];
After pushing the SecondViewController, I pop to UINavigationController's rootViewController which is FirstViewController. This code works fine when the app is in the foreground.
I have an iOS8 interactive notification calling the FirstViewController, which pushes the SecondViewController using performSegueWithIdentifier.
Interactive Notification correctly calls the FirstViewController but the UINavigationController controller does not push the SecondViewController(see above code), even though the code correctly when the app is in the foreground.
Some Initial Investigations I did
Segue Identifier is correct
When I put the breakpoint on the fourth line on the case where the UINavigationController does not push the SecondViewController, secondController instance is a SecondViewController.
breakpoint showed that the UINavigationController only had FirstViewController even after the performSegueWithIdentifier.
No memory leak in instruments
Any help is appreciated!
I have 1 View Controller and 1 Tab Bar Controller with 2 Navegation Controller.
In my simple View I have one form and then when I click in button "Login" I want to load the first View in my Tab Bar Controller.
With this code works perfect:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
Inicial *telaInicial = [storyboard instantiateViewControllerWithIdentifier:#"tabBar"];
[self presentViewController:telaInicial animated:YES completion:nil];
But I want to send and value to this View "Inicial" and I put this:
telaInicial.email = email.text;
But I got this error message:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController setEmail:]: unrecognized selector sent to instance 0x9fdc8f0'
The object telaInicial that you think is an instance of Inicial is actually a UITabBarController therefore you cannot set the email property on it as it doesn't exist.
I think you want something like this...
UITabBarController *tabBarController = [self.storyboard instantiateViewControllerWithIdentifier:#"tabBar"];
UINavigationController *navController = tabBarController.viewControllers[0];
Inicial *telaInicial = navController.topViewController;
telaInicial.email = // blah
It means that there isn't a setEmail method for the telaInicial object. Is that an instance of a subclass you've written? If so, you might be getting the error because you haven't written a setEmail method for it or because you haven't declared the instance variable email as an #property, in which case the setter and getter methods for it would be synthesized automatically by XCode (prior to XCode 4.something, I think you need to synthesize your properties explicitly - after that version, they're automatic).
Try to write class, that will store your data.
You can access to it from AppDelegate (not really good solution, but very fast).
Before anything and everything, I know there exists a similar thread by another user, I tried the code answered in that but it didnt worked for me as my VCs and storyboard is a bit different, thus asking same question with my setup and parameters
My storyboard and app looks like this, my initial rootViewController as a tabBarController.
I am using this PKRevealController to add a slider left menu bar
https://github.com/pkluz/PKRevealController/blob/master/Documentation/USAGE.md
I added the following code (taken from the answer on similar question I found on SO) to my appDelegate's didFinishLaunchingWithOptions method
PKRevealController *revealController = (PKRevealController *)self.window.rootViewController;
UIViewController *leftViewController = [[GDmenuViewController alloc] init];
UIViewController *frontViewController = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"tabBarCtrl"];
[revealController setLeftViewController:leftViewController];
[revealController setFrontViewController:frontViewController];
GDmenuViewController is the UITableViewController class I made for my UITableView menu to use on left
tabBarCtrl is the StoryBoardID for the tabBarController I set
Upon compilation I am getting the following error
2014-03-16 18:18:06.659[3595:70b] -[UITabBarController setLeftViewController:]: unrecognized selector sent to instance 0xcf565a0
2014-03-16 18:18:06.662[3595:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController setLeftViewController:]: unrecognized selector sent to instance 0xcf565a0'
--------Update-------------
I changed the code in app delegate to the following since my tabBarController was my initialVC
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
GDmenuViewController *leftViewController = [tabBarController.storyboard instantiateViewControllerWithIdentifier:#"leftMenu"];
PKRevealController *revealController = [PKRevealController revealControllerWithFrontViewController:tabBarController leftViewController:leftViewController];
self.window.rootViewController = revealController;
Now I am not getting that error but still my menuViewController (TableView) isnt showing up.
I can run the app and even slide to see that the PKVC is working but instead of my tableView, it just shows up a gray blank View
For anyone who comes up across similar problem, here is how I fixed and made it to work :
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
GDmenuViewController *leftViewController = [tabBarController.storyboard instantiateViewControllerWithIdentifier:#"leftMenu"];
PKRevealController *revealController = [PKRevealController revealControllerWithFrontViewController:tabBarController rightViewController:leftViewController];
self.window.rootViewController = revealController;
my TabBarController is my rootViewController as well as the entry point
my leftMenu is the swipe menu TableViewController with a custom class GDmenuViewController
PS: If the swipe works but you dont see your swiped menuVC as expected (grey in my case), check every thing in identityInspector of your menuVC, most probably problem will be there.
In my case, my cells (static) were buggy, so I had to delete all cells and readd them.
I know there are lots of postings about this but I have tried everything and nothing has worked. So I have tried to pass an object between two view controllers, to a DBKIngredientsViewController embedded in a navigation item. I have a push segue with the identifier "showIngredientsSegue" to the DBKIngredientsViewController. The error message I receive is:
'NSInvalidArgumentException', reason: '-[DBKIngredientsViewController topViewController]: unrecognized selector sent to instance 0x8a92450'
The view controller to which I am segueing is embedded in a navigation controller, which I think is messing it up. What's the way around this? To be clear, the DBKViewController is already embedded in a navigation controller, and the push segue pushes the DBKViewController, not the navigation controller embedding it. I have tried it different ways but none seem to work.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"showIngredientsSegue"]){
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
DBKIngredientsViewController *controller = (DBKIngredientsViewController *)navController.topViewController;
controller.targetRecipe = selectedRecipe;
}
}
Are you sure that segue.destinationViewController is a UINavigationController? It seems like it's just a DBKIngredientsViewController so this should work:
DBKIngredientsViewController *controller = (DBKIngredientsViewController *)segue.destinationViewController
Also if DBKViewController already has a navigation controller then you do not need a second one if you are pushing DBKIngredientsViewController. You would only need a second one if you are modally displaying DBKIngredientsViewController.