Tuesday, January 31, 2012

iOS 5 : UIStepper Control

There is a new pre-built control in iOS 5 for incrementing or decrementing a value, UIStepper. The control has two buttons aligned horizontally, one labeled with a plus (+) the other a minus (-).
One nice feature is a continuous option, wherein if the user presses and holds down either +/- button, the stepper value is incremented repeatedly. The longer the button is held down, the faster the increment will occur. Also, you can have the increment value wrap around when a range of values is specified, for example, if you set min and max to 0 and 99 respectively, when the value reaches 99, the next value will be 0 (and vice-versa).

Below is a screenshot that shows the output of the example code below:

Below is a short code example showing the various properties available in UIStepper:
- (void)viewDidLoad
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 100, 30)];
[label setTextColor:[UIColor blackColor]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:UITextAlignmentLeft];
[label setText: @"Значение:"];
[[self view] addSubview:label];
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(120, 20, 0, 0)];
[stepper addTarget:self action:@selector(stepperPressed:) forControlEvents:UIControlEventValueChanged];

[stepper setMinimumValue:0];
[stepper setMaximumValue:99];

[stepper setWraps:YES];

[stepper setContinuous:NO];

[stepper setStepValue:2];

[[self view] addSubview:stepper];

- (void) stepperPressed:(UIStepper *)stpr{
[label setText: [NSString stringWithFormat:@"Значение:%2.0f", stpr.value]];

Show the UITableView textField cell above the Keyboard when typing

I have an app where, in Interface Builder, I set up a UITableView that has a text field near the bottom of the view. When I run the app and try to enter text into that field, the keyboard slides up overtop of the field so I can’t see what I’m typing until I hide the keyboard again.
Most of the iPhone App developers come across this situation. I found the solution for this issue.


- (UITableViewCell *)tableView:(UITableView *)mytableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell.delegate = self;
cell.indexPath = indexPath;
cell.rightTextField.tag = (NSInteger)indexPath;
//Disables UITableViewCell from accidentally becoming selected.
cell.selectionStyle = UITableViewCellEditingStyleNone;

– (void)keyboardWasShown:(NSNotification*)aNotification
//when keyboard is up, that time just bring your text filed above the keyboard
NSIndexPath *indexPath = (NSIndexPath*)self.actifText.tag;
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.m_myTable.contentInset = contentInsets;
self.m_myTable.scrollIndicatorInsets = contentInsets;
[self.m_myTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{ // Once keyboard is hidden then bring back your table into your original position.
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.m_myTable.contentInset = contentInsets;
self.m_myTable.scrollIndicatorInsets = contentInsets;