我想从 AppDelegate
向所有其他 View Controller 传递一个字符串。我定义了一个名为 appName
的变量并为其分配了一个字符串
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSString *appName;
@end
AppDelegate.m
@implementation AppDelegate
@synthesize appName;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
appName=@"SatKacPara";
return YES;
}
以下实现无法访问 appName
属性。我想不通。
ViewController1.m
appLabel.text=[(AppDelegate*) [UIApplication shareApplication].delegate ].appName;
最佳答案
您的代码行中有一个简单的拼写错误。应该是:
appLabel.text = ((AppDelegate*)[UIApplication sharedApplication].delegate).appName;
但是没有必要使用这样的属性。您无需硬编码即可获取应用的显示名称。
删除您的 appName
属性并将这行代码更改为:
appLabel.text = [NSBundle mainBundle].infoDictionary[@"CFBundleDisplayName"];
即使您重命名您的应用或对其进行本地化,这也能显示正确的名称。
关于ios - 访问 AppDelegate 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33457144/