【发布时间】: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