2011-08-19 21 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]; 

應該這樣做。