-1

i need to pass data between to Views. I really can't figure out why is not working. Variable selectedRow in SecondViewController is always empty.

FirstViewController.m

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    SecondViewController *newView = [[SecondViewController alloc] init];
    newView.selectedRow = [tablelist objectAtIndex:indexPath.row];
    newView.title = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
    [self.navigationController pushViewController:newView animated:YES];
}

SecondViewController.h

@interface SecondViewController : UIViewController
{
    NSString *title,*selectedRow;
}

@property(nonatomic,retain) NSString *title;
@property (nonatomic,retain) NSString *selectedRow;

@end

SecondViewController.m

@synthesize title,selectedRow;

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (self) {
         textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 64, 320, 370)];
         textView.text = selectedRow;
         textView.editable = FALSE;
         [self.view addSubview:textView.text];
     }
     return self; }

I'm struggling for hours. I really cant figure why. Any help? Thank you

Fabio
  • 3
  • 1

3 Answers3

1

The problem is that you are looking at the selectedRow value before you set the property. You can't set the selectedRow property until after the call to init completes.

Since your use of the property is to setup the view controller's view, you should move that code to viewDidLoad where it belongs. As a rule, you should not access self.view in the init method of a view controller.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (self) {
     }

     return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 64, 320, 370)];
    textView.text = selectedRow;
    textView.editable = FALSE;
    [self.view addSubview:textView.text];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

I used savestrings to pass data to the next viewcontroller. The only thing you need to do is import the viewcontroller from where the data comes in the viewcontroller.

You can use this in the first viewcontroller

//1e savestring
NSString *saveString1 = emailfield.text;
NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
[defaults1 setObject:saveString1 forKey:@"saveString1"];
[defaults1 synchronize];

ANd this in the viewcontroller where you need to load data

//First load string
NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
NSString *loadString1 = [defaults1 objectForKey:@"saveString1"];
[emailfield setText:loadString1];
Sam VG
  • 143
  • 1
  • 2
  • 9
0

if you are using storyboard to get a viewcontroller ,you can try this.

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

  UIStoryboard *storyboard = [ UIStoryboard storyboardWithName:@"Main" bundle:nil ];
  SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"test" ];
  vc.title = @"something";
  [self.navigationController pushViewController:detailView animated:YES];
}
SeanChense
  • 846
  • 8
  • 14