在我的应用程序中,我有多个类:D,问题是我需要从 b 类中的 a 类访问一个变量。为此,我使用合成,我在接口括号之间的.h 中声明我的变量,然后声明变量的属性,它是一个 NSString。然后我使用合成:)。但是当我在 bi 类中访问它时,NSString 等于 nil :§。当我断点检查 NSString 是否被填充时,NSLog 会在 a 类中显示正确的字符串,但在 bi 类中得到了 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
//在类a .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];