iOS Appdelegate 字段填充和 object 创建
iOS Appdelegate field population and object creation
我是 iOS 的新手,正在尝试了解 Appdelegate 回调和字段填充的功能。
所以这就是我正在做的事情:
在 Appdelegate 的 header
中声明全局 属性
@property NSString* GlobalUsername;
在其中一个回调
中填充此 属性 的值
- (void)applicationDidBecomeActive:(UIApplication *)application {
_GlobalUsername = @"username";
}
现在在 ViewControler 的按钮点击方法中,我正在创建 AppDelegate 的 object,访问 GlobalUsername 并使用 textView 在屏幕上显示它。
- (IBAction)ShowDetails:(id)sender
{
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSString* details=[NSString stringWithFormat:@"Username is : %@",appdelegate.GlobalUsername];
[_textView setText:details];
}
现在我不明白的是 GlobalUsername 的值是如何与 AppDelegate 的 object 相关联的。 GlobalUsername 是在应用程序激活时填充的,而不是在我创建 Appdelegate 实例时填充的,那么 sharedApplication 方法返回的 object 如何仍然封装了此信息?
delegate
of UIApplication
根据单例模式工作。每次尝试获取它时只有 1 个实例。例如:
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
...Any API requests...
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
如果 UIApplication 的委托每次都不同,则不会保存网络指示器的状态。
我是 iOS 的新手,正在尝试了解 Appdelegate 回调和字段填充的功能。
所以这就是我正在做的事情: 在 Appdelegate 的 header
中声明全局 属性@property NSString* GlobalUsername;
在其中一个回调
中填充此 属性 的值- (void)applicationDidBecomeActive:(UIApplication *)application {
_GlobalUsername = @"username";
}
现在在 ViewControler 的按钮点击方法中,我正在创建 AppDelegate 的 object,访问 GlobalUsername 并使用 textView 在屏幕上显示它。
- (IBAction)ShowDetails:(id)sender
{
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSString* details=[NSString stringWithFormat:@"Username is : %@",appdelegate.GlobalUsername];
[_textView setText:details];
}
现在我不明白的是 GlobalUsername 的值是如何与 AppDelegate 的 object 相关联的。 GlobalUsername 是在应用程序激活时填充的,而不是在我创建 Appdelegate 实例时填充的,那么 sharedApplication 方法返回的 object 如何仍然封装了此信息?
delegate
of UIApplication
根据单例模式工作。每次尝试获取它时只有 1 个实例。例如:
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
...Any API requests...
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
如果 UIApplication 的委托每次都不同,则不会保存网络指示器的状态。