1

我的应用程序在多个选项卡中运行,其中一个运行带有 Overlayview 的相机。我在相机上有一个关闭按钮,我需要一些关于如何在关闭相机后立即加载上一个选项卡或特定选项卡的帮助。有人可以指出我解决这个问题的方向吗?

4

3 回答 3

2

在进入相机视图之前,您必须保存前一个选项卡的索引,如果相机视图被取消,调用[self.tabBarController setSelectedIndex:prevTabIndex],假设这prevTabIndex是以前选择的选项卡的索引。祝你好运!

编辑: 在您的AppDelegate.hput 属性中previousIndex,如下所示:

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, assign) int previousTabIndex;
@property (strong, nonatomic) UITabBarController *tabBarController;

@end

在您的AppDelegate.m文件中,执行以下操作:

    #import "AppDelegate.h"

#import "FirstViewController.h"

#import "SecondViewController.h"

@implementation AppDelegate
@synthesize previousTabIndex;
- (void)dealloc
{
    [_window release];
    [_tabBarController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    UIViewController *viewController1, *viewController2;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil] autorelease];
        viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil] autorelease];
    } else {
        viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPad" bundle:nil] autorelease];
        viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPad" bundle:nil] autorelease];
    }
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = @[viewController1, viewController2];
    [self.tabBarController setSelectedIndex:0];
    self.tabBarController.delegate = self;
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    self.previousTabIndex = self.tabBarController.selectedIndex;
    return YES;
}

@end

在您的相机视图中,您应该有一些方法会在用户点击取消按钮时被调用,如下所示:

- (IBAction)cancel:(id)sender {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [self.tabBarController setSelectedIndex:appDelegate.previousTabIndex];
}

希望这会有所帮助。如果这对您没有帮助,我真的建议您阅读一些 Objective-C 和 iOS 编程的基础知识。祝你好运!

编辑#2

对于情节提要,在Tab Bar Controller场景中添加Object模板,将其类设置为您的AppDelegate. SetUITabbarControllerdelegate出口到AppDelegate对象。在你的AppDelegate,像我第一次编辑一样添加属性,实现

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    self.previousTabIndex = self.tabBarController.selectedIndex;
    return YES;
}

在您的相机视图中,当用户取消视图时

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [self.tabBarController setSelectedIndex:appDelegate.previousTabIndex];

就这样!祝你好运两次!

于 2012-10-19T05:47:10.693 回答
0

使用tabBarControlllerselectedIndex属性返回或移动到特定选项卡。使用 imagePickerController 的 didFinishPickingMediaWithInfo 移动到特定选项卡

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    imgview.image = [info objectForKey:UIImagePickerControllerEditedImage];
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    self.tabBarController.selectedIndex = yourPreviousIndex or any index
}

编辑像这样保存您的previousIndex:

Add int previousIndex in AppDelegate.h

(void)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
   previousIndex = self.tabBarController.selectedIndex;
}
于 2012-10-19T05:48:24.953 回答
0

这适用于 iOS 9.0

UIStoryboard *myStoryBoard =[UIStoryboard storyboardWithName:[self.storyboard valueForKey:@"name"] bundle:nil];

UITabBarController *tabBarController = [myStoryBoard instantiateViewControllerWithIdentifier:@"tabStoryboardID"];

tabBarController.selectedIndex = 1;

[self presentViewController:tabBarController animated:YES completion:nil];
于 2015-09-29T13:21:28.290 回答