2013-05-27 64 views
0

所以我要直截了當地說。我需要從一個視圖控制器(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)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

你在哪裏從mainviewcontroller添加實例?女巫導航你使用? –

+0

它在我的MainViewController.m文件中。 –

+0

是的,但是在哪裏添加了對象引用?窗口,uinavigation標籤欄,... –

回答

0

通知是相當無關之間信號的好方法類似這樣的課程。它通過獲取消息而不會在類之間創建很多依賴關係。你的FolderViewController不需要完全訪問你的MainViewController,所以你不需要在它們之間添加引用。

在FolderViewController方法結束時,把這個:

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

在某處,MainViewController(initviewDidLoad,EG)的初始化,把這個:

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