需求:從通信錄點擊到人物詳情頁面,點擊聊天后進入聊天詳情頁面 ,進入聊天詳情後 點擊返回後或者是向右滑動返回,都要回到消息列表而不是回到人物詳情頁面!微信
那到需求後,仔細研究了下微信的作法,發現微信從人物詳情頁面跳轉到聊天詳情後,返回按鈕顯示的字是 "消息" 而不是 "人物詳情" 因此想到其實他是從消息頁面推過去的.(其實在這以前我嘗試了許多方法,沒有成功而已.....) .ide
剛開始使用的是通知 post
// NSMutableDictionary *dict = @{}.mutableCopy; // [dict setObject:[self.otherId description] forKey:@"otherId"] ; // [dict setObject:[self.userInfoDic objectForKey:@"user_name"] forKey:@"groupName"]; // [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToChatVC" object:nil userInfo:dict]; // // [self.tabBarController setSelectedIndex:2]; // [self.navigationController popToRootViewControllerAnimated:NO];
在發送消息的時候發送了一條通知 , 而後在消息列表頁面添加了觀察者:動畫
//- (void)push2ChnatVC:(NSNotification *)noti { // ChatViewController *chatView = [[ChatViewController alloc] initWithChatter:noti.userInfo[@"otherId"] isGroup:NO]; // chatView.groupName = noti.userInfo[@"groupName"]; // chatView.hidesBottomBarWhenPushed = YES; // [self.navigationController pushViewController:chatView animated:YES]; //}
這種方法能夠大體實現這種效果,第一次的時候推頁面的時候還有動畫,後面不知爲什麼莫名的沒有了動畫效果....code
通過掙扎以後又想到了另一個方法,(其實一開始就應該想到的,也不用費這麼大勁了..)orm
可使用tabbarController的子viewController(NavigationController)來直接推啊.!!!圖片
具體實現以下: ip
ChatViewController *chatVC = [[ChatViewController alloc] initWithChatter: [NSString stringWithFormat:@"%@", self.otherId] isGroup:NO]; chatVC.groupName = [self.userInfoDic objectForKey:@"user_name"]; chatVC.hidesBottomBarWhenPushed = YES; HXNavigationController *nav = self.tabBarController.viewControllers[2]; chatVC.groupName = [self.userInfoDic objectForKey:@"user_name"]; chatVC.hidesBottomBarWhenPushed = YES; self.tabBarController.selectedViewController = nav; [self.navigationController popToRootViewControllerAnimated:NO]; [nav pushViewController:chatVC animated:YES];
點擊消息 先拿到消息列表的導航,而後使用該導航push ChatVC ,而後再 [self.navigationController popToRootViewControllerAnimated:NO]; 這樣就能夠保證通信錄導航的棧內就只有rootVC了,這樣一來 再點擊通信錄的時候依然是通信錄的主頁面.string
從消息列表push過去後 ChatVC 的右tabbarItem 就顯示消息,點擊消息返回或者向右滑動返回就能夠順利的切換到消息列表了..! it
如此就能夠輕鬆的實現像微信同樣 從聊天列表直接跳轉到消息列表了!!!而且支持滑動手勢.
注:用這種方法須要先跳到列表頁,而後從列表頁推界面,因此有那麼一瞬間是能看到消息列表的(時間很短).. 而微信作的效果則是 看起來跟從人物詳情頁跳轉到聊天列表同樣 . 因此我猜測他們應該是在跳轉以前將詳情頁面截圖,而後放到消息列表頁面,而後再跳轉到聊天頁面(跳轉成功以後把圖片移除).這樣一來看起來就跟直接從人物詳情頁面直接跳轉同樣了.
經過此次問題解決讓我對 tabbarController的子控制器以及導航的子控制器都是那些,以及他們的聲明週期如何瞭解的更深入了! 之後再遇到相似問題就能順利的解決了,嘿嘿嘿!