tableView segue after clicking cell is not recognized - objective-c

I have a tableViewController and when a cell is clicked, I want to record the name of the cell and pass it to a new QuestionViewController programatically. The problem I'm getting is that when the cell is clicked, I get an error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'segueToViewController''
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.name = [self.listOfNames objectAtIndex:indexPath.row];
NSLog(#"selected cell: %i, %#", (int)indexPath.row, self.listOfNames);
[self performSegueWithIdentifier:#"segueToViewController" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSLog(#"prepare for segue: %#", segue.identifier);
if ([[segue identifier] isEqualToString:#"segueToViewController"]){
QuestionViewController *QVC = segue.destinationViewController;
QVC.currentResidentName = [[NSString alloc] initWithString:self.name];
}
}
Any idea how to make the segue work? I'm not sure why prepareForSegue is not able to identify the segue.
NSLog prints:
selected cell: 0, 123456789
which is good, but it doesn't get to the "prepare for segue" NSLog. When I debug the problem, it crashes right at prepareForSegue

Can you confirm that the segue exists in your storyboard? Even if triggered programatically the segue must exist in your storyboard as described here.
If the segue does exist, check for typos in its name. The name must be spelled the same way in your storyboard and in your code. (This has been a common source of bugs for me, and it seems there is no mechanism for ensuring that storyboard identifiers match the source code.)
If the segue exists and is spelled correctly, then this Stack Overflow thread points at other solutions for more exotic cases, such as projects with multiple storyboards. Also try doing a clean build as suggested in that thread.

Related

How to segue from a custom delegate and datasource

I have a view with 2 tableviews in it. This view has the controller PlayerDetailController
Now to control the 2 tableviews I have 2 other controllers.
tablePlayersDataSourceDelegate
tablePlayerNewsDataSourceDelegate
I ctrl-dragged from the PlayerDetail view to the newsView to make a segue. In my tablePlayerDataSourceDelegate I've the following methods to preform this segue.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"showPlayerDetailNews" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = (NSIndexPath *)sender;
PlayerNews *news = [_tableSource objectAtIndex:indexPath.row]; // ask NSFRC for the NSMO at the row in question
if ([segue.identifier isEqualToString:#"show detail"]) {
[segue.destinationViewController setImageURL:[NSURL URLWithString:news.image]];
[segue.destinationViewController setNewsTitle:news.title];
[segue.destinationViewController setNewsDescription:news.content];
[segue.destinationViewController setNewsCopy:news.image_copyright];
[segue.destinationViewController setNewsUrl:news.url];
[segue.destinationViewController setNewsShortDescription:news.summary];
}
}
But when I test I get the following error.
** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<tblPlayerNewsDatasourceDelagete: 0x1e5e44c0>) has no segue with identifier 'showPlayerDetailNews''
I get why this is giving the error. Because the playerDetail view uses the class playerDetailController and I am trying to do te segue in the tablePlayerNewsDelegate class. Do you maybe know a way to work around it?
EDIT
Here you see a picture of what I am talking about
So you can see the two tableviews in the playerDetailView. When a cell in the bottom tableview is clicked it should go to the next view.
EDIT2
This is what I do in my playerDetailController to fill up my tableview. I've put this in my viewDidLoad.
tabelPlayerNews=[[tblPlayerNewsDatasourceDelagete alloc]init];
[tabelPlayerNews setTableSource:_newsArray];
tblNews.dataSource=tabelPlayerNews;
tblNews.delegate=tabelPlayerNews;
tabelPlayerNews.view=tabelPlayerNews.tableView;
I'm not sure whether I understand that or not but in general....
To use performSegueWithIdentifier: the drag for the segue should start at the controller that will make the call, not at a view or other object. I think that's what the error message is telling you: assuming there's a segue called "showPlayerDetailNews" in your storyboard, it doesn't belong to the correct object.

pushing tableview results in cellForRowAtIndexPath exception

I am trying to build a multilayer xmlparser which shows data in a tableview and switches to the next view based on the selected row (most likely to the same viewcontroller).
I started with storyboard segues but as i am using dynamic cells i dont know how to create more than one push segue (because it needs to push to various viewcontrollers). So i kept the storyboard views, deleted all the segues and used the code below instead.
however it throws this exception when the new view tries to pupulate the row:
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'UITableView dataSource
must return a cell from tableView:cellForRowAtIndexPath:'
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"index: %i", indexPath.row);
switch (indexPath.row) {
case 0:
{
DetailViewController *detailViewController = [DetailViewController alloc];
[self.navigationController pushViewController:detailViewController animated:YES];
break;
}
//...
default:
{
LayerViewController *layerViewController= [LayerViewController alloc];
[layerViewController setStartUpWithIndex:indexPath.row andLayer:layercount];
[self.navigationController pushViewController:layerViewController animated:YES];
break;
}
}
}
setStartUpWithIndex:andLayer: is my init method...
the problem does not occur when im pushing via storyboard segue with the following segue code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
[[segue destinationViewController] setStartUpWithIndex:indexPath.row andLayer:layercount];
}
i think i am missing something the "segue" method does which i need to include in my didSelectRowAtIndexPath as cellForRowAtIndexPath: works fine in the first layer of the view.
requested edit:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"LayerCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = [[articles objectAtIndex:indexPath.row]objectForKey:#"HerstellerName"];
// cell.textLabel.text = #"ololol";
return cell;
}
the articles array is working btw
You aren't creating any cells. I don't know why Apple didn't include this in the default implementation, but after:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
You need to add:
if (cell==nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Otherwise you're just relying on dequeued cells which won't exist when the tableView loads.
Edit: And if you're not using ARC, you need to wrap that alloc method with an autorelease.
You are pushing another viewController to be viewed and this next viewController has no dataSource methods implemented. This what happened.
As far as I understood, you are trying to present an xml node with a table in the way that each table row consist child nodes of presented node. selecting row with chld node gets you to next table view which presents table with child nodes of selected node.
IMO you may need only one view controller to present current xml node, which changes only data to be presented in a table and reloads the table again. So no pushing view controllers is needed. You can use then method
– reloadRowsAtIndexPaths:withRowAnimation:
for all the nodes in the table, setting animation similar to changing view controller (aka sliding old table left)

segue.destinationViewController - NSInvalidArgumentException

When a user selects a row in UITableViewController, I want to segue to another viewController and set its UIImageView to a previously set image. For now, I am making it generic - always show /images/en.jpeg.
flickrTVC.m (UITableViewController):
#implementation flickrTVC
...
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showPhoto"])
UIImage *photo = [UIImage imageWithContentsOfFile:#"images/en.jpeg"];
[segue.destinationViewController setDisplayedPhoto:photo];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"showPhoto" sender:self];
}
I have -(void)setDisplayedPhoto:(UIImage *)image; (it sets self.photo to image) in my photoViewController.h (segue.destinationViewController).
I am getting
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController setDisplayedPhoto:]: unrecognized selector sent to instance 0x1f5c4a60'
on the following line: [segue.destinationViewController setDisplayedPhoto:photo];. Even with the implementation blank, the error still shows up.
I am new to objective-c and, probably, I am just messing some things up.
Any help would be appreciated.
Thanks!
The exception occurs because the object that was there does not define the method setDisplayedPhoto:. This could be because the segue.destinationViewController is currently set to a completely different controller (e.g. for a different view than you think). It could also be that you've defined something similar to that method but not exactly the same; if so then the compiler has probably issued a warning about the setDisplayedPhoto: method call.

Objective-C, Storyboard: instantiateViewControllerWithIdentifier returns nil

I have a UITableViewController with a storyboard push segue linking from the prototype cell to a detail page, a regular old UIViewController. In the storyboard, the detail ViewController has an identifier, and the segue has an identifier which is the same as the detail identifier except that the first letter is lowercase. Furthermore, the detail ViewController has a "custom class" (AttractionDetailViewController) selected in the class pulldown.
Doesn't work. The problem is that instantiateViewControllerWithIdentifier:#"AttractionDetails returns nil.
Relevant code. First the prepareForSegue method which the debugger has never entered.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"attractionDetails"])
{
AttractionDetailViewController *attrDetailVC = [segue destinationViewController];
}
}
Instead it goes into this method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//AttractionDetailViewController *attrDetailVC = [[AttractionDetailViewController alloc] init];
AttractionDetailViewController *attrDetailVC = [self.storyboard instantiateViewControllerWithIdentifier:#"AttractionDetails"];
NSIndexPath *selIndexPath = [self.tableView indexPathForSelectedRow];
attrDetailVC.theAttraction = [attractions objectAtIndex:indexPath.row];
[self.navigationController pushViewController:attrDetailVC animated:YES];
}
Since instantiateViewControllerWithIdentifier returns nil it throws an exception of course. The really interesting thing is, if I use the alloc init line instead, it works, but the screen is all black.
Anyway, I've read up about this and tried a few different things and I'm still stymied. Does anyone have any suggestions?
The problem is that you didn't instantiate your master view controller (the UITableViewController) from the storyboard, so its storyboard property is nil.

getting error unrecognized selector sent at tableview when last row of table view is clicked

i m parsing json data and populating the tableview and making some validation with the incoming json data..everthing works fine.i made the code such that when the last table view row is clicked it got to open a modal view controller.when clicked .i m getting this error [tableiew1] Unrecognised selector send at the instance...could u guys help me out below is the code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
if (indexPath.row == 5) {
if (self.dvController6 == nil)
{
Vad_tycker *temp = [[Vad_tycker alloc] initWithNibName:#"Vad_tycker" bundle:[NSBundle mainBundle]];
self.dvController6 = temp;
[temp release];
}
[self presentModalViewController:self.dvController6 animated:YES];
}
}
Seems like you have forgotten to provide the access for the tableView1 in Vad_tycker.
Or You should do a crosscheck whether you have assigned the correct instance in tableView delegate's and also make sure to provide the implementation for the method of delegate's in their respect target classes.
I think you forgot to connect the tableView Datasource and Delegate methods in the vad_tycker controller.
Also check that the instance of UITableView i.e. in your case tableView1 is also connected with the TableView. on the view.
Thanks