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;
}
澄清的要點:您應該在Cocoa中大寫NAMES類的第一個字母和所有名稱中所有其他單詞的第一個字母。你的措詞不清楚。當你說「所有單詞」的讀者可能會認爲你的意思是所有單詞。 – 2015-02-09 00:34:14
謝謝!我更新了答案 - 這個問題中的代碼已經使用了很好的約定來處理實例變量, – Sheamus 2015-02-09 00:54:22
謝謝你的幫助。如何在上下文中通過didReceiveRemoteNotification獲取_friendsTable的句柄?它在選擇了正確的選項卡後顯示在屏幕上。我應該保存對UITableView的引用嗎?或者我可以在運行時到達那裏,而不必在屏幕上保存對它的引用? – Eric 2015-02-09 22:13:37