已解决:怀疑使用 NSNotification Center:D
在我的 AppDelegate(.m) 中,我在 openURL 中添加了以下例程
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
... other code to handle Dropbox imported URLs
// Check to see if a file has been sent over via another app, like e-mail
if (url != nil && [url isFileURL])
{
MainViewController *firstViewController = [[MainViewController alloc]init];
[firstViewController handleOpenURL:url];
NSURL *fileFromEmail=url;
// add the url as a dictionary item, so that I can open it with NSNotificationCenter
NSDictionary *fileInfo=[NSDictionary dictionaryWithObject:fileFromEmail forKey:@"fileFromEmail"];
// add a notification named "segueListener" and pass on the dictionary object
[[NSNotificationCenter defaultCenter] postNotificationName:@"segueListener" object:nil userInfo:fileInfo];
return YES;
}
}
然后在 viewDidLoad 的 MainViewController(.m) 中,我添加了一个 NSNotification Observer:
- (void)viewDidLoad
{
[super viewDidLoad];
...
// if a notification named "segueListener" is sent out, perform the method: segueToImportFile
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(segueToImportFile:) name:@"segueListener" object:nil];
}
然后添加了 handleOpenURL 方法:
-(void) handleOpenURL:(NSURL *)url
{
// capture the url and store it into _importedFileURL (NSURL)
_importedFileURL=url;
}
和 segueToImportFile 方法:
-(void)segueToImportFile:(NSNotification *)notification
{
NSDictionary *file=[notification userInfo]; // get the dictionary object userInfo
_importedFileURL=[file objectForKey:@"fileFromEmail"];
[self performSegueWithIdentifier:@"Select Course for Import" sender:self];
}
最后,segue 会将 _importedFileURL 发送到列出课程的新 TableViewController:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UINavigationController *navigationController=segue.destinationViewController;
SelectCourseTableViewController *selectCourseTableViewController=[[navigationController viewControllers]objectAtIndex:0];
selectCourseTableViewController.segueFrom=@"Email Import"; // from the storyboard
selectCourseTableViewController.importedFile = _importedFileURL;
}