self.tableView indexPathForSelectedRow returns nil - objective-c

I'm fairly new to Objective-C and have encountered this following issue.
I've setup the storyboard and the linked up the view controllers accordingly. Also, setup the segue. However, for some weird reason, self.tableView indexPathForSelectedRow is returning me nil.
Really appreciate for the help in advanced!
- (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:#"infoTherapyCloseCase"])
{
infoTherapyPhaseCloseCaseViewController *info = [segue destinationViewController];
//info = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
int row = [myIndexPath row];
TherapyPhases *tp = _DetailModal[row];
info.DetailModal = #[tp];
}
}

Related

The indexPath of custom table cell is undefined, objective c

I have created a custom cell call PendingDoctorTableViewCell and is used in PendingDoctorTableView. Everything is work find and i added a disclosure indicator by using
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
It's able to performed the segue "ShowPendingDoctor".
However when i used the prepareForSegue, the indexPath is undefined.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"ShowPendingDoctor"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
ActionForPendingDoctorViewController *destViewController = segue.destinationViewController;
destViewController.myObjects = [myObject objectAtIndex:indexPath.row];
// Hide bottom tab bar in the detail view
// destViewController.hidesBottomBarWhenPushed = YES;
}
}
How can i get to know which row had been tap. because its always return the object of 1st index.
Anyone can help me? i'm rushing for my project. Thanks.
Declare a variable of NSIndexPath to save your currently selected indexPath
NSIndexPath *selectedIndexPath;
Use Tableview delegate to identify selected row and perform segue programmatically
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:YOUR_SEGUE_IDENTIFIER sender:self];
}
Pass your object to next ViewController through prepareForSegue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:YOUR_SEGUE_IDENTIFIER])
{
ActionForPendingDoctorViewController *destViewController = segue.destinationViewController;
destViewController.myObjects = [myObject objectAtIndex:selectedIndexPath.row];
}
}

Unrecognized selector - using indexpath.row to send a string to view2

Im having trouble passing a string to a second view. I am trying to grab the indexpath.row for the selected tableviewcell and pass the associated string but keep getting "unrecognized selector.."
When using DLog the console displays the correct string. On the view im passing info. to I have an NSString setup. Thanks for any help you can provide!
Here is the prepareforsegue code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"kShowMovie"])
{
MoviePlayerNEWViewController *dvc = (MoviePlayerNEWViewController *)[segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DemoVideos *demovideo = [_tableContents objectAtIndex:indexPath.row];
dvc.videoDemoString = demovideo.vd_link;
DLog(#"Video link to pass = %#",demovideo.vd_link);
}
UIViewController * controller = segue.destinationViewController;
[controller setHidesBottomBarWhenPushed:YES];
}
The error message is saying that the controller you're trying to call setVideoDemoString: on is a UINavigationController, not a MoviePlayerNEWViewController as you think it is. You probably want this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"kShowMovie"])
{
UINavigationController *nav = segue.destinationViewController;
MoviePlayerNEWViewController *dvc = nav.topViewController;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DemoVideos *demovideo = [_tableContents objectAtIndex:indexPath.row];
dvc.videoDemoString = demovideo.vd_link;
DLog(#"Video link to pass = %#",demovideo.vd_link);
}
UIViewController * controller = segue.destinationViewController;
[controller setHidesBottomBarWhenPushed:YES];
}

PrepareForSegue Tableview issue

I have an issue with segue when clicking on cell and trying to transfer my date to DetailViewController
I didn't find here answers to my question, so I am asking here.
Here is my Method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// NSLog(#"Source Controller = %#", [segue sourceViewController]);
// NSLog(#"Destination Controller = %#", [segue destinationViewController]);
// NSLog(#"Segue Identifier = %#", [segue identifier]);
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
if (indexPath){
lastNews *item = [news objectAtIndex:indexPath.row];
[segue.destinationViewController setDetail:item];
NSLog(#"%#", item.title);
}
}
if I will remove if condition only
and leave my code like this one:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// NSLog(#"Source Controller = %#", [segue sourceViewController]);
// NSLog(#"Destination Controller = %#", [segue destinationViewController]);
// NSLog(#"Segue Identifier = %#", [segue identifier]);
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
lastNews *item = [news objectAtIndex:indexPath.row];
[segue.destinationViewController setDetail:item];
NSLog(#"%#", item.title);
}
my data can be transferred but only always first cell.
If I will implement it like this one:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"detailNews"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
//if (indexPath){
lastNews *item = [news objectAtIndex:indexPath.row];
[segue.destinationViewController setDetail:item];
NSLog(#"%#", item.category);
}
}
it will send but always a title of news and always from first news. (NSLOG only for checking)
I know it receives all values, I have checked it by NSLOG, not only the title.
For your information I have always latest 20 news parsed from web site
That's working and my cells filled by news titles.
Could you please suggest me where I am wrong?
Thank you
if "sender" is the cell that's calling this, you can do:
NSIndexPath *indexPath = [[self tableView] indexPathForCell:sender];
lastNews *item = [news objectAtIndex:indexPath.row];
[segue.destinationViewController setDetail:item];
Ok I think I solved my last issue. I have forgotten connect my IBOUTLET Tableview to my viewcontroller. That's it

Storyboard Preparing for Seque+Passing over data

I have a detail view controller that is hooked up to a table view cell. I have a data model. It is an NSObject and has a NSMutableArray will all my properties I need in them.
I hooked up a label to an #property and named it questionLabel. For some reason, when I push over to the detail view, it does not display the question I put in the data model for the NSMutableArray.
At first I thought the segue was not working, but when I tried to change the navigation bar title, it worked when I passed my data from my NSMutableArray.
Anyone know what I am doing wrong with my label?
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"pushDetail"])
{
// Create an instance of our DetailViewController
DetailQuestionViewController *DQV = [[DetailQuestionViewController alloc] init];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
// Setting DQV to the destinationViewController of the seque
DQV = [segue destinationViewController];
Question *selectedQuestion = [self.myQuestionList questionAtIndex:path.row];
DQV.questionLabel.text = [selectedQuestion questionName];
DQV.navigationItem.title = [selectedQuestion questionRowName];
}
}
UPDATE (For: adambinsz)
Table view
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"pushDetail"])
{
// Create an instance of our DetailViewController
DetailQuestionViewController *DQV = [[DetailQuestionViewController alloc] init];
// Setting DQV to the destinationViewController of the seque
DQV = [segue destinationViewController];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Question *selectedQuestion = [self.myQuestionList questionAtIndex:path.row];
DQV.detailItem = selectedQuestion;
}
}
detailView
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
Question *theQuestion = (Question *)self.detailItem;
self.questionLabel.text = theQuestion.questionName;
NSLog(#"The Question's questionName is %#", theQuestion.questionName);
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
The reason I think it may not be working, is because the if statement for self.detailItem is not checking it correctly.
The method gets called just not the detailItem.
Your label probably hasn't been created yet when you try to set its text. I would create a selectedQuestion instance variable on DetailQuestionViewController and set it when you create the view controller. Something like this:
// Create an instance of our DetailViewController
DetailQuestionViewController *DQV = [[DetailQuestionViewController alloc] init];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
// Setting DQV to the destinationViewController of the seque
DQV = [segue destinationViewController];
DQV.selectedQuestion = [self.myQuestionList questionAtIndex:path.row];
And in DetailQuestionViewController's viewDidLoad method, set your label's text by using:
self.questionLabel.text = [selectedQuestion questionName];
self.navigationItem.title = [selectedQuestion questionRowName];

Cannot pass value from UITableViewCell to a detail view

I am trying to change the color of a label in my detail view based on which cell is touched in a table view.
I just can't make this happen. Below is my code. I have included the header file from the detail view, and created an IBOutlet for the label.
Update: Ideally, when I clicked on a cell, the label color in detailview should be red. And here is a screencast about my problem. http://www.youtube.com/watch?v=i0nhbsNJWHY&feature=youtube_gdata_player
#pragma mark - TableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"ReferenceDetail" sender:tableView];
}
#pragma mark - Segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ReferenceDetail"]) {
ReferenceDetailViewController *referenceDetailViewController = [segue destinationViewController];
UIViewController *referenceDetailView = [segue destinationViewController];
if(sender == self.searchDisplayController.searchResultsTableView) {
NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
NSString *destinationTitle = [[filteredReferenceArray objectAtIndex:[indexPath row]] name];
[referenceDetailView setTitle:destinationTitle];
}
else {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *destinationTitle = [[referenceArray objectAtIndex:[indexPath row]] name];
[referenceDetailView setTitle:destinationTitle];
[referenceDetailViewController.label1 setTextColor:[UIColor redColor]];
}
}
}
The problem you are having is that the outlet UILabel inside your ReferenceDetailViewController has not loaded up from the xib, yet you are trying to set it's textColor property.
As a way to make sure this is the problem, try setting a breakpoint in the line where you set the color and another breakpoint inside the ReferenceDetailViewController [viewDidLoad] method. The line where you setup the color should be getting called first.
A simple solution to your problem is to have a property on your ReferenceDetailViewController class that "holds" the UIColor instance and later set it on the viewDidLoad method:
// ...
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ReferenceDetail"]) {
ReferenceDetailViewController *referenceDetailViewController = [segue destinationViewController];
UIViewController *referenceDetailView = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *destinationTitle = [[referenceArray objectAtIndex:[indexPath row]] name];
[referenceDetailView setTitle:destinationTitle];
referenceDetailViewController.auxTextColor = [UIColor redColor];
}
// ..
And in ReferenceDetailViewController:
#interface ReferenceDetailViewController : UIViewController
#property (retain, nonatomic) UIColor *auxTextColor;
// ...
#end
#implementation ReferenceDetailViewController
- (void)viewDidLoad
{
[super viewDidLoad];
label1.textColor = self.auxTextColor;
// ...
}
#end
If you must do this for a set of labels, it'd be wise to have a NSDictionary object to hold these properties for you.
The outlets are not setup yet when you segue so you can't get to them to change them. Try sending some object or something over and in the viewdidload of the viewcontroller you segue to you read that and then use that as the way to know what color to change to and then in that same code you change the color of the label.