【问题标题】:how to push UIImagePickerController into UINavigationController?如何将 UIImagePickerController 推入 UINavigationController?
【发布时间】:2011-03-18 04:39:07
【问题描述】:

我的程序中有以下编码,在 AppDelegate 中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    myList = [[List alloc] init];
    mySettings = [[Settings alloc] init];
    myCriteria = [[Criteria alloc] initWithStyle:UITableViewStyleGrouped];

    first = [[UINavigationController alloc] initWithRootViewController:myList];
    second = [[UINavigationController alloc] initWithRootViewController:mySettings];
    third = [[UINavigationController alloc] initWithRootViewController:myCriteria]; 

    myTabBar = [[UITabBarController alloc] init];

    myTabBar.view.frame = CGRectMake(0,20,320,460);


    UITabBarItem *first1 = [[UITabBarItem alloc] initWithTitle:@"List" image:[UIImage imageNamed:@"list.png"] tag:0];
    UITabBarItem *second2 = [[UITabBarItem alloc] initWithTitle:@"My Profile" image:[UIImage imageNamed:@"settings.png"] tag:1];
    UITabBarItem *third3 = [[UITabBarItem alloc] initWithTitle:@"Criteria" image:[UIImage imageNamed:@"Criteria.png"] tag:2];


    UINavigationController *localNavigationController = [[UINavigationController alloc] initWithRootViewController:myTabBar];

    first.tabBarItem = first1;
    second.tabBarItem = second2;
    third.tabBarItem = third3;

    myControllerArray = [[NSArray alloc] initWithObjects:first, second, third, nil];

    [myTabBar setViewControllers:myControllerArray];
    [myTabBar setCustomizableViewControllers:myControllerArray];

    [self.window addSubview:myTabBar.view];

    // Add the view controller's view to the window and display.
   // [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

这三个是不同的类,现在我想使用相机和相册,所以当我在按钮目标上编写如下代码时,它不起作用

    - (void) useCamera
{

    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeCamera])
    {
        NSLog(@"If is true");
        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];

    //  [self.navigationController pushViewController:imagePicker animated:YES];

        imagePicker.delegate = self;
        imagePicker.sourceType = 
        UIImagePickerControllerSourceTypeCamera;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                  (NSString *) kUTTypeImage,
                                  nil];
        imagePicker.allowsEditing = NO;

        [self.tabBarController setCustomizableViewControllers:[[NSArray alloc]initWithObjects:imagePicker,self,nil]];
        [self.tabBarController setViewControllers:[[NSArray alloc]initWithObjects:imagePicker,self,nil]];

        //[self presentModalViewController:imagePicker animated:YES]; I tried

        //[self.view addSubview:imagePicker.view]; I tried
        //self.view=imagePicker.view; I tried


        //[myScrollView addSubview:imagePicker]; I tried
        //[myView addSubview:imagePicker]; I tried

        [imagePicker release];

        newMedia = YES;
    }
}

我尝试了上述所有方法,但它们无法正常工作并显示 UIImagePickerController,

现在我该怎么办

如果有人对我的问题不清楚,可以再问我.....

【问题讨论】:

  • 您是否尝试通过其中一个导航控制器展示 imagePicker?
  • [self.navigationController pushViewController:imagePicker animated:YES];,我做了这个,你在说这个吗?
  • 试试 presentModalViewController
  • 试试这个:[self presentModalViewController:imagePicker animated:YES]
  • @veer 你有没有使用 imagePickerviewControllerDelegate

标签: iphone uinavigationcontroller uiimagepickercontroller


【解决方案1】:

[self.view addSubview:imagePicker.cameraOverlayView]

另外,您是否尝试将任何其他视图添加到您的self.view?我想知道是否正在添加 imagePicker,但未加载,因为您将其添加到的视图未显示。

【讨论】:

  • 如果你使用 imagePicker.view 而不是 imagePicker.cameraOverlayView 它也可以在相机不存在时工作,就像在模拟器上一样。
【解决方案2】:

您是否尝试通过其中一个导航控制器呈现 imagePicker?

来自Reference

通过调用当前活动视图控制器的presentModalViewController:animated: 方法来呈现用户界面,将您配置的图像选择器控制器作为新的视图控制器传递。在 iPad 上,您也可以使用弹出框呈现用户界面,如 initWithContentViewController:UIPopoverController 类参考中的“呈现和关闭弹出框”中所述。

【讨论】:

  • @2KV7,在你的 xcode 中尝试我的编码,你会看到你建议的答案有什么问题......
  • 您当前活动的视图控制器是哪个?您调用 useCamera 的那个?
  • 它在 TabBarItem 上是第二个,然后只需单击一个按钮并调用一个将我带到相机源的 UIImagePickerController 的方法,但它并没有让我做到这一点
  • 你可以试试那个tabbaritem的navigationcontroller吗?
【解决方案3】:

如果使用 GKImagePicker:

[self.view addSubview:self.imagePicker.imagePickerController.view];

【讨论】:

    【解决方案4】:

    你总是可以这样做的:

      let cam        = UIImagePickerController()
      cam.delegate   = self
      cam.sourceType = .camera
    
      self.show(cam, sender: nil)
    

    为我工作

    【讨论】:

      【解决方案5】:

      您是否尝试通过 tabBarController 呈现 UIImagePickerControllerUITabBarController 继承自 UIViewController)?假设您为 AppDelegate 创建了一个单例,并且 tabBarController 是 AppController 的一个属性(为方便起见,我在下面为单例添加了示例代码),代码将如下所示:

      AppController.h

      #import <UIKit/UIKit.h>
      
      @interface AppController : NSObject <UIApplicationDelegate> {
        UITabBarController *tabBarController;
      }
      // Class methods for convenience
      + (AppController *)sharedAppController;
      
      // *** Properties
      @property (nonatomic, retain) IBOutlet UIWindow *window;
      @property (nonatomic, retain) UITabBarController *tabBarController;
      
      @end
      

      AppController.m

      #import "AppController.h"
      
      static AppController *sharedInstance;
      
      @implementation AppController
      
      @synthesize window=_window;
      @synthesize tabBarController;
      
      #pragma mark - Initialization
      - (id)init
      {
        // Create the sharedInstance of the application
        if (sharedInstance) {
          NSLog(@"Error: You are creating a second AppController");
        }
        [super init];
        sharedInstance = self;
      
        return self;
      }
      
      + (AppController *)sharedAppController
      {
        return sharedInstance;
      }
      
      - (void)dealloc
      {
        [_window release];
        [tabBarController release];
        [super dealloc];
      }
      

      现在在您的班级中,在 useCamera 方法上通过执行以下操作调用 imagePicker:

      - (void)useCamera
      {
        // YOUR CODE ...
      
        // Place image picker
        [[[AppController sharedAppController] tabBarController] 
         presentModalViewController:imagePicker animated:YES];
      
        // YOUR RELEASE CODE ...
      }
      

      祝你好运,编码愉快!

      【讨论】:

        猜你喜欢
        • 2011-11-28
        • 2013-05-07
        • 1970-01-01
        • 1970-01-01
        • 2015-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多