在我的应用程序中我有多个类:D,我需要从类b中的类a访问变量。为此我使用synthesize,我在接口括号之间的.h中声明我的变量,然后声明变量的属性是一个NSString。然后我使用合成:)。但是当我在类b中访问它时,我的NSString等于nil:§。当我断点检查NSString是否被填充时,NSLog在classe a中显示正确的字符串,但在b类中我得到nil:§。其次,我想得到你的意见:最好使用像NSUserDefaults这样的全球化或旧学校c全球声明或这种“综合”方式吗?如果有人想帮助我,非常感谢你:)。
//polymorphViewController.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "LCYDataBackedTableView.h"
#import "RootViewController.h"
#import "LCYLockSettingsViewController.h"
#import "LockScreenAppDelegate.h"
#import "MyWebViewController.h"
#import "MemberViewController.h"
#import "SharedViewController.h"
#import "UploadedViewController.h"
@interface PolymorphViewController : LCYDataBackedTableView{
NSString *secondId;
}
@property (readwrite,nonatomic) NSString *secondId;
@end
//在课堂上.h
@interface firstViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate>{
IBOutlet UITableView * newsTable;
UISearchBar *searchBar;
UISearchDisplayController *searchDisplayController;
...
NSString *idNumber;
}
@property (readwrite, retain) NSString *idNumber;
- (void)launchIt:(NSString *)typeofdata sharedOrUploaded:(BOOL)smogogo;
@end
//in class a .m
@implementation firstViewController
@synthesize idNumber
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
idNumber = [[stories objectAtIndex: storyIndex] objectForKey: @"sizing_id"];
PolymorphViewController *polymorph = [[PolymorphViewController alloc]init];
[[self navigationController] pushViewController:polymorph animated:YES];
[polymorph viewDidLoad];
}
//in class B .m
-(void)dataByChoice: (NSString *)command{
self.title = @"Test";
myWebView = [[UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:myWebView];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *defaults = [prefs stringForKey:@"myKey"];
NSString *defaults2 = [prefs stringForKey:@"mySecondKey"];
NSString *username = defaults;
NSString *password = defaults2;
firstViewController *WC = [[firstViewController alloc]initWithNibName:nil bundle:nil];
NSLog(WC.idNumber); <-- equals nil :(
NSString *idName = WC.idNumber;
NSString *partialUri = [@"http://www.google.com" stringByAppendingString: idName];
NSString *finalUri = [partialUri stringByAppendingString: command];
NSURL *url = [NSURL URLWithString:finalUri];
答案 0 :(得分:1)
你的课应该看起来像这个.h文件 @interface Class_B:UIViewController
@property (copy,nonatomic) NSString *someValue;
@end
.m文件
@implementation Class_B
@synthesize someValue = _someValue;
- (void) viewDidLoad {
_someValue = @"This is my come value";
}
@end
并且调用类(Class_A)应该是这样的
- (void) viewDidLoad {
Class_B *cls_b = [[Class_B alloc] initWithNibName:@"Class_B_nib" bundle:nil];
NSLog(@"Some value echo :%@",cls_b.someValue);
}
答案 1 :(得分:1)
当您需要在应用程序的会话中存储本地数据时使用NSUserDefaults
,我不会仅仅在类之间共享变量。
@synthesize
指令用于生成属性的访问器方法(现在,除非您想要更改setter和getter的名称,您甚至不需要它),使用@property
指令声明。 / p>
如果您只需要从另一个对象访问该属性,那么在接口文件(.h)中声明的属性是完美的。如果你需要在应用程序中共享一个属性,我正在考虑singleton pattern,但肯定有很多方法可以做到。