Iphone 在同一UIViewController中多次使用appDelegate
我不熟悉objective-C和cocoa 在我的UIViewController中,我需要以不同的方法多次访问AppDelegate A.正在调用每个方法:Iphone 在同一UIViewController中多次使用appDelegate,iphone,objective-c,pointers,nsinteger,appdelegate,Iphone,Objective C,Pointers,Nsinteger,Appdelegate,我不熟悉objective-C和cocoa 在我的UIViewController中,我需要以不同的方法多次访问AppDelegate A.正在调用每个方法: AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 消耗更多性能 B.我试图在UIViewController中创建一个全局参数: #import <UIKit/UIKit.h> #import "AppDelegate.h" @
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
消耗更多性能
B.我试图在UIViewController中创建一个全局参数:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
@interface Login_ViewController : UIViewController<UITextFieldDelegate,UIImagePickerControllerDelegate>{
AppDelegate *appDelegate;
}
但我得到了警告(尽管代码有效)
appDelegate中的businessId声明为:
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property NSInteger *businessId;
从应用程序委托中的声明中删除
*
:
@property NSInteger (assign) businessId;
NSInteger
是一个原语,因此您不需要对象指针。a。这不会给性能带来任何损失,除非您每秒调用100000000。顺便说一句,不要假设smth-始终测量。有一个叫做时间分析器的工具——用它来找出所有的瓶颈
NSInteger只是一个从typedef到int的类型-它是POD类型而不是ObjC对象,所以您不能向它发送消息。使用NSInteger而不是NSInteger*您可以这样定义AppDelegate #定义委托(AppDelegate*)[[UIApplication sharedApplication]委托];
现在,您可以在需要的地方使用委派。谢谢,工作顺利。但是第一个问题呢?它会消耗性能吗?没有惩罚。没什么好担心的。什么是最佳编码实践?这是一种非常常见的模式。您还可以定义一个文本宏来加快键入速度,但这并没有什么大的区别。另一个用于存储程序范围变量的流行且合理的选项是
NSUserDefaults
。您可以在这里使用所有常量声明生成全局类或相同类,也可以像这样#import#import“AppDelegate.h”#define DELEGATE(AppDelegate*)[[UIApplication sharedApplication]DELEGATE];谢谢-但是我如何在代码中使用它呢?我试图导入常量类并编写APP_DELEGATE.shouldDo,但它无法编译
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property NSInteger *businessId;
@implementation AppDelegate
@synthesize businessId;
@property NSInteger (assign) businessId;