2011-08-19 71 views
0

我想通过iPhone应用程序上的两个视图传递一个字符串。在我要恢复在.hi字符串我的第二个观点有:iPhone SDK,通过视图传递字符串

#import <UIKit/UIKit.h> 
#import "MBProgressHUD.h" 
#import "RootViewController.h" 

@interface PromotionViewController : UITableViewController { 

    NSString *currentCat; 
} 

@property (nonatomic, retain) NSString *currentCat; 

@end 

而且在具有的.mi:

@synthesize currentCat; 

然而,在第一个视图控制器,当我尝试集该变量我得到一个未找到错误:

PromotionViewController *loadXML = [[PromotionViewController alloc] initWithNibName:@"PromotionViewController" bundle:nil]; 
     [self.navigationController pushViewController:loadXML animated:YES]; 
     [PromotionViewController currentCat: @"Test"]; 

这第三行给了我:类方法+ currentCat没有找到

我在做什么错?

回答

1

汤姆, 的问题出现在你的代码您尝试使用静态方法调用该类来设置字符串。这将工作,如果你实现了一个名为currentCat的静态方法:

我不认为这是你想要的。 请参阅下文了解如何解决您的问题。

[PromotionViewController currentCat:@"Test"]; 
//This will not work as it is calling the class itself not an instance of it. 

[loadXml setCurrentCat:@"Test"]; 
//This will work. Keep in mind if you are going to call the objective-c 
//synthesize setting directly you will need to capitalize the first letter 
//of your instance variable name and add "set" to the front as I've done. 

//Alternatively in objective-c 2.0 you can also use 
//the setter method with the . notation 

loadXml.currentCat = @"Test"; 
//This will work too 
0

你需要得到这样的字符串,因为它是一个属性,而不是一个方法:

NSString* myString = controller.currentCat; // where controller is an instance of PromotionViewController 
0

你需要做的:

loadXML.currentCat = @"Test"; 
0
PromotionViewController *loadXML = [[PromotionViewController alloc] initWithNibName:@"PromotionViewController" bundle:nil]; 
[loadXML setCurrentCat: @"Test"]; 
[self.navigationController pushViewController:loadXML animated:YES]; 

应该这样做。

相关问题