1

我正在收聽與特定朋友的對話中有新消息的APN。當它收到時,我想選擇我的標籤導航中的第二個選項卡,然後選擇傳入有效負載中指定的對話。我得到它的工作,假設我們已通過身份驗證,並顯示適當的選項卡,但現在我想選擇表中適當的單元格,我不知道該怎麼做。如何從應用程序委託中選擇一個UITableView項目?

此外,我想選擇一個特定的控制器,而不是硬編碼的事實,這是選項卡2,但我也無法做到這一點。

這裏是它的肉:

UITabBarController *tabViewController = (UITabBarController *) self.window.rootViewController; 
tabViewController.selectedIndex = 1; 

這裏是更多的背景:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)launchOptions 
{ 
    DDLogDebug(@"didReceiveRemoteNotification got launch options: %@", launchOptions); 

    NSDictionary *apsInfo = [launchOptions objectForKey:@"aps"]; 
    NSString *message = [apsInfo objectForKey:@"alert"]; 

    NSNumber *fromUserId = [launchOptions objectForKey:@"from_user"]; 
    if (fromUserId) { 
     DDLogDebug(@"Got message from friend id %@", fromUserId); 
     [self showConversationWithfriendIdWhenPossible:fromUserId message:message]; 
    } 
} 

- (void)showConversationWithfriendIdWhenPossible: (NSNumber *)friendId message:(NSString *) message { 
    FriendMatch *friend = [FriendMatch MR_findFirstByAttribute:@"id" withValue:friendId]; 

    DDLogDebug(@"Got friend %@", friend.firstName); 

    if (message) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" 
                 message:message 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 

    if (![self.window.rootViewController isKindOfClass:[UITabBarController class]]) { 
     self.showConversationWithfriendId = friendId; 
     DDLogDebug(@"Not showing the conversation because the controller is %@", NSStringFromClass([self.window.rootViewController class])); 
     return; 
    } 
    self.showConversationWithfriendId = nil; 
    DDLogDebug(@"val: %@", (UITabBarController *) self.window.rootViewController); 
    UITabBarController *tabViewController = (UITabBarController *) self.window.rootViewController; 
    tabViewController.selectedIndex = 1; 
} 

回答

0

首先,你應該爲每個字的第一個字母在你的類名,所以friendMatch會成爲FriendMatch。這將允許您輕鬆地區分實例變量和類,併爲您提供更多的理智。

錶行選擇

我假設你有一些朋友的列表的地方 - 我稱之爲friends。此代碼選擇該行,並將表格滾動到所選內容,以便選擇出現在所顯示的單元格的中間。排序表格後,此操作也很有用。

int numFriends = friends.count; 
for (int i = 0; i < numFriends; i++) 
{ 
    NSDictionary *dict = friends[i]; 
    if (dict == matchedFriend) 
    { 
     NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:i inSection:0]; 
     [_friendsTable scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO]; 
     [_friendsTable selectRowAtIndexPath:selectedIndexPath animated:FALSE scrollPosition:UITableViewScrollPositionNone]; 

     break; 
    } 
} 

選項卡選擇

對於您的標籤欄中選擇一個標籤,我會用每一個標籤欄項映射到一些有意義的名字,使用字典的方法。這樣,您可以選擇給定的選項卡,而不管其位置。

+0

澄清的要點:您應該在Cocoa中大寫NAMES類的第一個字母和所有名稱中所有其他單詞的第一個字母。你的措詞不清楚。當你說「所有單詞」的讀者可能會認爲你的意思是所有單詞。 – 2015-02-09 00:34:14

+0

謝謝!我更新了答案 - 這個問題中的代碼已經使用了很好的約定來處理實例變量, – Sheamus 2015-02-09 00:54:22

+0

謝謝你的幫助。如何在上下文中通過didReceiveRemoteNotification獲取_friendsTable的句柄?它在選擇了正確的選項卡後顯示在屏幕上。我應該保存對UITableView的引用嗎?或者我可以在運行時到達那裏,而不必在屏幕上保存對它的引用? – Eric 2015-02-09 22:13:37

相關問題