Can't set NSArray in segue - ios

DBPostListViewController has a NSArray *postList that needs to be set.
I keep getting
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewController setPostList:]: unrecognized selector sent to instance 0x6e493d0
within the prepareForSegue for the setPostList function here:
NSArray *tempAry = [str componentsSeparatedByString:#"|#|"];
NSMutableArray *postArray = [[NSMutableArray alloc] initWithCapacity:[tempAry count]];
for (NSString *entry in tempAry) {
DBPost *tempPost = [[DBPost alloc] initWithString:entry];
[postArray addObject:tempPost];
}
DBPostListViewController *dest = [segue destinationViewController];
[dest setPostList:postArray];

Your error message explains it:
-[UITableViewController setPostList:]: unrecognized selector
You send that message on these lines of code:
DBPostListViewController *dest = [segue destinationViewController];
[dest setPostList:postArray];
Objective-C is dynamically typed, so even though you've declared dest to be a DBPostListViewController, it could be any Objective-C object at all. And in this case it happens to be a UITableViewController.
You forgot to set the custom class in your storyboard file, so UIKit is just making an instance of a plain UITableViewController. Fix your storyboard and re-run your app.

Related

prepareForSegue for TabBarController

I'm trying to pass data from my ViewController to TabBarController by using Objective-C. I'm trying to assign some data to "bottomTabEventList" (which is a property of my custom TabBarController class). Unfortunately, my program crashes by giving unrecognized selector instance error/warning.
In custom header of TabBarController class, named BottomTabview:
#interface BottomTabView : UITabBarController <UITabBarControllerDelegate>
#property(strong,nonatomic)EventList *bottomTabEventList;
#end
And prepareForSegue method in ViewController.m
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
BottomTabView *btw = [segue destinationViewController];
//checking
if(btw == nil)
NSLog(#"btw in viewController is nil");
else
NSLog(#"btw in viewController is NOT nil");
if(self.eventList.eventList == nil)
NSLog(#"eventList in viewController is nil");
else
NSLog(#"eventList in viewController is NOT nil"); //end of checking
btw.bottomTabEventList = self.eventList; //This is where crash appears
}
Exact crash log is:
-[ViewController setBottomTabEventList:]: unrecognized selector sent to instance 0x7fe923c6ba00
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController setBottomTabEventList:]: unrecognized selector sent to instance 0x7fe923c6ba00'
Segue is from ViewController to BottomTabView and its type is "Present Modally". I'd really appreciate if you can help/guide me. Thanks in advance.
The problem seems to be that btw is not actually of type BottomTabView and is just a UIViewController hence the crash log giving:
[ViewController setBottomTabEventList:]: unrecognized selector sent to instance
As UIViewController does't know what BottomTabEventList is.
You need to make sure that btw is actually a BottomTabView instance.
Do a little introspection and I bet it will not go into this statement:
if ([btw isKindOfClass:[BottomTabView class]]){
btw.bottomTabEventList = self.eventList;
}

Peek in iOS 9 App Crashing

I am trying to do Peek and Pop in my iOS 9 capable app. The view in question has a UITableView, so I have in my code:
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
// check if we're not already displaying a preview controller
if ([self.presentedViewController isKindOfClass:[WebViewController class]]) {
return nil;
}
// shallow press: return the preview controller here (peek)
self.webViewController = [[[WebViewController alloc] initWithNibName:#"WebViewController" bundle:[NSBundle mainBundle]] autorelease];
return self.webViewController;
}
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
// deep press: bring up the commit view controller (pop)
self.webViewController = [[[WebViewController alloc] initWithNibName:#"WebViewController" bundle:[NSBundle mainBundle]] autorelease];
[self showViewController:self.webViewController sender:self];
}
WebViewController is the ViewController I have already set up to display the content when the row of the tableview is selected. The error I get is:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFConstantString stringByAppendingString:]: nil argument'
*** First throw call stack:
(0x182160f5c 0x19764bf80 0x182160ea4 0x182fb8868 0x1001307a4 0x1876cf9ac 0x1876cf720 0x187a025f8 0x187960844 0x18796cde4 0x1876a91e4 0x182117c30 0x1821159d4 0x182115e04 0x182044dc0 0x18d4e0088 0x18771ef60 0x10014ca68 0x197e6a8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
Your log it's saying exactly what is wrong with your code:
-[__NSCFConstantString stringByAppendingString:]: nil argument'
You are performing stringByAppendingString passing a value that is nil
Also, autorelease is not used anymore if you are using ARC (it's default by now)

ios [__NSCFNumber length]: unrecognized selector sent to instance

i have created one custom cell and put button on it
and button event is like this
[cell.BtnRequest addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
and didtapbutton code is this
- (void)didTapButton:(id)sender {
WorkerDetailsVC *wvc=[self.storyboard instantiateViewControllerWithIdentifier:#"workerdetailsvc"];
NSIndexPath *indepath = [self.tblorderdata indexPathForCell:sender];
wvc.arrworkarrequest=[arrData objectAtIndex:indepath.row];
NSLog(#" arr send :%#", wvc.arrworkarrequest);
wvc.struserid = struserid;
[self.navigationController pushViewController:wvc animated:YES];
}
and on the next page i got the array arrworkarrequest and after the load all next page it will show error like this
2014-10-01 15:08:42.607 demo[2151:60b] -[__NSCFNumber length]: unrecognized selector sent to instance 0x911a860
2014-10-01 15:08:42.613 dmeo[2151:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x911a860'
Your problem is that sender is of type UIButton not UITableViewCell so it's getting stuck in the [self.tblorderdata indexPathForCell:sender] because that method call expects a type of UITableViewCell.
You need to find the Cell that the button is in. You may be able to do this using a while loop (although it's a bit horrible). Something like:
id obj = sender;
while (![obj isKindOfClass:[UITableViewCell class]]) {
obj = [sender superview];
}
[self.tblorderdata indexPathForCell:obj];
This feels like it's abusing the view hierarchy though.
This happens if you compare the length of a string directly
for example
NSString *myString = #"2";
using this statement will generate the error
if(myString.length==2)

App crashing on iPad but not simulator

My Application is crashing when I run it on the iPad but works 100% on the iPad simulator I am using Version 4.6.1 of Xcode and Version 6.1.3 on the iPad.
The problem lies where I am trying to pass the value of an int between segues
in my .h
#property (nonatomic, assign)int currentQuestion;
in my .m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"level1correct"]){
AddLevel1IncorrectViewController *incorrect = [segue destinationViewController];
incorrect.CQValue = self.currentQuestion;
}}
AddLevel1Incorrect.h
#property (nonatomic, assign)int CQValue;
AddLevel1Incorrect.m
#synthesize CQValue = _CQValue;
- (void)imageSelect{
int numItems = [arrayPath count];
NSMutableArray *left = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *right = [NSMutableArray arrayWithCapacity:numItems];
for (NSDictionary *itemData in arrayPath) {
[left addObject:[itemData objectForKey:#"L"]];
[right addObject:[itemData objectForKey:#"R"]];
}
NSLog(#" value of %d CQValue ", self.CQValue);
leftImageViewer.image = [UIImage imageNamed:left[self.CQValue]];//this is the point where the crash happens
rightImageViewer.image = [UIImage imageNamed:right[self.CQValue]];
}
The interesting thing is it does display the correct value in the NSLog in the Console as you will see at the top of the Crash Message
2013-04-03 22:50:00.404 thefyp[1506:907] value of 1 CQValue
2013-04-03 22:50:00.408 thefyp[1506:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'
*** First throw call stack:
Any ideas where I am going wrong here?
Your code is very brittle, meaning it makes a lot of assumptions about the data it's working with without verifying the data is accurate.
You never check the bounds of the array before you access it. self.CQValue is 1, but in this case the array itself is empty. so left[self.CQValue], is left[1], which is invalid.
Check arrayPath to make sure it's not empty.

IOS 6 ARC Unrecognized Selector Sent to Instance

I hope someone can help with this.
I have a UITableViewController and want to pass a value to a UIViewController called NewsArticleViewController when the tablecell is selected. I've created a segue from the tablecell to the view controller.
When I call my prepareForSegue method below:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"ShowNewsArticle"])
{
NSIndexPath *indexPath = [self._tableView indexPathForSelectedRow];
NSDictionary *article = [_articles objectAtIndex:indexPath.row];
NSString *articleID = [article valueForKey:#"id"];
NSLog(#"Trying %#", articleID);
NewsArticleViewController *detailViewController = [segue destinationViewController];
detailViewController.articleID = articleID;
}
}
The NSLog shows the NSString value correctly before the error occurs on the last line.
I get the error:
2012-12-11 23:08:41.915 My School[4689:c07] -[UIViewController setArticleID:]: unrecognized selector sent to instance 0x8088140
2012-12-11 23:08:41.916 My School[4689:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController setArticleID:]: unrecognized selector sent to instance 0x8088140'
*** First throw call stack:
(0x1800012 0x11c5e7e 0x188b4bd 0x17efbbc 0x17ef94e 0x3ca1 0x554ac7 0x554b54 0x1bc899 0x1bcb3d 0xbc3e83 0x17bf376 0x17bee06 0x17a6a82 0x17a5f44 0x17a5e1b 0x1cc87e3 0x1cc8668 0x10d65c 0x1f4d 0x1e75 0x1)
libc++abi.dylib: terminate called throwing an exception
On the destination view controller, NewsArticleViewController, I have declared this in the header:
#property(strong,nonatomic) id articleID;
And I have synthesized the property in the method. I'm using ARC, I don't know if this is the specific cause but I can't proceed until I sort this out. Thanks.
In your error message, UIViewController is reporting the "unrecognized selector" error. I suspect your storyboard has not specified your custom NewsArticleViewController for this scene. Thus, it's using the default UIViewController which obviously doesn't understand the setArticleID.
Check the "Custom Class" setting for the view controller in Interface Builder:
If the custom class has not specified, it will look like the above screen snapshot. Just fill in the class name.

Resources