首先让我为我糟糕的英语道歉。所以我试图做这个程序,从另一个类中获取一个类中的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'总是零,我只是不明白为什么。 谢谢你的帮助。
答案 0 :(得分:2)
试试这个,
paisesDetailViewController *nextview = [[paisesDetailViewController alloc] init];
nextView.dados = self;
[self.navigationController pushViewController:nextview animated:YES];
[nextview release];
更新:
试试这段代码:
- (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];
}
答案 1 :(得分:0)
确保包含
@synthesize escolha;
tableViewController.m中的。还要确保在paisesDetailViewController.m中分配并实例化dados
tableViewController *dados = [[tableViewController alloc] init];
然后尝试使用
-(IBAction) createEvent
{
NSString *help = dados.escolha;
}
此外,您的大写惯例是倒退的。通常,类名是大写的,而实例不是,例如, MyObject *myObject
。
答案 2 :(得分: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
}