所以我会直言不讳。我需要从一个视图控制器(FolderViewController)能够在我的其他视图控制器(MainViewController)上调用 - (void)。我想调用的方法将根据用户选择的文件夹刷新服务器。每次有效地挑选其中一个文件夹时,我如何调用 - (void)?
这是我的didselectPathAtIndexRow:在我的FolderViewController上:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MainViewController *demoController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"server"] isEqualToString:@"imap.mail.me.com"]){
if (indexPath.row == 0)
theAppDelegate.folder = @"INBOX";
if (indexPath.row == 1)
theAppDelegate.folder = @"Sent Messages";
if (indexPath.row == 2)
theAppDelegate.folder = @"Drafts";
if (indexPath.row == 3)
theAppDelegate.folder = @"Deleted Messages";
if (indexPath.row == 4)
theAppDelegate.folder = @"Archive";
if (indexPath.row == 5)
theAppDelegate.folder = @"Junk";
} else {
if (indexPath.row == 0)
theAppDelegate.folder = @"INBOX";
if (indexPath.row == 1)
theAppDelegate.folder = @"[Gmail]/Starred";
if (indexPath.row == 2)
theAppDelegate.folder = @"[Gmail]/Sent Mail";
if (indexPath.row == 3)
theAppDelegate.folder = @"[Gmail]/Drafts";
if (indexPath.row == 4)
theAppDelegate.folder = @"[Gmail]/Trash";
if (indexPath.row == 5)
theAppDelegate.folder = @"[Gmail]/All Mail";
if (indexPath.row == 6)
theAppDelegate.folder = @"[Gmail]/Spam";
}
}
这是我的MainViewController上的 - (void):
- (void)refreshTheServers {
[self.menuContainerViewController toggleLeftSideMenuCompletion:^{}];
KeychainItemWrapper* keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"KeychainTest" accessGroup:nil];
NSString *userForConnect = [NSString stringWithFormat:@"%@", [keychain objectForKey:(__bridge id)(kSecAttrAccount)]];
NSString *passForConnect = [NSString stringWithFormat:@"%@", [keychain objectForKey:(__bridge id)(kSecValueData)]];
CTCoreAccount *account = [[CTCoreAccount alloc] init];
dispatch_queue_t taskQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(taskQ, ^{
[account connectToServer:[[NSUserDefaults standardUserDefaults]
stringForKey:@"server"]
port:993
connectionType:CTConnectionTypeTLS
authType:CTImapAuthTypePlain
login:userForConnect
password:passForConnect];
dispatch_async(dispatch_get_main_queue(), ^{
// ... the thread is over, do stuff on the main thread ...
CTCoreFolder *inbox = [account folderWithPath:theAppDelegate.folder];
NSArray *inboxMessages = [inbox messagesFromUID:1 to:0 withFetchAttributes:CTFetchAttrEnvelope];
[[self allMessages] removeAllObjects];
[[self allMessages] addObjectsFromArray:inboxMessages];
[self.messagesTable reloadData];
});
});
}
答案 0 :(得分:0)
通知是在这样相当不相关的类之间发出信号的好方法。它通过不会在类之间创建大量依赖关系来获取消息。您的FolderViewController不需要对MainViewController的完全访问权限,因此您不需要在它们之间添加引用。
在FolderViewController的方法结束时,输入:
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangedFolder" object:nil];
在MainViewController初始化的某个地方(init
或viewDidLoad
,例如),请输入:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTheServers) name:@"ChangedFolder" object:nil];