iPhone SDK,通过视图传递字符串

时间:2011-08-19 15:10:22

标签: iphone objective-c xcode uitableview

我正在尝试通过iPhone应用程序上的两个视图传递字符串。在我的第二个观点,我想恢复.h中的字符串我有:

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

@interface PromotionViewController : UITableViewController {

    NSString *currentCat;
}

@property (nonatomic, retain) NSString *currentCat;

@end

在.m我有:

@synthesize currentCat;

但是,在第一个视图控制器中,当我尝试设置该变量时,我得到一个未找到的错误:

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

第三行给了我一个:类方法+找不到currentCat

我做错了什么?

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

你需要这样做:

loadXML.currentCat = @"Test";

答案 3 :(得分:0)

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

应该这样做。