开发者

iPhone SDK, passing strings through views

开发者 https://www.devze.com 2023-03-29 02:11 出处:网络
I\'m trying to pass a string through two views on an iPhone app. On my second view that I want to recover the string in the .h i have:

I'm trying to pass a string through two views on an iPhone app. On my second view that I want to recover the string in the .h i have:

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

@interface PromotionViewController : UITableViewController {

    NSString *currentCat;
}

@property (nonatomic, retain) NSString *currentCat;

@end

And in the .m i have:

@synthesize currentCat;

However, in the first view controller when I try and set that variable I get a not found error:

PromotionViewController *loadXML = [[PromotionViewController alloc] initWithNibName:@"Prom开发者_开发技巧otionViewController" bundle:nil];
        [self.navigationController pushViewController:loadXML animated:YES];
        [PromotionViewController currentCat: @"Test"];

That third line gives me a: Class method +currentCat not found

What am i doing wrong?


Tom, The issue in your code appears that you are trying to set the string using a static method call to the class. This would work if you implemented a static method named currentCat:

I don't think this is what your want. See below on how to correct your issue.

[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


You need to get the string like this, as it's a property and not a method:

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


You need to do:

loadXML.currentCat = @"Test";


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

That should do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消