Tôi đang cố gắng làm cho ứng dụng của mình nhớ tab nào được xem lần cuối trước khi ứng dụng thoát, vì vậy ứng dụng sẽ mở ra cùng một tab khi tab được khởi chạy tiếp theo. Đây là chức năng của chức năng điện thoại iPhone: làm thế nào tôi có thể làm điều này?Làm cách nào để nhớ tab được chọn cuối cùng trong UITabBarController?
Trả lời
trong đoàn của UITabBar, ghi đè lên
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
và lưu trữ index mục 's trong NSUserDefaults. Lần tới khi ứng dụng của bạn bắt đầu, hãy đọc ứng dụng từ đó và đặt lại thành được chọn. Một cái gì đó như thế này:
-đầu tiên, bạn sẽ thiết lập một đại biểu cho UITabBar của bạn, như thế này:
tabBarController.delegate = anObject;
-in anObject, ghi đè lên didSelectItem:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setInteger: [NSNumber numberWithInt: tabBarController.selectedIndex]
forKey:@"activeTab"];
[def synchronize];
}
Lưu ý rằng bạn lưu một số NSNumber, vì các giá trị int không thể được nối tiếp trực tiếp. Khi bạn khởi động ứng dụng một lần nữa, nó sẽ đọc và thiết lập các giá trị selectedIndex từ giá trị mặc định:
- (void)applicationDidFinishLaunchingUIApplication *)application {
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
int activeTab = [(NSNumber*)[def objectForKey:@"activeTab"] intValue];
tabBarController.selectedIndex = activeTab;
}
Lưu trữ chỉ mục tab đã chọn trong tùy chọn NSUserDefaults mỗi khi người dùng chọn một tab mới. Sau đó, khi ứng dụng bắt đầu sao lưu, hãy tải giá trị đó từ các tùy chọn và chọn tab đó theo cách thủ công.
Đây là cách tôi đã làm nó. Cả hai phương thức đều nằm trong appDelegate và tabBarController là một biến cá thể.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
//Remember the users last tab selection
NSInteger tabIndex = self.tabBarController.selectedIndex;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger: tabIndex forKey:@"activeTab"];
if (![userDefaults synchronize])
{
NSLog(@"Error Synchronizing NSUserDefaults");
}
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
//Set the tabBarController to the last visted tab
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
int activeTab = [(NSNumber*)[userDefaults objectForKey:@"activeTab"] intValue];
self.tabBarController.selectedIndex = activeTab;
}
mã này không hoạt động đối với tôi –
Tôi subclassed TabBarController và:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.selectedIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"activeTab"];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSUserDefaults standardUserDefaults] setInteger: self.selectedIndex
forKey:@"activeTab"];
}
CẬP NHẬT
In the UITabBarControllerDelegate's Delegate, overwrite
Objective C
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
NSLog(@"%lu",self.tabBarController.selectedIndex);
return YES;
}
In this delegate method you will get last selected index.
Swift 3,2
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
{
print("%i",tabBarController.selectedIndex)
return true
}
Đây không phải là 'UITabBarDelegate', đó là' UITabBarControllerDelegate'. – MoLice
Tôi đã chỉnh sửa câu trả lời của mình. –
Dưới đây là giải pháp Swift 3. Chỉ cần thiết lập các lớp của TabBarController của bạn để RememberingTabBarController
trong Storyboard
import Foundation
import UIKit
class RememberingTabBarController: UITabBarController, UITabBarControllerDelegate {
let selectedTabIndexKey = "selectedTabIndex"
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
// Load the last selected tab if the key exists in the UserDefaults
if UserDefaults.standard.object(forKey: self.selectedTabIndexKey) != nil {
self.selectedIndex = UserDefaults.standard.integer(forKey: self.selectedTabIndexKey)
}
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
// Save the selected index to the UserDefaults
UserDefaults.standard.set(self.selectedIndex, forKey: self.selectedTabIndexKey)
UserDefaults.standard.synchronize()
}
}
- 1. Làm cách nào để đặt tab đã chọn trong UITabBarController bằng StoryBoard?
- 2. Làm cách nào để có tùy chọn được chọn cuối cùng từ nhiều lựa chọn?
- 3. UITabBarController có thể chọn tab thông qua mã không?
- 4. Có cách nào để tạo hộp thoại chọn tệp java nhớ thư mục cuối cùng không?
- 5. Làm cách nào để có được bản ghi cuối cùng trước bản ghi cuối cùng trong mysql?
- 6. Chuyển sang tab hoạt động cuối cùng trong VIM
- 7. Làm thế nào để có được lỗi cuối cùng (WSAGetLastError)?
- 8. StringBuilder: làm thế nào để có được chuỗi cuối cùng?
- 9. Chọn không có tab nào trong UITabBar
- 10. Mở lại tab đã đóng cuối cùng trong Visual Studio
- 11. Làm thế nào để có được hồ sơ cuối cùng
- 12. Cách kiểm tra tab nào được chọn trong VB.net TabControl
- 13. Chọn hàng cuối cùng trong bảng SQL
- 14. : cuối cùng con không chọn phần tử cuối cùng
- 15. Làm thế nào tôi có thể lập trình thiết lập tab được chọn của UITabBarController trong khi cũng kích hoạt shouldSelectViewController trong UITabBarControllerDelegate
- 16. Làm cách nào để có được màu sắc của hình cuối cùng trong matplotlib?
- 17. Làm cách nào để có được 50 tài liệu cuối cùng trong mongoDB?
- 18. Làm cách nào để ngăn trang trống cuối cùng?
- 19. Làm thế nào để chọn TD đầu tiên và cuối cùng trong một hàng?
- 20. Làm thế nào để có được tài liệu được tạo cuối cùng trong couchdb?
- 21. Làm cách nào để in các cột được chọn cách nhau bằng các tab?
- 22. Làm cách nào để lấy id được chèn cuối cùng bằng cách sử dụng api JS?
- 23. mysql chọn từ n hàng cuối cùng
- 24. Trong thực tế, làm thế nào cuối cùng là "sự nhất quán cuối cùng" trong HRD?
- 25. Làm cách nào để lấy URL cuối cùng được tìm nạp bởi cURL?
- 26. Android: làm cách nào để có được chi tiết vị trí 50 ô (tháp) cuối cùng?
- 27. Bảng phân cảnh - UITabBarController
- 28. Làm cách nào để tạo GUI trong tab trong MatLab?
- 29. Làm thế nào để có được ngày cuối cùng của tháng bằng cách sử dụng php
- 30. Làm cách nào để có được 7 ký tự cuối cùng của một chuỗi PHP?
Hãy xem câu hỏi gần đây này, quá: http://stackoverflow.com/questions/1619478/how-can-i-have-my- iphone-app-start-with-a-specific-screen-hiển thị –