UIStepper on IOS 5 - ios5

I can't use UIStepper to take value from UITextfield to increment or decrement stepper.value please help me with this problem.
My code:
-(void)stprPressd:(id)sender
{
float a=[textField.text floatvalue]
[stepper setMinimumValue:0];
[stepper setContinues:YES];
[stepper setWraps:YES];
[stepper setStepValue:1]
textField.text=[NSString stringWithFormat#"%f",stepper.value]
stepper.value=a;
stepper setMaximumValue:10];
}

I think you are setting the control properties in its change event. You should initialize and set the control properties into viewDidLoad method.
- (void)viewDidLoad {
[super viewDidLoad];
// Frame defines location, size values are ignored
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(120, 20, 0, 0)];
// Set action target and action for a particular value changed event
[stepper addTarget:self action:#selector(stepperPressed:) forControlEvents:UIControlEventValueChanged];
[stepper setMinimumValue:0];
[stepper setMaximumValue:10];
[stepper setContinues:YES];
[stepper setWraps:YES];
[stepper setStepValue:1]
[self.view addSubview:stepper];
[stepper release];
}
And your stepperPressed: method should look like this
-(void)stepperPressed:(UIStepper *)sender{
textField.text=[NSString stringWithFormat#"%lf",sender.value]
}

Related

How do I call a method from a UIbutton selector?

How do I call this method from the button selector code I have below:
- (void)displayEditorForImage:(UIImage *)imageToEdit
{
AFPhotoEditorController *editorController = [[AFPhotoEditorController alloc] initWithImage:imageToEdit];
[editorController setDelegate:self];
[self presentViewController:editorController animated:YES completion:nil];
}
here's the UIButton I'm trying to call the method with:
//edit button
UIButton *editButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[editButton addTarget:self
action:#selector(editBtnTouch)
forControlEvents:UIControlEventTouchDown];
[editButton setTitle:#"Effects" forState:UIControlStateNormal];
**// the following line shows the selector where I'm unsure how to call the method from the code above**
if ([[UIScreen mainScreen] respondsToSelector:#selector(displayEditorForImage:)] &&
([UIScreen mainScreen].scale == 2.0)) {
// Retina display
editButton.frame = CGRectMake(220.0, 320.0, 60.0, 40.0);
} else {
editButton.frame = CGRectMake(220.0, 315.0, 60.0, 40.0);
}
[self.view addSubview:editButton];
thanks for the help
When the selector you add to the button is called, the button itself will be passed as the first argument if the selector allows for one. In order to get a reference to your image, you would either need to do this (assuming the image you want is the background image for the button):
- (void)displayEditorForImage:(UIButton*)sender {
UIImage* imageToEdit = [sender backgroundImageForState:UIControlStateNormal];
AFPhotoEditorController *editorController = [[AFPhotoEditorController alloc] initWithImage:imageToEdit];
[editorController setDelegate:self];
[self presentViewController:editorController animated:YES completion:nil];
}
Or you would need to make a custom UIButton that has an extra property for the associated image, and then just access the image through the button in the selector:
- (void)displayEditorForImage:(UIButton*)sender {
if([sender isKindOfClass:[YourCustomButtonClass class]]) {
UIImage* imageToEdit = ((YourCustomButtonClass*)sender).customImageProperty;
AFPhotoEditorController *editorController = [[AFPhotoEditorController alloc] initWithImage:imageToEdit];
[editorController setDelegate:self];
[self presentViewController:editorController animated:YES completion:nil];
}
}
Edit:
You seem to be confused as to how to make a UIButton call a method on when it is tapped. You need to add the object which defines the method as the button's target (in this case self), add the method as the selector, and to called the method when the button is tapped use the UIControlEventTouchUpInside. So your code would be:
[editButton addTarget:self action:#selector(displayEditorForImage:) forControlEvents:UIControlEventTouchUpInside];

Objective C - UIStepper reaction

I tried to ask this question, but somehow I failed to express myself right I guess so I deleted it and started fresh.
I have a hierarchy:- ViewController => UIView => Stepper
Here is my code of the UIView:-
#implementation Controls
#synthesize m_numColumn=_whatever;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
int i;
m_numColumn= [[UIStepper alloc]init];
m_numColumn.frame = CGRectMake(1, 1, 30, 30);
m_numColumn.autorepeat= FALSE;
[m_numColumn setValue:2];
m_displayColumnNum= [[UILabel alloc]init];
m_displayColumnNum.frame = CGRectMake(30, 30, 60, 30);
// m_displayColumnNum.backgroundColor =lgammaf(<#float#>);
// m_displayColumnNum.text= [NSString stringWithFormat:#"aaaaaa"];
i= m_numColumn.value;
m_displayColumnNum.text= [NSString stringWithFormat:#"%d",i];
[self addSubview:m_numColumn];
[self addSubview:m_displayColumnNum];
// Controls.delegate = [m_numColumn.navigationController.viewControllers objectAtIndex:0];
[m_numColumn addTarget:self action:#selector(stepperChanged:) forControlEvents:UIControlEventTouchUpInside];
//[m_numColumn setdelegate];
}
return self;
}
The important line here is:
[m_numColumn addTarget:self action:#selector(stepperChanged:) forControlEvents:UIControlEventTouchUpInside];
What I would like is to react on that event in the UIViewController (parent object). How do I do that?
Is the problem that you don't get the event? I have always used UIStepper with "UIControlEventValueChanged", in your case it should be:
[m_numColumn addTarget:self action:#selector(stepperChanged:) forControlEvents:UIControlEventValueChanged];
there is the point of the controlEvents you are listening to, but there is also a problem with the target
[m_numColumn addTarget:self action:#selector(stepperChanged:) forControlEvents:UIControlEventTouchUpInside];
you shouldn't use "self" which is a reference to your view and not your view controller, either use a "myController" variable or do this method inside your controller

button not displaying views

I have added multiple actions to one button
Play audiofile
Display array of views
Button is playing audiofile but not displaying views.
UIButton *playpauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playpauseButton addTarget:self action:#selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];
[playpauseButton addTarget:self action:#selector(displayviewsAction:) forControlEvents:UIControlEventTouchUpInside];
playpauseButton.frame = CGRectMake(0, 0, 50, 50);
[playpauseButton setImage:[UIImage imageNamed:#"play.png"] forState:UIControlStateNormal];
[playpauseButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
UIBarButtonItem *playpause = [[UIBarButtonItem alloc] initWithCustomView:playpauseButton];
Play Pause Action coding
-(void)playpauseAction:(id)sender
{
if
([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:#"play.png"] forState:UIControlStateNormal];
[audioPlayer pause];
} else {
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
}
}
DisplayviewsAction coding
- (void)displayviewsAction:(id)sender
{
CGRect pageViewRect = self.view.bounds;
pageViewRect = CGRectInset(pageViewRect, 30.0, 30.0);
self.pageViewController.view.frame = pageViewRect;
// Configure the page view controller
UIPageViewController *pageViewController = [[[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]autorelease];
NSArray *viewControllers = [NSArray arrayWithObject:pageViewController];
[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
[self.view addSubview:self.pageViewController.view];
}
Any advice what i m missing in the code or doing wrong.
Thanks for help.
Here are some minor corrections you should make - they may not all help with the problem, but you never know.
Add a completion block here:
[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
I don't remember for sure, but I think setViewControllers is done asynchronously - nothing cannot be added until after the completion handler is done. To add the block, you simply put a ^ and a block of code in (like a normal method):
[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:^{
// your code here.
}];
This may fix the problem, but I cannot be sure until I can get back to my computer.
You should also put an NSLog at the beginning of every method, at least until you are done debugging. That way, you always know what is being called and what isn't. If it is not called, try removing the autorelease from the pageViewController - it may be released because it isn't being used.
I think the issue is in displayviewsAction, try something more simple like:
- (void) displayviewsAction:(id)sender
{
UIView* testView = [[UIView alloc] initWithFrame:CGRectInset(self.view.bounds, 30.0, 30.0)];
testView.backgroundColor = [UIColor greenColor];
[self.view addSubview:testView];
[testView release];
}
That should help you narrow down the problem. Depending on what your final aim is you might not need all those view controllers; instead consider UIView's transitionFromView... for the animation effects.

number pad keyboard not show return key in iphone

I am new to iPhone technology. I've saved a problem in a iPhone application, but when i use textfield keyboard type number pad the keyboard doesn't show a return/done button.
How can I hide the number pad keyboard after entering the number in the textfield? Do you have any solutions for that. Please provide help.
You can do this in this way:
#property (nonatomic, strong) UITextField *textField;
- (void)viewDidLoad {
[super viewDidLoad];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonDidPressed:)];
UIBarButtonItem *flexableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[self class] toobarHeight])];
[toolbar setItems:[NSArray arrayWithObjects:flexableItem,doneItem, nil]];
self.textField.inputAccessoryView = toolbar;
}
- (void)doneButtonDidPressed:(id)sender {
[self.textField resignFirstResponder];
}
+ (CGFloat)toolbarHeight {
// This method will handle the case that the height of toolbar may change in future iOS.
return 44.f;
}
you must be using UIKeyboardTypeNumberPad, try this instead UIKeyboardTypeNumbersAndPunctuation,
It'll not only show the return key but also provide you with some extra punctuations
There is no return or done key in number pad. You can do one thing when user touch outside of the textfield you can hide the keyboard. You should do something like this -
if (there is a touch outside of your textField)
{
[textField resignFirstResponder];
}
This question has already been answered, I know because thats how i got the solution. You have 2 options, first is to hide the keyboard by implementing a touch on the mainview that will send the "finished editing" message. that Hides the keyboard [self.view endEditing:YES];
If you do add the touch listener to the mainview you have to implement a condition so that any other buttons keep working.
What you do want to do to simulate a return key is to actually add it like this:
Register for a keyboard did show notification and add this to the code:
if ([self.passCodeLabel isFirstResponder])
{
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
//doneButton.frame = CGRectMake(0, 163, 257, 257);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:#"doneup.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"donedown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:#selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
NSLog(#"%#",[[UIApplication sharedApplication] windows]);
UIView* keyboard;
NSLog(#"Shared applicaiton windows count:%i",tempWindow.subviews.count);
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
NSLog(#"%#",[keyboard description]);
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:#"<UIPeripheralHostView"] == YES)
{
NSLog(#"Adding return button");
[keyboard addSubview:doneButton];
}
}
}
This will add your own "done" button image to the keyboard (which you can just copy by taking a screenshot of the screen of the blank slot and adding the done text).
Btw the code i pasted works on my layout. For yours you might have to modify it a bit, but the principle is the same.
Create the button
Look for the keyboard subview
Add the button to that subview
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneWithNumberPad)],
nil];
[numberToolbar sizeToFit];
_txt_mobileno.inputAccessoryView = numberToolbar;
}
-(void)doneWithNumberPad
{
[_txt_mobileno resignFirstResponder];
}

How to kill a selector that is set to fire after a delay (on the iPhone)?

If I have a view with a performSelector set to fire after a delay:
[self performSelector:#selector(generateBall) withObject:NULL afterDelay:1.5];
...but I removeFromSuperview that view before the selector fires (for example, due to user interaction), then my app crashes.
Is there a way to kill the delayed selector in the dealloc method for that view?
EDIT:
I've tried both:
[[NSRunLoop mainRunLoop] cancelPerformSelector:theBall target:self argument:nil];
and
[[NSRunLoop currentRunLoop] cancelPerformSelector:theBall target:self argument:nil];
and while both work (allowing me to load a new view), loading the previous view ends up giving me a gray screen.
I haven't been able to find any tutorials or other information about cancelPerformSelector other than those Apple docs that were listed, and the documentation on threads and run loops seem to be very convoluted (mostly because they don't list working code samples, which would make it easier for me to step through and understand what was going on).
Because I'm using performSelector:afterDelay, the only way I've been able to properly "kill" any previously requested but not launched functionality is using:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:theBall object:nil];
The following code sample shows how this works (create a new View template XCode project called "select", and replace the selectViewController.h file with this):
#import "selectViewController.h"
#implementation selectViewController
UILabel *lblNum;
UIButton *btnStart, *btnStop;
int x;
- (void) incNum {
x++;
lblNum.text = [NSString stringWithFormat:#"%i", x];
[self performSelector:#selector(incNum) withObject:NULL afterDelay:1.0];
}
- (void) stopCounter {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(incNum) object:NULL];
}
- (void)viewDidLoad {
x = 0;
lblNum = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
lblNum.textAlignment = UITextAlignmentCenter;
[self.view addSubview:lblNum];
btnStart = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnStart.frame = CGRectMake(40, 270, 240, 30);
[btnStart setTitle:#"start" forState:UIControlStateNormal];
[btnStart addTarget:self action:#selector(incNum) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnStart];
btnStop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnStop.frame = CGRectMake(40, 310, 240, 30);
[btnStop setTitle:#"stop" forState:UIControlStateNormal];
[btnStop addTarget:self action:#selector(stopCounter) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnStop];
[self performSelector:#selector(incNum) withObject:NULL afterDelay:1.0];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[lblNum release];
[super dealloc];
}
#end
-cancelPerformSelectorsWithTarget:
or
-cancelPerformSelector:target:argument:
I've found that this works great:
[NSObject cancelPreviousPerformRequestsWithTarget:self];