开发者

how to push UIImagePickerController into UINavigationController?

开发者 https://www.devze.com 2023-02-18 09:39 出处:网络
I have following coding in my Program, in AppDelegate - 开发者_开发技巧(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

I have following coding in my Program, in 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;
}

Where these three are different classes, now I want to use camera and album, so when I write coding as below on a button target, it won't work

    - (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;
    }
}

I tried all the above ways but they are not working and showing UIImagePickerController,

now what should I do

if anyone in not clear from my question, can ask me again.....


What about [self.view addSubview:imagePicker.cameraOverlayView]

Also, have you tried adding any other view to your self.view? I wonder if the imagePicker is being added, but is not loaded because the view you are adding it to is not being displayed.


Did u try presenting the imagePicker through one of the navigation controllers?

From Reference

Present the user interface by calling the presentModalViewController:animated: method of the currently active view controller, passing your configured image picker controller as the new view controller. On iPad, you can alternatively present the user interface using a popover as described in initWithContentViewController: and “Presenting and Dismissing the Popover” in UIPopoverController Class Reference.


If using GKImagePicker:

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


You can always do:

  let cam        = UIImagePickerController()
  cam.delegate   = self
  cam.sourceType = .camera

  self.show(cam, sender: nil)

worked for me


Did you try presenting the UIImagePickerController through the tabBarController (UITabBarController inherits from UIViewController)? Assuming that you created a singleton for for your AppDelegate and that the tabBarController is a property of the AppController (for your convenience I added a sample code for the singleton below), the code would look something like this:

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];
}

Now in your class, on the useCamera method call the imagePicker by doing this:

- (void)useCamera
{
  // YOUR CODE ...

  // Place image picker
  [[[AppController sharedAppController] tabBarController] 
   presentModalViewController:imagePicker animated:YES];

  // YOUR RELEASE CODE ...
}

Good luck and happy coding!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号