Unrecognized selector error when pushing segue in Objective C - ios

I have an app which has a sidebar menu (a UITableView) with different rows that are connected separately with the main view controller with a (push) Segue.
Only one of this doesn't work, so when I tap on it I recieve this message:
**** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainTabBarController topViewController]: unrecognized selector sent to instance 0x14880ce00'terminating with uncaught exception of type NSException*
In the SideBarViewController this is the part of the code that causes the crash:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(#"[segue identifier]: %#",[segue identifier]);
if ([[segue identifier] isEqualToString:#"sync"]) {
MainViewController *mainViewSync= (MainViewController *)[[segue destinationViewController] topViewController];
mainViewSync.sync = 1;
}}
How can I fix That?

Related

Issues with rootViewController and Home Screen Quick Actions

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.

Can't set data to an array in prepareForSegue

I'm using Objective-C, I want to copy an array for another view controller and push it. Here is my code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"comment"]){
commentViewController *vc = segue.destinationViewController;
vc.comments = [[NSArray alloc]initWithArray:comments];
}
}
But it doesn't work. I got this in Xcode:
2015-12-06 16:27:45.802 net[1244:29117] -[UIViewController setComments:]: unrecognized selector sent to instance 0x7ff8d34eb720
2015-12-06 16:27:45.807 net[1244:29117] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController setComments:]: unrecognized selector sent to instance 0x7ff8d34eb720'
Someone can help me?
Select your destination view controller in the storyboard. And in the identity inspector make sure that your destination vc is your custom class as seen in the pic.

Add Value to property on ViewController from AppDelegate

I have the following code:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
[tabBarController performSegueWithIdentifier:#"push_display_incomming_call" sender:self];
This shows me the ViewController I want to see correctly. But now I want to change the property's value of the ViewController is being Segue before it's shown, I'm Trying the following with no success.
incommingCallViewController *mainController = (incommingCallViewController*) tabBarController;
mainController.name.text = #"Eddwn Paz";
This is the stacktrace from xCode Console:
2014-07-10 16:46:09.413 mobile-app[9354:60b] -[OptionsTabBarViewController name]: unrecognized selector sent to instance 0x17552540
2014-07-10 16:46:09.414 mobile-app[9354:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[OptionsTabBarViewController name]: unrecognized selector sent to instance 0x17552540'
Assume that push_display_incomming_call segue presents IncommingCallViewController view controller as modal. So you should use code:
IncommingCallViewController *callVC =
(IncommingCallViewController *)tabBarController.presentedViewController;
callVC.name.text = #"Eddwn Paz";
Remarks:
You receive the error because you try to use tabBarController as instance of incommingCallViewController while it is instance of UITabBarController thus it does not have required name property.

UITableViewController crashes after cell selected

I have an UITableViewController that Ive pulled out in story board and made a custom UITableViewController class to control it. The table is loaded correctly but when a cell is selected a segue is supposed to be preformed to a ViewController but instead the app crashes and I get this:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "lZs-yn-NHN-view-Nia-cR-yrl" nib but didn't get a UITableView.'
The UITableViewController is preceded by a tabViewController and a NavigationController in story board if that is affecting anything. This code runs but after this the app crashes:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"DisplayMovieFromAllMovies"]) {
NSIndexPath * indexPath = [self.tableView indexPathForCell:sender];
NSString * sectionHeader = [self.headerArray objectAtIndex:indexPath.section];
NSDictionary * selectedMovieDict = [[self.movieSortedDict objectForKey:sectionHeader] objectAtIndex:indexPath.row];
NSLog(#"selected: %#", [selectedMovieDict objectForKey:#"title"]);
[segue.destinationViewController setMovieDict:selectedMovieDict];
}}
Maybe I wired something incorrectly up in storyboard.

Why is my app crashing when I try to set the value of this variable in prepareForSegue?

The gist of what I'm trying to do is set the value of a label on the window I'm segueing to the text that the user entered on the previous window.
The user segues by clicking "Read", which segues them.
Here's my prepareForSegue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
ReadingViewController *destination = segue.destinationViewController;
destination.textToRead = self.textInput.text;
}
textToRead is just an NSString object that holds the text that the user enters. (This text is then set as a label with the viewDidLoad method.)
The window that is segued to is set as a different view controller as expected (ReadingViewController) and I created the segue by control-dragging from the "Read" UIButton to the next window.
I can't seem to figure out what the problem is. Here's the error it gives:
2013-03-13 19:00:08.119 Project Lego[1523:c07] -[UINavigationController setTextToRead:]: unrecognized selector sent to instance 0x894d230
2013-03-13 19:00:08.122 Project Lego[1523:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setTextToRead:]: unrecognized selector sent to instance 0x894d230'
*** First throw call stack:
(0x1c92012 0x10cfe7e 0x1d1d4bd 0x1c81bbc 0x1c8194e 0x2949 0x45bb87 0x45bc14 0x10e3705 0x172c0 0x17258 0xd8021 0xd857f 0xd76e8 0x46cef 0x46f02 0x24d4a 0x16698 0x1beddf9 0x1bedad0 0x1c07bf5 0x1c07962 0x1c38bb6 0x1c37f44 0x1c37e1b 0x1bec7e3 0x1bec668 0x13ffc 0x244d 0x2375 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)
If you want to access the view controller embedded in a navigationVC, do something like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UINavigationController *nav = (UINavigationController*)segue.destinationViewController;
ReadingViewController *destination = [nav.viewControllers objectAtIndex:0];
destination.textToRead = self.textInput.text;
}
Your segue is referencing the UINavigationController, not your viewcontroller class. Check your Storyboard to make sure the segue is connecting the right controllers.
You seem to be setting textToRead on the Navigation controller. I am assuming you want to set this in the view controller. If so you need to
if ([segue.destinationViewController
isKindOfClass:[UINavigationController class]])
{
mvc = (ReadingViewController *) [segue.destinationViewController topViewController];
mvc.textToRead = self.textInput.text;
} else {
ReadingViewController *destination = segue.destinationViewController;
destination.textToRead = self.textInput.text;
}

Resources