为 UIApplication 委派 属性
Delegate Property for UIApplication
所以我开始学习 iOS 的 Core Data 并写出了这部分代码:
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
id delegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *objectContext = [delegate managedObjectContext];
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Album"];
//An array of our sorted Album objects by date in ascending order
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];
NSError *error = nil;
NSArray *fetchedAlbums = [objectContext executeFetchRequest:fetchRequest error:&error];
self.albums = [fetchedAlbums mutableCopy];
[self.tableView reloadData];
}
我的问题是这一行的目的是什么:
id delegate = [UIApplication sharedApplication].delegate;
我知道一个事实,即对于委托,您需要将特定对象的当前实例传递给委托 属性,后者将该对象设置为另一个对象的委托。我的问题是你什么时候做:
id delegate = [UIApplication sharedApplication].delegate;
哪个对象被传递给那个 delegate
属性? sharedApplication
方法 returns 是我假设的应用程序的单例实例,然后您将获得该应用程序的单个实例的 delegate
属性。该代表指的是 AppDelegate.h
/AppDelegate.m
文件中使用的同一个代表吗?
并且如果该委托 属性 与 AppDelegate.h
/AppDelegate.m
文件中使用的委托相同,那么 [=20= 中不应该有一行代码] 文件中的 viewDidLoad
方法,我们在委托 属性 中传递 AppDelegate.m
的当前实例,类似于我们为 tableView
:[=28 所做的=]
self.tableView.delegate = self.
我看不到这一行中 delegate
属性 在哪里完成:
id delegate = [UIApplication sharedApplication].delegate;
您不需要将您的 AppDelegate
指定为您的应用程序的代表,因为它会自动为您完成。
在 objective-c 中你可以查看 main.m 函数,在那里你可以找到提到你的地方 AppDelegate
class:
int main(int argc, char *argv[])
{
@autoreleasepool {
int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
return retVal;
}
}
此调用后,系统会将 class AppDelegate
的实例设置为您的应用程序的委托。
在 Swift 中,缺少 main.m 这就是为什么在您的委托 class:
声明之前总是有 @UIApplicationMain
@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {
}
因为 UIApplication.sharedApplication()
是单例,UIApplication.sharedApplication().delegate
总是 returns AppDelegate
.
的同一个实例
因此,如果您从一个视图控制器修改 AppDelegate
中的一些 属性,您可以确保其他视图控制器将可以访问修改后的版本。您可以通过实现一个简单的计数器并检查您是否始终可以读取最新值来轻松地自己验证它:)
所以我开始学习 iOS 的 Core Data 并写出了这部分代码:
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
id delegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *objectContext = [delegate managedObjectContext];
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Album"];
//An array of our sorted Album objects by date in ascending order
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];
NSError *error = nil;
NSArray *fetchedAlbums = [objectContext executeFetchRequest:fetchRequest error:&error];
self.albums = [fetchedAlbums mutableCopy];
[self.tableView reloadData];
}
我的问题是这一行的目的是什么:
id delegate = [UIApplication sharedApplication].delegate;
我知道一个事实,即对于委托,您需要将特定对象的当前实例传递给委托 属性,后者将该对象设置为另一个对象的委托。我的问题是你什么时候做:
id delegate = [UIApplication sharedApplication].delegate;
哪个对象被传递给那个 delegate
属性? sharedApplication
方法 returns 是我假设的应用程序的单例实例,然后您将获得该应用程序的单个实例的 delegate
属性。该代表指的是 AppDelegate.h
/AppDelegate.m
文件中使用的同一个代表吗?
并且如果该委托 属性 与 AppDelegate.h
/AppDelegate.m
文件中使用的委托相同,那么 [=20= 中不应该有一行代码] 文件中的 viewDidLoad
方法,我们在委托 属性 中传递 AppDelegate.m
的当前实例,类似于我们为 tableView
:[=28 所做的=]
self.tableView.delegate = self.
我看不到这一行中 delegate
属性 在哪里完成:
id delegate = [UIApplication sharedApplication].delegate;
您不需要将您的 AppDelegate
指定为您的应用程序的代表,因为它会自动为您完成。
在 objective-c 中你可以查看 main.m 函数,在那里你可以找到提到你的地方 AppDelegate
class:
int main(int argc, char *argv[])
{
@autoreleasepool {
int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
return retVal;
}
}
此调用后,系统会将 class AppDelegate
的实例设置为您的应用程序的委托。
在 Swift 中,缺少 main.m 这就是为什么在您的委托 class:
声明之前总是有@UIApplicationMain
@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {
}
因为 UIApplication.sharedApplication()
是单例,UIApplication.sharedApplication().delegate
总是 returns AppDelegate
.
因此,如果您从一个视图控制器修改 AppDelegate
中的一些 属性,您可以确保其他视图控制器将可以访问修改后的版本。您可以通过实现一个简单的计数器并检查您是否始终可以读取最新值来轻松地自己验证它:)