我有这个appdelegate.h
#import <UIKit/UIKit.h>
@interface appDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
NSString *name;
}
@property (nonatomic, retain) NSString *name;
@end
和.m文件
@synthesize name;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
name=@"john";
return YES;
}
现在......我想从另一个控制器获取此名称,如果我尝试在我的viewDidLoad方法中调用它,它可以工作..
- (void)viewDidLoad
{
appDelegate *test= (appDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@", test.name);
}
但是如果我尝试在我的initWithNibName中做同样的事情,它就不起作用......
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
appDelegate *test= (appDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@", test.name);
}
任何人都可以帮助我吗?这个问题让我发疯了......
答案 0 :(得分:1)
如果要覆盖-initWithNibName:
,则需要返回该类的实例(或self
);
请在-initWithNibName:
中尝试以下代码。它对我有用。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
appDelegate *test= (appDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@", test.name);
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
}
return self;
}
我认为这对你有用。