i have my button, created from storyboard:
#property (weak, nonatomic) IBOutlet UIButton *plusOneBtnPoisonTwo;
with its method:
- (IBAction)plusOnePlayerTwoPoison:(id)sender;
then I check device rotation and set new position:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
// NSLog(#"%d", toInterfaceOrientation);
if(!UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
self.plusOneBtnPoisonTwo.center = CGPointMake(284, 406);
}
}
the button moves in the new position, but the method plusOnePlayerTwoPoison is no more triggered...
Any Idea?
Thanks.
Try using this instead. Its just a guess that it may resolve your problem.
#property (retain, nonatomic) IBOutlet UIButton *plusOneBtnPoisonTwo; //change to retain
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//add button action manually by code
[plusOneBtnPoisonTwo addTarget:self action:#selector(plusOnePlayerTwoPoison:) forControlEvents:UIControlEventTouchDown];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
// NSLog(#"%d", toInterfaceOrientation);
if(!UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
[plusOneBtnPoisonTwo setFrame:CGRectMake(284, 406,50,100)];
}
}
Related
Hey I converted my project to ARC automatically and the only thing I had to fix was that Xcode changed array that had a size of [8] to [4] (still don't know what was going on there)
However, I now realized I got another problem: every time I hit a button in my Popup, the app crashed if it is linked to an IBAction. If I remove the reference to the header and main file, the button is clickable, but as soon as i assign it to a method (even if the method is empty), the whole app jut freezes/crashes.
This is how I initiate my popup:
PopUpViewController *popViewController =
[[PopUpViewController alloc] initWithNibName:#"PopUpViewController" bundle:nil];
[popViewController setTitle:#"This is a popup view"];
[popViewController showInView:self.view
animated:YES];
popViewController.view.center=self.view.center;
Pretty basic stuff, I ripped it off a tutorial I found online. My header is this:
#interface PopUpViewController : UIViewController
- (IBAction)baa:(id)sender;
#property (strong, nonatomic) UIButton *ba; // I was trying to add those buttons as properties here
#property (strong, nonatomic) IBOutlet UIButton *kl; //but no luck whatsoever
#property (strong, nonatomic) IBOutlet UIView *popUpView;
#property (strong, nonatomic) IBOutlet UIView *ppp;
- (IBAction)openSomething:(id)sender;
- (IBAction)openYoutube:(id)sender;
- (IBAction)openFacebook:(id)sender;
- (void)showInView:(UIView *)aView animated:(BOOL)animated;
#end
And my main
#import "PopUpViewController.h"
#interface PopUpViewController ()
#end
#implementation PopUpViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)showAnimate
{
// not important for this question
}
- (void)removeAnimate
{
// shortened
}
- (IBAction)baa:(id)sender{
// NSLog(#"asd"); I commented it out, but even an empty method kills the app
// only if I leave a button unassigned to any method, the app "survives" a button click
}
- (IBAction)openSomething:(id)sender
{
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.test.de"]];
}
- (IBAction)openYoutube:(id)sender
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.youtube.com/"]];
}
- (IBAction)openFacebook:(id)sender
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.facebook.com/"]];
}
- (void) doSomething{
// also tried an empty method with a "new" name over here to eliminate any caching errors, no luck
}
- (IBAction)closePopup:(id)sender {
[self removeAnimate];
}
- (void)showInView:(UIView *)aView animated:(BOOL)animated
{
[aView addSubview:self.view];
if (animated) {
[self showAnimate];
}
}
- (void)viewDidLoad
{
self.view.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:.0];
self.popUpView.layer.cornerRadius = 5;
self.popUpView.layer.shadowOpacity = 1.0;
self.popUpView.layer.shadowOffset = CGSizeMake(0.2f, 0.2f);
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
Any ideas what I could do different? I referenced everything to everything else possible now and I don't even know how it looked before anymore, but I somehow think it has something to do with the referencing.
Thanks a lot in advance, Alex
I was able to figure out on my own:
In the header of my main ViewController I added
#property (strong, nonatomic) PopUpViewController *popViewController;
Which still fired the crash. But then I adjusted the popup "opening" block to
_popViewController =
[[PopUpViewController alloc] initWithNibName:#"PopUpViewController" bundle:nil];
[_popViewController setTitle:#"This is a popup view"];
[_popViewController showInView:self.view
animated:YES];
_popViewController.view.center=self.view.center;
(added the underscores) and now it's working perfectly again.
Actually I am curious why it worked before but I am not going to investigate this any further.
I have an UIImage captured from the camera with UIImagePickerController.
Now after the user clicks on it, I'd like it to show full screen and be able to to zoom it in and out using pinch gestures and also the double tap gesture to zoom in a particular area. In other words, I'd like to emulate what the ios's default image browser does.
I display the captured image in an UIImageView with:
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
which makes the image go full screen. But how do I implement zooming. Do I need to do it from scratch using gesture recognizers? Or maybe there's a default image display view with all that implemented I am not aware of?
i have create this effect for one of my app, dont forget to set delegate of your scrollview.
code for .h file
#import <UIKit/UIKit.h>
#interface ImageViewerController : UIViewController<UIScrollViewDelegate>
// The scroll view used for zooming.
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
// The image view that displays the image.
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
// The image that will be shown.
#property (strong, nonatomic) NSString *imageUrlString;
#end
code for .m
#import "ImageViewerController.h"
#interface HNImageViewerController ()
- (IBAction)handleSingleTap:(UIButton*)tapGestureRecognizer;
#end
#implementation ImageViewerController
- (void)viewDidLoad {
[super viewDidLoad];
[self.imageView setImage:[UIImage imageNamed:#"placeholder-image"]];
self.scrollView.delegate=self;
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
#pragma mark - UIScrollViewDelegate methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (self.scrollView.zoomScale == self.scrollView.minimumZoomScale) {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
#pragma mark - Private methods
- (IBAction)handleSingleTap:(UIButton *)tapGestureRecognizer {
[self dismissViewControllerAnimated:YES completion:nil];
}
This is very easy to implement:
- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer;
and then:
- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer {
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}
When the user zooms far enough out of the image by pinching out, then releases, I want it to close the UIImageView/scroll view, basically exiting the picture viewer.
How would I go about doing this? scrollViewDidEndZooming is called after it animates back into place, so I can't use that, and none of the other delegate methods seem helpful.
i have create this effect for one of my app, dont forget to set delegate of your scrollview.
you need to update the private method with your pinch gesture(thats all)
code for .h file
#import <UIKit/UIKit.h>
#interface ImageViewerController : UIViewController<UIScrollViewDelegate>
// The scroll view used for zooming.
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
// The image view that displays the image.
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
// The image that will be shown.
#property (strong, nonatomic) NSString *imageUrlString;
#end
code for .m
#import "ImageViewerController.h"
#interface HNImageViewerController ()
- (IBAction)handleSingleTap:(UIButton*)tapGestureRecognizer;
#end
#implementation ImageViewerController
- (void)viewDidLoad {
[super viewDidLoad];
[self.imageView setImage:[UIImage imageNamed:#"placeholder-image"]];
self.scrollView.delegate=self;
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
#pragma mark - UIScrollViewDelegate methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (self.scrollView.zoomScale == self.scrollView.minimumZoomScale) {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
#pragma mark - Private methods
- (IBAction)handleSingleTap:(UIButton *)tapGestureRecognizer {
[self dismissViewControllerAnimated:YES completion:nil];
}
I am working on a very basic app that displays a popover when the user is entering text into a UITextField. Unfortunately, the popover is not showing up and the default keyboard is appearing (which shouldn't). Here is my relevant code below:
NumberPadViewController.h
#import <UIKit/UIKit.h>
#import "NumberViewController.h"
#interface NumberPadViewController : UIViewController <UITextFieldDelegate> {
IBOutlet UITextField *numTest;
}
#property (nonatomic, strong) NumberViewController *numberPicker;
#property (nonatomic, strong) UIPopoverController *numberPickerPopover;
#end
NumberPadViewController.m
- (BOOL)textFieldShouldBeginEditing:(UITextField *) textField
{
// Create popover controller if nil
if(_numberPickerPopover == nil){ //make sure popover isn't displayed more than once in the view
_numberPickerPopover = [[UIPopoverController alloc]initWithContentViewController:_numberPicker];
}
[_numberPickerPopover presentPopoverFromRect:numTest.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
return NO;
}
My popover class is called NumberViewController.h
#interface NumberViewController : UIViewController {
}
#property (strong, nonatomic) IBOutlet UIButton *oneButton;
NumberViewController.m
#import "NumberViewController.h"
#interface NumberViewController ()
#end
#implementation NumberViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSInteger buttonHeight = _oneButton.frame.size.height * 4;
NSInteger buttonWidth = _oneButton.frame.size.width * 3;
self.contentSizeForViewInPopover = CGSizeMake(buttonWidth, buttonHeight);
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I have created the UITextField in Storyboard, and set the delegate there. Can anyone see what I am doing wrong?
Thanks in advance to all who reply.
Ensure the textFieldShouldBeginEditing delegate method is being called. The rest of the code looks correct.
This is just a thought, but when a popover does a popover thing i think it becomes first responder, but your text view is first responder... so it might not be able to over do it.... if this is the error then before you tell the popover to appear you can say [textView resignFirstResponder]; and see if that helps.... it's just a thought though not 100% sure i will have to do some testing ~
also check to see if _numberPicker is not nil aswell i don't know what happens if you try to display a popover with no controller but you can see if that's the problem
Seeing as your popover isn't represented in the storyboard (if I read your post right) I think you need to add the popover view as a subview in code. Something like:
[self addSubview:_numberPickerPopover];
There are potentially a few places to do this. Probably makes the most sense in your textFieldShouldBeginEditing: method, after you've inited it.
First my .h file:
#interface SettingsViewController : UIViewController <UINavigationControllerDelegate>
{
IBOutlet UISwitch *gravaSwitch;
...
}
#property (retain, nonatomic) UISwitch *gravaSwitch;
...
#end
My viewDidload in .m file (it works):
...
// SET SWITCH BUTTON STATE
keychain = [[KeychainItemWrapper alloc] initWithIdentifier:#"TrafficApp" accessGroup:nil];
if ( [[keychain objectForKey:(id)kSecAttrAccount] isEqualToString:#"" ] )
[self.gravaSwitch setOn:FALSE];
else [self.gravaSwitch setOn:TRUE];
...
But my switchChanged doesn't work and I don't know why. On IB everything is right connected, it enters in this method but gravaSwitch is always null.
- (IBAction)switchChanged:(id)sender
{
if ( self.gravaSwitch.on )
{
NSLog(#"IF");
[self.gravaSwitch setOn:FALSE animated:YES];
}
else
{
NSLog(#"ELSE");
[self.gravaSwitch setOn:TRUE animated:YES];
}
}
Regards.
I think the error is the following:
#interface SettingsViewController : UIViewController <UINavigationControllerDelegate>
{
UISwitch *gravaSwitch;
...
}
#property (retain, nonatomic) IBOutlet UISwitch *gravaSwitch;
...
#end
IBOutlet placeholder has to be inserted into #property. Change your code and try to connect your outlet again.
Edit:
Try to create your UISwitch programatically. Change your #property as the following:
#property (retain, nonatomic) UISwitch *gravaSwitch;
Leave #synthesize as is. Then in your viewDidLoad method add your UISwitch (by default on property is FALSE):
// Width and height are set to zero but they take the default dimension
UISwitch *yourSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];
// add target and action
mySwitch addTarget:self action:#selector(yourCustomAction:) forControlEvents:UIControlEventValueChanged];
self.gravaSwitch = yourSwitch;
// don't forget because gravaSwitch has already a retain policy
[release yourSwitch];
// add the switch to the view
[self.view addSubview:self.gravaSwitch];
Within the same controller, but outside the viewDidLoad method, add the following method:
- (void)yourCustomAction:(id)sender
{
if ( self.gravaSwitch.on )
{
NSLog(#"IF");
[self.gravaSwitch setOn:FALSE animated:YES];
}
else
{
NSLog(#"ELSE");
[self.gravaSwitch setOn:TRUE animated:YES];
}
}
- (void)dealloc
{
self.gravaSwitch = nil; // remember to dealloc the switch!!
[super dealloc]
}
You could call self.gravaSwitch = nil; also in viewDidUnload method.
As an alternative you can set gravaSwicth to assign policy as follow. In this case you haven't to call self.gravaSwitch = nil; both in dealloc and/or viewDidUnload.
#property (assign, nonatomic) UISwitch *gravaSwitch;
Hope it helps.
Edit 2:
This code works for me. This is the implementation (.m file) for MyViewController.
#synthesize gravaSwitch;
- (void)viewDidLoad
{
[super viewDidLoad];
UISwitch *yourSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];
[yourSwitch addTarget:self action:#selector(yourCustomAction:) forControlEvents:UIControlEventValueChanged];
self.gravaSwitch = yourSwitch;
[yourSwitch release];
[self.view addSubview:self.gravaSwitch];
}
- (void)yourCustomAction:(id)sender
{
if(self.gravaSwitch.on)
{
NSLog(#"on");
}
else
{
NSLog(#"off");
}
}
where gravaSwicth is declared within MyViewController.h as follows:
#interface MyViewController : UIViewController
{
UISwitch *gravaSwitch;
...
}
#property (retain, nonatomic) UISwitch *gravaSwitch;
...
#end
rembember to call self.gravaSwicth = nil in dealloc in MyViewController.m!!
FWIW I had this problem when my UISwitch was in a UITableViewCell, and I put it at the top of the list of subviews in Interface Builder (and therefore beneath the other views in the cell when running). It worked in a develop build but not a production build for who knows what reason. In prod, I would click on the UISwitch and the whole row would blink ask if selected.
So I moved it to the bottom of the list (and therefore above the other views in the cell when running) and then I could click on the UISwitch.
-(IBAction)alarmSwitchToggled:(id)sender
{
if ([switchAlarm isOn]) {
NSLog(#"switch is ON");
}
else
{
NSLog(#"switch is OFF");
}
}
Give proper connections in nib file and dont forget to make it as valuechanged instead of touchupinside