我是 Objective-C 和可可的新手。
在我的 UIViewController 中,我需要以不同的方法多次访问 AppDelegate
A. 在每个方法中调用:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
消耗更多性能?
B. 我尝试在 UIViewController 中创建一个全局参数:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
@interface Login_ViewController : UIViewController<UITextFieldDelegate,UIImagePickerControllerDelegate>{
AppDelegate *appDelegate;
}
实现和使用:
- (void)viewDidLoad
{
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.businessId = [businessId integerValue];
}
- (BOOL)credentialsValidated
{
appDelegate.businessId = [[NSUserDefaults standardUserDefaults] integerForKey:BUSINESS_ID];
}
但我收到警告(尽管代码有效)
Incompatible integer to pointer conversion assigning to 'NSInteger *' (aka 'int *') from 'NSInteger' (aka 'int');
appDelegate中businessId的声明为:
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property NSInteger *businessId;
和实施:
@implementation AppDelegate
@synthesize businessId;