2011-06-24 64 views
1

首先讓我爲我的壞英語道歉。所以我試圖做這個程序,從另一個類中獲取一個類的NSString的值。這裏的代碼:iPhone:從另一個類訪問NSString值

/---------------------/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]; 
} 

問題是,'dados.escolha'總是零,我只是不明白爲什麼。 感謝您的幫助。

回答

2

試試這個,

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

UPDATE:

試試這個代碼:

- (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]; 
} 
+0

這樣做了。謝謝。但要解決它,我不得不做* dados = [[tableViewController alloc] init];每當我做到這一點時,我總是得到零。現在它的工作,這就是metter :) Thks – JTFerreira

+0

Superbbb Man ... it Worked ... nextView.dados = self;我正在尋找類似場景的解決方案,並最終解決上述問題。非常感謝... –

0

請確保您包括

@synthesize escolha; 
在tableViewController.m

。另外,還要確保你分配和paisesDetailViewController.m

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

然後嘗試使用簡單

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

而且,你的大小寫約定是倒退。通常,類名是大寫的,而實例不是,例如MyObject *myObject

+0

「tableViewController * dados = [[tableViewController alloc] init];」 你不是說「dados = [[tableViewController alloc] init];」?通過做,因爲你說我正在創建一個新的「爸爸」... 無論如何,我一直得到零dados.escolha ... – JTFerreira

0
-(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 
} 
+0

self.dados.escolha ==零,同樣的問題... – JTFerreira