0

I have the code below : where Cell new is Custom cell which has array which has object at index when user selects a row which has to be transfered to anothe uiviewcontroller to display that msg But I am unable to do that.

by doing this : I get an error Incompatible pointer types assigning to 'NSString *' from 'UILabel *'

VC2.DisplayMsg.text = cell.textLabel;

displayMsg is a label in uiviewcontroller where the text has to be displayed.Where Am I going wrong?

Please help me out or suggest any other way to transfer value from navigational uitableviwController to another viewcontroller on selecting the row and displaying the message in that uiviewController.Please help me out.I appreciate a great deal.

Thanks in Advance.

Complete code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


Cellnew *cell = [array objectAtIndex:indexPath.row];


      ViewControllerDisplayMsg *VC2 = [[ViewControllerDisplayMsg alloc] init];

VC2.DisplayMsgtext = cell.textLabel.text;


    [self performSegueWithIdentifier:@"displayMsgSegueIdentifier" sender:self];

}

The error :

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString textLabel]: unrecognized selector sent to instance

James Patrick
  • 273
  • 4
  • 19
  • Two issues: First, you shouldn't refer to the controls in your destination controller (e.g. `VC2.DisplayMsg.text`), as they have not been created by the time `prepareForSegue` is running. Second, you are referencing the cell's `textLabel` control, not its `text` property. I think you meant `cell.textLabel.text`, not `cell.textLabel`. – Rob Mar 19 '13 at 13:20
  • Hello Rob Sir .I am currently stuck in the implementation of uitableviewcontroller .This is the first time.I have no clue how to proceed after didSelect method .So far I have created custom cell which loads via json.So after that it should display msg pertaining to the row clicked in the next view.I dont know how to do that. – James Patrick Mar 19 '13 at 13:26
  • Hello Rob can you refer any link for didselect to uiviewcontroller with data (storyboard).It would help me a great deal.Thanks again. – James Patrick Mar 19 '13 at 13:50

3 Answers3

2

You should define a NSString property in other viewController and first assign the value from your table to this string and finally in your viewController's viewDidLoad method you can assign this string to the label text. It is good idea to define your Other Controller as a property to your FirstController.

You should keep the graphical elements updated within the class because this corresponding class or controller is responsible to show the graphical elements & values.

YourTableViewController.h

@class SecondViewController;

@property(strong,nonatomic)SecondViewController secondVC;

YourTableViewController.m

self.secondVC=[SecondViewContrlller alloc]initwithNibName:@"SecondViewController" bundle:nil];
self.secondVC.stringValue= cell.textLabel.text;

YourOtherViewController.h

@property(strong,nonatomic)NSString *stringValue;

YourOtherViewController.m

-(void)viewDidLoad{

self.displayMsg.text=self.stringValue;

[super viewDidLoad];
}
nsgulliver
  • 12,655
  • 23
  • 43
  • 64
  • Hello Sir I have implemented the above code ,the line : self.secondVC=[SecondViewContrlller alloc]initwithNibName:@"SecondViewController" bundle:nil]; is supposed to be storyboard how do I do that ? – James Patrick Mar 19 '13 at 13:14
  • You are welcome, have you solved your issue? – nsgulliver Mar 19 '13 at 15:05
  • No I have not .I am still stuck here :**VC2 = [[ViewControllerDisplayMsg alloc] init];** **VC2.DisplayMsgtext = cell.textLabel.text; [self performSegueWithIdentifier:@"displayMessage" sender:self];** cause exception: **Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString textLabel]: unrecognized selector sent to instance** – James Patrick Mar 19 '13 at 15:12
  • Could you post the code and error in your question? – nsgulliver Mar 19 '13 at 15:14
  • I have updated the complete code with error. – James Patrick Mar 19 '13 at 15:18
  • What is DisplayMsgtext in your code? please post the code how you defining these strings. – nsgulliver Mar 19 '13 at 15:22
  • set the breakpointer and also try to print out the value of the `cell.textLabel.text` and see what it shows for result – nsgulliver Mar 19 '13 at 15:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26471/discussion-between-james-patrick-and-nsgulliver) – James Patrick Mar 19 '13 at 15:26
1

Take a NSString property in your UIViewController, say DisplayMsg: then in didSelectRowAtIndexPath method:

VC2.DisplayMsg=cell.textLabel.text;
Ken Clark
  • 2,500
  • 15
  • 15
1
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
    }
}

This code will help you

Vinodh
  • 5,262
  • 4
  • 38
  • 68