【问题标题】:Best way to call a method in a different view?在不同的视图中调用方法的最佳方式?
【发布时间】:2013-05-27 18:16:45
【问题描述】:

所以我要直截了当。我需要从一个视图控制器 (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];
        });
    });
}

【问题讨论】:

  • 你在哪里添加了 mainviewcontroller 的实例?你在用女巫导航吗?
  • 它在我的 MainViewController.m 文件中。
  • 是的,但是在哪里添加了对象引用?窗口,导航标签栏,...
  • 对不起,我不明白。由于切换文件夹等原因,每次我想重新加载 UITableView 时都会调用它。
  • 好吧,如果您有参考,只需在界面中添加方法以使其可见并调用它

标签: iphone methods sdk controller void


【解决方案1】:

通知是在此类完全不相关的类之间发出信号的好方法。它无需在您的类之间创建大量依赖关系即可获得消息。您的 FolderViewController 不需要对 MainViewController 的完全访问权限,因此您真的不想在它们之间放置引用。

在FolderViewController方法的最后,放这个:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangedFolder" object:nil];

在 MainViewController 初始化的某个地方(initviewDidLoad,例如),输入:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTheServers) name:@"ChangedFolder" object:nil];

【讨论】:

    猜你喜欢
    • 2015-04-30
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多