segue not working causing app to crash - ios

my code is breaking on the segue. the segue was modal at first , i tried to change it to push but its still not working my code is the following :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([[segue identifier ] isEqualToString:#"detailLoanSeg"]){
detailLoan *theController=[segue destinationViewController];
theController.time=_time;
theController.cuurLoan=_cuurLoan;
theController.currName=_currName;
}
}
and the error in xcode is :
014-05-07 04:38:11.257 LoanCalc[1561:a0b] -[UIViewController setTime:]:
unrecognized selector sent to instance 0xa05f1f0
2014-05-07 04:38:11.266 LoanCalc[1561:a0b] *** Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason: '-[UIViewController setTime:]:
unrecognized selector sent to instance 0xa05f1f0'

It looks like, in storyboard, you forgot to indicate the custom class of the destination view controller. In storyboard, select that destination VC, click the third inspector from the right and set it's Custom Class to "detailLoan" (which violates class naming convention).
The error is that your destination vc is a generic UIViewController, which naturally doesn't implement the setTime: method.

Are you sure the destination class is your View Controller and not a UINavigationController? Put a breakpoint and check what theController class is. Also make sure that the view controller in the Storyboard has its class set from the default UIViewController to your custom subclass. It's almost certainly one of those two issues.

Related

A problem setting a value of a class that is being segued to in Objective-C

I have 2 view controllers and a segue between them that I trigger programmatically.
The first view controller has some data stored in self.myCustomProperty that it needs to pass to the 2nd controller. The second view controller with class name MyTableViewController, which inherits from UITableViewController, has a property customData which implies also the corresponding setter method setCustomData:somedata.
The first view controller performs the segue on a certain event:
[self performSegueWithIdentifier:#"MyCustomSegue" sender:self];
which starts this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if( [segue.identifier isEqualToString:#"MyCustomSegue"] ){
MyTableViewController *destinationController = segue.destinationViewController;
[destinationController setCustomData:self.myCustomProperty];
}
}
The problem is that [destinationController setCustomData:self.myCustomProperty] causes:
'NSInvalidArgumentException', reason: '-[UITableViewController setCustomData:]: unrecognized selector sent to instance 0x...
After inspecting the problem I noticed that the destinationController variable has the class UIViewController class instead of MyTableViewController. I suppose the exception is thrown because UIViewController does not have the customData property.
I have searched for some time on the Internet but neither did I find mention of such problems nor did I find a way to turn UIViewController into MyTableViewController. The only method to do the latter that I have found is object_setClass(id obj, Class cls), but using it can cause serious problems.
How can I solve the issue?
Your error says:
'NSInvalidArgumentException', reason: '-[UITableViewController setCustomData:]: unrecognized selector sent to instance 0x...
Note that the class name in this error message is UITableViewController, not the name for your specific class. This is a tell-tale sign that you have neglected to set the base class for your storyboard scene. It's using the default UITableViewController rather than your specific subclass. And the base UITableViewController obviously doesn't know what to make of the setCustomData method.
Set the base class for your storyboard scene, and it should fix the issue.

starting a table view controller

I am trying to push a table view controller that I have on my storyboard from a view controller that I don't have in my storyboard. I get these errors when i get to initializing the first table view cell. I've made sure the table view controller and the table view cell are set to the correct class types that I've defined and I've set the correct identifier for the cell.
Error Occurs at Line:
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ListPrototypeCell" forIndexPath:indexPath];
Error:
*** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:5439
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier ListPrototypeCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
What am I missing?
P.S that other link does not have the answer, I need to initialize multiple cells of this type, I need the index for different data.
Why not just put all the view controllers in one storyboard?
If you're trying to push from a UITableViewCell, you could use a segue method.
First: Control-Drag from your TableViewController to your ViewController to link them with a push segue. Name the segue. E.G. "segueName"
Create a prepareForSegue method like so:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"segueName"])
{
// do whatever you want here
}
and then under your didSelectRowAtIndexPath:, run the segue using this:
[self performSegueWithIdentifier:#"segueName" sender:nil];
Perhaps you could elaborate a bit more, if this does not work.

unrecognized selector sent to instance error when a button is clicked

I'm not entirely sure why I am getting this error. It occurs when I click the button on my FirstViewController:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FirstViewController 1ButtonPress:]: unrecognized selector sent to instance 0x10954bbd0'
My FirstViewController has a button (1ButtonPress) which performs a segue with another ViewController which has a embed NavigationController (so in my storyboard, the segue is from FirstViewController to the NavigationController). On this segue, some data is transferred to the other ViewController by:
First.m
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"abc"]) {
UINavigationController *navController = [segue destinationViewController];
SecondViewController *BVC = (SecondViewController *)(navController.topViewController);
BVC.BLLImg = [UIImage imageNamed:#"SomeImage.jpg"];
BVC.BLLTitle = #"SomeText";
}
}
The segue has the identifier "abc" in storyboard. Looking around it, I believe it has something to do with an incorrect class, but everything on first inspection looks fine. Any other reason why I would be getting this error?
Let's go through the error you saw. It says that an unrecognized selector was sent to an instance of FirstViewController.
From what I deduce, you hooked up your button to your view controller, and removed the implementation from your view controller later, assuming that it'd remove the connection you earlier created from your button to your view controller. Unfortunately, that's not how it works. You need to remove the connection from the button too.
To do that, right click on your button in the storyboard, and remove the connection which says 1ButtonPress:.
Side note: You're doing too much for a beginner to understand. Please try going through the Cocoa world step by step. Also, read up this article to learn about how to name your methods and variables in a more easy-to-understand way.
Right-click the 1ButtonPress button in your Storyboard. Remove the action to 1ButtonPress:.

IOS opening other view controller on buttonclick not working with storyboards

I am having a silly problem with opening another viewcontroller on a buttonclick.
On my viewcontroller on my storyboard, I have a button. When a users clicks on this button, a new viewcontroller should be opened. As far as I know, there are 2 ways to do this.
in code
on the storyboard itself
Both methods are not working for me.
First the problem with storyboards:
(source: livefilestore.com)
I have connected my mainViewController with a navigationController, and I have connected my button with my next view (for adding an item). However, when I run the program, I get the following error:
'NSGenericException', reason: 'Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
I really don't understand this error, because the mainviewcontroller IS managed by a UINavigationController.
So, as this didn't work, I tried to handle it in code. I connected the buttonclick action to the mainViewController.h file and implemented it in the .m file.
This is the action method when a user clicks on the button:
- (IBAction)actionBtnAdd:(id)sender {
AddInventoryViewController *addinvController = [self.storyboard instantiateViewControllerWithIdentifier:#"AddInventoryViewController"];
[self presentViewController:addinvController animated:NO completion:nil];
}
The other viewcontroller is in the same storyboard, and the ID of the other viewcontroller is "AddInventoryViewController". This gives me the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
Anybody who know the problem? What would be the best approach to solve this?
Your navigation controller is not the initial view controller. Select your nav controller on storyboard, go into Attributes Inspector and select "Is Initial View Controller".
Make your navigation controller Initial View Controller.
Then create a push-segue from button to target controller.
Then you can use following method to make some adjustments before pushing:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"TargetSegue"]) {
TargetViewController *vc = segue.destinationViewController;
//vc.items = nil;
}
}
Update:
There are more options:
If your initial view controller is tabbar: Add segue from one of the tabs to navigation controller which contains your view controller.
If your initial view controller is another navigation controller:
Remove navigation controller which contains your view controller. Then add the segue between the thirdViewController and the source from which it is presented. The you can use push segue between the button and the target view controller.
If you want to do it programmatically:
Remove navigation controller which contains your view controller. Push the thirdViewController from your currently presented navigation controller. Push the targetViewController from buttonAction.
Hope it will help :)
In your Xcode .Click the storyboard on the left side and on top select Editor->EmbedIn->Navigation controller .It will automatically add your navigation controller to the first view controller .Then it would work
You have to set the Navigation controller as the first controller
See before the Navigation controller there is the storyboard starting arrow
Under your IBAction method instead of using
[self presentViewController:addinvController animated:NO completion:nil];
use this code and check
[self.navigationController pushViewController:addinvController animated:YES];

When I use prepareForSegue to pass value to the aim controller, there are some problems

Firstly, I use storyboard to create two uiviewcontroller: firstviewcontroller and secondviewcontroller. And using xcode to embed a uinavigationcontroller into secondviewcontroller(editor --> embed in --> navigation controller). Then, I also want to use segue to pass value from first to second. there is the code:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"passValue"])
{
secondViewController *saveViewController = [segue destinationViewController];
[saveViewController setValue:data forKey:#"data"];
}
}
But there will report error, the log is:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key data.'
However, when I delete the navigation controller that I use Xcode to insert it and define the navigation bar by code, this problem will disappear. I guess the reason is the real destinationViewController is navigationcontroller instead of secondviewcontroller. So if I want to reserve the navigation controller that is embedded by Xcode, how can fix this problem?
You're right, destinationViewController will be a UINavigationController you have secondViewController embeded in. Maybe try that:
secondViewController *saveViewController = [[segue destinationViewController] topViewController];

Resources