JTFerreira
JTFerreira

Reputation: 15

iPhone: Access a NSString value from another class

First let me apologise for my bad english. So I has trying to do this program that fetch the value of a NSString in one class from another class. Here's the code:

/---------------------/tableViewController.h -----------

#import <UIKit/UIKit.h>
@class paisesDetailViewController;

@interface tableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>{
    IBOutlet UITableView *paisesTableView;
    paisesDetailViewController *PaisesDetailViewController;

    //DB
    NSString *DBName;
    NSString *DBPath;
    NSMutableArray *ArrayPais; //means Countrys
    NSString *escolha; //means choise 
}

@property(nonatomic, retain) paisesDetailViewController *PaisesDetailViewController;
@property(nonatomic, retain) NSMutableArray *ArrayPais;
@property(nonatomic, retain) NSString *escolha;

@end




-----------------------------//tableViewController.m---------------

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSInteger row = [indexPath row];
    if(self.PaisesDetailViewController == nil)
    {
        paisesDetailViewController *apaisesDetail = [[paisesDetailViewController alloc] initWithNibName:@"paisesDetailView" bundle:nil];
        self.PaisesDetailViewController = apaisesDetail;
        [apaisesDetail release];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    escolha = [[NSString alloc] init];
    self.escolha = [NSString stringWithFormat:@"%@", [ArrayPais objectAtIndex:(row)]]; 

    paisesDetailViewController *nextview = [[paisesDetailViewController alloc] init];
    [self.navigationController pushViewController:nextview animated:YES];
    [nextview release];

}




-----------------------------------//paisesDetailViewController.h------------
#import <UIKit/UIKit.h>
#import "tableViewController.h"
@class tableViewController;

@interface paisesDetailViewController : UIViewController {
    tableViewController *dados;

}

@property(nonatomic, retain) tableViewController *dados;

@end


//-----------------------paisesDetailViewController.m
#import "paisesDetailViewController.h"
#import "tableViewController.h"

@implementation paisesDetailViewController
@synthesize dados;

-(IBAction) createEvent
{
        NSString *help = [[NSString alloc] initWithString: dados.escolha]; 
}

The problem is that the 'dados.escolha' is always nil and I just can't understand why. Thanks for the help.

Upvotes: 1

Views: 900

Answers (3)

-(IBAction) createEvent
{
    NSString *help = [[NSString alloc] init];

    help = [NSString stringWithFormat: @"%@", self.dados.escolha]; 

    NSLog(@"help is : %@ ", help);

    // You have synthesized them, so use with self
}

Upvotes: 0

user745098
user745098

Reputation:

Try this,

paisesDetailViewController *nextview = [[paisesDetailViewController alloc] init];
nextView.dados = self;
[self.navigationController pushViewController:nextview animated:YES];
[nextview release];

UPDATE:

Try this code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    if(self.PaisesDetailViewController == nil)
    {
        paisesDetailViewController *apaisesDetail = [[paisesDetailViewController alloc] initWithNibName:@"paisesDetailView" bundle:nil];
        self.PaisesDetailViewController = apaisesDetail;
        [apaisesDetail release];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    escolha = [[NSString alloc] init];
    self.escolha = [NSString stringWithFormat:@"%@", [ArrayPais objectAtIndex:(row)]]; 

    paisesDetailViewController *nextview = [[paisesDetailViewController alloc] init];
    nextView.dados = self;
    [self.navigationController pushViewController:nextview animated:YES];
    [nextview release];
}

Upvotes: 2

PengOne
PengOne

Reputation: 48398

Make sure that you included

@synthesize escolha;

in tableViewController.m. Also make sure that you allocated and instantiated dados in paisesDetailViewController.m

tableViewController *dados = [[tableViewController alloc] init];

Then try using simply

-(IBAction) createEvent
{
        NSString *help = dados.escolha; 
}

Also, your capitalization conventions are backwards. Generally, the class name is capitalized and the instance is not, e.g. MyObject *myObject.

Upvotes: 0

Related Questions