0

ということで、いきなり本題に入ります。別のビュー コントローラー (MainViewController) で -(void) を呼び出すには、1 つのビュー コントローラー (FolderViewController) から必要です。呼び出したいメソッドは、ユーザーが選択したフォルダーに基づいてサーバーを更新します。フォルダの 1 つが効率的に選択されるたびに -(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];
        });
    });
}
4

1 に答える 1

0

通知は、このようにまったく関係のないクラス間でシグナルを送る良い方法です。クラス間に多くの依存関係を作成することなく、メッセージを伝えます。FolderViewController は MainViewController へのフル アクセスを必要としないため、それらの間に参照を配置する必要はありません。

FolderViewController のメソッドの最後に、次のように記述します。

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

MainViewController (initまたはviewDidLoad、など)の初期化のどこかに、次のように記述します。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTheServers) name:@"ChangedFolder" object:nil];
于 2013-05-28T02:12:27.947 に答える