Use a View Controller as a camera overlay? - iphone

I have an app that loads the camera and overlays buttons for fire, reload, and back, as well as a png that animates when the fire and reload buttons are tapped. How would I load the View Controller, which has a .xib with the buttons and image, as an overlay when the camera view loads?
I have the camera view loading when a button on the main screen is tapped, which used to open another View with the buttons and gun image.
Below is what I have done so far, which is loading the camera. I have a separate View Controller named PlayViewController, with an xib for the interface:
-(IBAction)getCameraPicture:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
PlayViewController* overlay = [[PlayViewController alloc] initWithNibName:#"PlayViewController" bundle:nil];
picker.cameraOverlayView = overlay.view;
[picker setDelegate:overlay];
[self presentModalViewController:picker animated:YES];
}
This is in the PlayViewController.m: code
#import "PlayViewController.h"
#import "SoundViewController.h"
#interface PlayViewController ()
#end
#implementation PlayViewControlleriPad...
`
PlayViewController.h:
#interface PlayViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
two array declarations...
}
#property (weak, nonatomic) IBOutlet UIButton *reloadButton;
#property (weak, nonatomic) IBOutlet UIButton *fireButton;
#property (weak, nonatomic) IBOutlet UITextField *ammoField;
#property (weak, nonatomic) IBOutlet UIImageView *type;
#end

Create your view controller and xib as usual, then add this to your code:
YourOverlay* overlay = [[YourOverlay alloc] initWithNibName:#"YourOverlayView" bundle:nil];
picker.cameraOverlayView = overlay.view;
[picker setDelegate:overlay];
[self presentModalViewController:picker animated:YES];
The overlay must implement UIImagePickerControllerDelegate and UINavigationControllerDelegate

picker.cameraOverlayView = someViewController.view;

Related

This code only works on iPhone Retina 4-inch simulator

I'm developing an iOS 5+ application with latest SDK and I have a custom UIView with a custom XIB.
This is TopMenuView.h:
#import <UIKit/UIKit.h>
#interface TopMenuView : UIView
#property (weak, nonatomic) IBOutlet UIButton *MenuButton;
#property (weak, nonatomic) IBOutlet UILabel *MenuLabel;
#property (weak, nonatomic) IBOutlet UIButton *SearchButton;
#property (weak, nonatomic) IBOutlet UIButton *ProfileButton;
#property (weak, nonatomic) IBOutlet UIButton *HomeButton;
#property (weak, nonatomic) UIViewController* parentController;
- (IBAction)MenuClicked:(id)sender;
- (IBAction)SearchClicked:(id)sender;
- (IBAction)ProfileClicked:(id)sender;
- (IBAction)HomeClicked:(id)sender;
+ (id)topMenuView;
#end
This is TopMenuView.m:
#import "TopMenuView.h"
#import "SearchViewController.h"
#import "ProfileViewController.h"
#import "HomeViewController.h"
#import "SWRevealViewController.h"
#implementation TopMenuView
#synthesize parentController;
+ (id)topMenuView
{
TopMenuView* topView = [[[NSBundle mainBundle] loadNibNamed:#"TopMenuView"
owner:nil
options:nil] lastObject];
// make sure customView is not nil or the wrong class!
if ([topView isKindOfClass:[TopMenuView class]])
{
[topView setFrame:CGRectMake(0, 0, 320, 49)];
return topView;
}
else
return nil;
}
#pragma mark -
#pragma mark IBAction methods
- (IBAction)MenuClicked:(id)sender
{
[self.parentController.revealViewController revealToggle:sender];
}
- (IBAction)SearchClicked:(id)sender
{
SearchViewController* search =
[[SearchViewController alloc] initWithNibName:#"SearchViewController"
bundle:nil];
[self.parentController.revealViewController setFrontViewController:search];
}
- (IBAction)ProfileClicked:(id)sender
{
ProfileViewController* profile =
[[ProfileViewController alloc] initWithNibName:#"MyProfileViewController"
bundle:nil];
[self.parentController.revealViewController setFrontViewController:profile];
}
- (IBAction)HomeClicked:(id)sender
{
HomeViewController* home =
[[HomeViewController alloc] initWithNibName:#"HomeViewController"
bundle:nil];
[self.parentController.revealViewController setFrontViewController:home];
}
#end
And on UIViewController I do this to show it:
- (void)viewDidLoad
{
[super viewDidLoad];
topMenuView = [TopMenuView topMenuView];
topMenuView.MenuLabel.text = #"Home";
topMenuView.parentController = self;
[self.view addSubview:topMenuView];
// Set the gesture to open the slide menu.
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
And this is the view:
This code works perfectly only on iPhone Retina 4-inch simulator. On iPhone Retina 3.5-inch and on iPhone simulator, it doesn't work.
The problem is that I do a click over any of that "buttons" it do nothing. No Touch Inside Up event is throw (I set a debug point inside the IBAction method).
I haven't test it on a real iPhone because I don't have a developer license yet.
What's happening? Why it doesn't work on iPhone 3.5-inch?
You probably have another view which covers over your buttons. Check your xib file for a view that is possibly being expanded by the system when it has a different size screen.
I had the same problem. Everything worked fine, until I uncheck "Use AutoLayout" button in Storyboard. I have UIView with button placed on it. And have action related to this button. Then in iPhone Retina 3.5-inch Simulator it doesn't respond to that action.
Finally, I found the solution in the Storyboard. It sets the height of my view where button is placed to 0. I don't know why. So I just specify its height programmatically.
Hope this will help!

Assign value to textfield firstview take of secondview

I have two views in my application: in main view there are input fields that the user must fill and a button connected to the second view. In the second view there is a table view, when the user selects a row automatically returns to the main view.
My problem is that when you return to the main view the values ​​of text fields are cleared.
Any solutions?
Thank you.
Second View header
#class MainViewController;
#interface ListaViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
{
UITableView *table;
UISearchBar *search;
MainViewController *child;
}
#property (nonatomic, retain) IBOutlet UITableView *table;
#property (nonatomic, retain) IBOutlet UISearchBar *search;
#property (nonatomic, retain) MainViewController *child;
- (IBAction)switchBack:(id)sender;
Second View Implementation:
-(IBAction)switchBack:(id)sender
{
child.selectedCountry = selectedCountry;
child.codiceComune = codiceComune;
[self.navigationController popViewControllerAnimated:YES];
}
First View:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
cityField.text = selectedCountry;
}
This not work!
-(IBAction)switchBack:(id)sender
{
MainViewController *controller = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated: YES];
[controller release];
}
It means you create new MainViewController, not return to previous.
Use this code instead
-(IBAction)switchBack:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
P.S. It works if you go to the second vievcontroller same way, as you write in -(IBAction)switchBack:(id)sender
As a guess without code, you're probably emptying or initialising your text fields in viewDidLoad, viewDidAppear or viewWillappear - do it in your viewController init instead.

App crashes in iPhone simulator when button is pressed. Xcode 3.2.5

My app is a tab bar application. There are two tabs at the bottom and an outlet on the first tab with a button that leads to a new window. When I build the code in xcode, it succeeds. When I launch the app in the simulator and click the button leading to the new window, it crashes the app. This is my code for the "FirstViewController" and the "GuitarBrandsViewController"
FirstViewController.h-
#import <UIKit/UIKit.h>
#import "GuitarBrandsViewController.h"
#interface FirstViewController : UIViewController {
FirstViewController *firstViewController;
IBOutlet UIWindow *window;
IBOutlet UIWindow *GuitarBrands;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
-(IBAction)gotoGuitarBrands;
#end
FirstViewController.m
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize window;
-(IBAction)gotoGuitarBrands{
GuitarBrandsViewController *screen = [[GuitarBrandsViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release];
}
GuitarBrandsViewController.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#interface GuitarBrandsViewController : UIViewController {
GuitarBrandsViewController *guitarBrandsViewController;
IBOutlet UIWindow *window;
IBOutlet UIWindow *Main;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
-(IBAction)gotoMain;
#end
GuitarBrandsViewController.m
#import "GuitarBrandsViewController.h"
#implementation GuitarBrandsViewController
#synthesize window;
-(IBAction)gotoMain{
FirstViewController *screen = [[FirstViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release];
}
I assume you are creating the GuitarBrandsViewController using Interface Builder as the code in the class itself would not work on it's own.
However, when you initialize GuitarBrandsViewController, you don't pass the NIB so you allocate the Controlling Class without the actual NIB information from IB.
Instead of
GuitarBrandsViewController *screen = [[GuitarBrandsViewController alloc] initWithNibName:nil bundle:nil];
Use
GuitarBrandsViewController *screen = [[GuitarBrandsViewController alloc] initWithNibName:#"GuitarBrandsViewController.xib" bundle:nil];
Adjust the nib name to the name of the actual nib you use.

Calling method from another class

To begin, I would like to apologize for my English :)
I have FirstViewController, which contains scrollView. This is scrolView with enabled paging, and have 2 pages with 2 different view controllers. From one of the view controllers by touching the button the third view controller appears like a modal view. I call a method in FirstViewController that must disable scrolling and hide two labels which is not contained in the scrollView.
Method is executing, but UI not changes, scrolling still enabled and labels still visible.
Now a bit of code:
This is a piece of the FirstViewController.h (not the whole code):
#interface FirstViewController : UIViewController <UIScrollViewDelegate> {
IBOutlet UIScrollView *scrollView;
IBOutlet UILabel *label1;
IBOutlet UILabel *label2;
}
#property (nonatomic, retain) UILabel *label1;
#property (nonatomic, retain) UILabel *label2;
#property (nonatomic, retain) UIScrollView *scrollView;
-(void)prepareToModal;
#end
Now it's -(void)prepareToModal; implementation:
-(void)prepareToModal {
[label1 setHidden:YES];
[label2 setHidden:YES];
scrollView.scrollEnabled = NO;
}
So, from the one of the view controllers, which contained in scrollView, I call prepareToModal
Previously:
#import "FirstViewController.h"
Next:
FirstViewController *vc = [[FirstViewController alloc] init];
[vc prepareToModal];
[vc release];
So, that's all. I put a breakpoint in prepareToModal, and it stopped executing. The method is called, but nothing changes on screen...
What am I doing wrong?
How to do this correctly?
Update:
I solved this problem.
When I present this modal view, I wrote this:
ThirdViewController *tvc = [[ThirdViewControler alloc] init];
tvc.delegate = self;
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:tvc];
[self presentModalViewController:nc animated:YES];
[tvc release];
[nc release];
Now, insted of [self presentModalViewController:nc animated:YES]; I write this:
[[[[UIApplication sharedApplication].windows objectAtIndex:0] rootViewController] presentModalViewController:nc animated:YES];
And It's working very well, I don't need a method -(void)prepareToModal;
Thanks a lot :)
Make sure you have hooked up your IBOutlets in Interface Builder.

Is that right if I switch View in this way... (IPhone)

I have a MyAppAppDelegate, it contains a window, and a UITabBarController.
#interface MyAppAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet UITabBarController *rootController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
#end
And I have View A, that contain a button to switch to View B. It is the .h file:
#import <UIKit/UIKit.h>
#class MyAppAppDelegate;
#class ViewBController;
#interface ViewAController : UIViewController {
IBOutlet UIView *view;
IBOutlet UIButton *switchToViewBButton;
}
#property (retain, nonatomic) UIView *view;
#property (retain, nonatomic) UIButton *switchToViewBButton;
-(IBAction) startSwitching: (id)sender;
#end
And it is the.m file:
#import "ViewAController.h"
#import "ViewBController.h"
#import "MyAppAppDelegate.h"
#implementation ViewAController
/*skip the default generated codes*/
-(IBAction) startClock: (id)sender{
NSLog(#"Start Switching");
[rootController presentModalViewController:ViewBController animated:YES];
}
Plz notice that the ViewB is not enable to display on UITabBarController, it only appear, when the ViewA button is clicked. Also, I found that the debugger tell me that the rootController is undeclared. but I already import MyAppDelegate to the file. thz a lot... ...
You need to synthesize the rootController instance:
#synthesize rootController;
Then it should work. Put this line of code below the implementation line in the .m file. There is no reason why you should be getting the second error, so try my solution and then tell us what happened.
Also, please try to write in complete sentences. In my experience, if you write well in a forum post, you will gain more respect from people who might help you.
No you need to do something like this:
ViewBController* vc = [[ViewBController alloc] initWithNib: #"ViewBController" mainBundle: nil];
if (vc != nil) {
[rootController presentModalViewController: vc animated:YES];
[vc release];
}
The mistake that you are making is that you are passing presentModalViewController: the class of the ViewBController. Instead it needs an instance.
ViewBController* viewBController = [[[ViewBController alloc] initWithNibName: #"NameOfViewBControllerNibFile" bundle:nil] autorelease];
[self presentModalViewController:viewBController animated:YES];
You can not access rootController from ViewAController, because it is a property of MyAppAppDelegate, not ViewAController. If you want to access the UITabBarController in charge of ViewAController, then inside ViewAController you use self.tabBarController
So if you want the UITabBarController to do the above, change it to
ViewBController* viewBController = [[[ViewBController alloc] initWithNib: #"NameOfViewBControllerNibFile" mainBundle: nil] autorelease];
[self.tabBarController presentModalViewController:viewBController animated:YES];
ViewBController *vc = [[[ViewBController alloc] initWithNib:#"ViewBController"
mainBundle:nil] autorelease];
MyAppDelegate *appDelegate = (MyAppAppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate.rootController presentModalViewController:vc animated:YES];