【问题标题】:Tab Index Correct in App Delegate Class but Not in App Delegate object选项卡索引在 App Delegate 类中正确,但在 App Delegate 对象中不正确
【发布时间】:2018-02-03 00:38:42
【问题描述】:

我有一个基于标签的应用程序。其中一个标签是相机。当用户选择取消时,我希望应用程序返回到之前所在的选项卡。我通过在我的应用程序委托类中保存以前的选项卡索引来做到这一点。在我的应用程序委托类的shouldSelectViewController 中,它记录了正确的索引。但是,在我的相机类中,它总是说之前的索引是0

    @interface LeafletAppDelegate : NSObject <UIApplicationDelegate, UIAlertViewDelegate, UITabBarDelegate> {

        NSManagedObjectModel *managedObjectModel;
        NSManagedObjectContext *managedObjectContext;       
        NSPersistentStoreCoordinator *persistentStoreCoordinator;
        UIWindow *window;

    }

    @property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
    @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
    @property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
    @property (strong, nonatomic) UITabBarController *tabBarController;
    @property (nonatomic, assign) NSUInteger previousTabIndex;

//Appdelegate.m 
#import "CameraViewController.m"
#pragma mark UITabBarController Delegate Methods
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    self.previousTabIndex = tabBarController.selectedIndex;
    NSLog(@"this is previous tab index: %lu", (unsigned long)self.previousTabIndex);
    return YES;
}


//CameraViewController.m 
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
    LeafletAppDelegate *appDelegate = (LeafletAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSLog(@"previous tab index: %lu", (unsigned long)appDelegate.previousTabIndex);
    [self.tabBarController setSelectedIndex:appDelegate.previousTabIndex];
}

为什么它会在我的应用委托类中打印正确的索引,但当我在我的相机类的应用委托对象中访问该属性时却不打印?

【问题讨论】:

  • 可能是因为您存储信息的对象与您尝试从中读取信息的对象不同。您可以使用调试器查看对象地址。或使用NSLog,例如NSLog(@"%@", self),打印对象地址。
  • 你是对的!但是我将如何访问正确的(相同的)应用程序委托对象?我看到的每个线程都只显示YouAppDelegateClass *appDelegate = (YourAppDelegateClass *)[[UIApplication sharedApplication] delegate];
  • appDelegate 应该只有一个,这才是找回它的正确方法。所以很难说。

标签: ios objective-c uitabbarcontroller uiimagepickercontroller


【解决方案1】:

不要为此使用 AppDelegate。正如类名所说,它仅用于应用程序委托。

你必须从 UITabBarController 创建一个子类,比如:

 @interface LeafletTabBarController : UITabBarController <UITabBarControllerDelegate>

注意这个类实现了UITabBarControllerDelegate,所以你需要在viewDidLoad里面设置self.delegate = self

另外,在这个子类中,您可以在视图控制器中创建属性@property (nonatomic) NSUInteger previousTabIndex 和取消操作:

LeafletTabBarController *tabBarController = (LeafletTabBarController*)self.tabBarController;
        [tabBarController tabBar:tabBarController.tabBar didSelectItem:tabBarController.tabBar.items[tabBarController.previousTabIndex]];

别忘了设置你当前的标签栏类,别忘了在你的 LeafletTabBarController 更新 - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 中的 previousTabIndex;

希望对你有帮助!

【讨论】:

  • 在我的相机视图控制器中,`LeafsnapTabBarController * tabBarController = (LeafsnapTabBarController*)self.tabBarController; [self.tabBarController setSelectedIndex:tabBarController.previousTabIndex];` 而是感谢这个工作!
猜你喜欢
  • 1970-01-01
  • 2016-10-30
  • 1970-01-01
  • 1970-01-01
  • 2014-07-24
  • 1970-01-01
  • 1970-01-01
  • 2010-09-25
  • 2014-06-26
相关资源
最近更新 更多