Hide UIPickerview On Done Button in UITableView - iphone

I have UITextField in each Cell of UITableview and I have added UIPickerview as inputView of UITextField and showing with Done button at its tool bar
My question is how can I hide this this pop up (Picker + toolbar) on click of done button ?
and show selected value of picker in text box in particular cell ?
Thanks and Regards
Edit : Code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
PremiumProductsDescriptionCell *cell = (PremiumProductsDescriptionCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[PremiumProductsDescriptionCell alloc] initShoppingCartCellWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
ShopProduct *p = (ShopProduct *)[[ShopProduct GetShoppingCart] objectAtIndex:indexPath.row];
cell.Quantity.text = [NSString stringWithFormat:#"%d",p.Quantity];
UIPickerView *quantityPicker = [[UIPickerView alloc] init];
quantityPicker.dataSource = self;
quantityPicker.delegate = self;
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:
CGRectMake(0,0, 320, 44)];
UIBarButtonItem *doneButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:#selector(hideKeyBoard)];
quantityPicker.tag = indexPath.row;
[myToolbar setItems:[NSArray arrayWithObject: doneButton] animated:NO];
cell.Quantity.inputAccessoryView = myToolbar;
cell.Quantity.inputView = quantityPicker;
cell.Quantity.delegate = self;
return cell;
}
Solved :
I have taken currentTextBox a variable and added following method and resizing its first responder in done button's click :)
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
currentTextBox = textField;
}

UIPopOver cant be dismissed from their class and you need to dismiss it from the calling class.
You have to call dismiss method from popover calling class, when user presses the done button
-(void)doneButtonClikd
{ ParentClass *viewController=[ParentClass alloc]init];
[viewController dismissPopOver];
}
I think this will solve your problem
For your inputview-
-(void)doneButtonclikd
{ [selectedTextfield resignFirstResponder];
}
Dont forget to save the currently selected textfield.

Assuming you put the UIPickerView in a popover, here's how to do it:
UIPopoverController* popover = ....
UIBarButtonItem* doneButton = ....
[doneButton addTarget:self action:#selector(closeMe)
forControlEvents:UIControlEventTouchUpInside]
// ....
- (void)closeMe
{
// Assuming popover is really a field or something...
[popover dismissPopoverAnimated:YES];
}

Use [self.view endEditing:YES] method.

Related

adding a uinavigationcontroller to a view as part of a stack of uiviews

In my iphone app, I first call a controller which stack up a few view controllers based on index. This is done cause based on the user's selection, i will need to show different screens (basically, i have a welcome screen, tabbar view - which is the main app, sign in and sign up pages). As of now, everything works perfectly - I stack them up and remove / switch based on the need. The problem is that i would like to add a nav bar to both the sign in and sign up views. However, when i do that, something weird happens - i see a nav bar, but on TOP of it, there is a white bar as well (half width more or less). How can i add this uinavbar successfully?
Here is the controller that is being called by the AppDelegate:
- (void)viewDidLoad
{
[super viewDidLoad];
myTabBarVC = [[tabBarController alloc] initWithNibName:#"tabBarController" bundle:nil];
[self.view insertSubview:myTabBarVC.view atIndex:0];
myLoginVC = [[loginViewController alloc] initWithNibName:#"loginViewController" bundle:nil];
[self.view insertSubview:myLoginVC.view atIndex:1];
mySignUp = [[SignUpView alloc] initWithNibName:#"SignUpView" bundle:nil];
[self.view insertSubview:mySignUp.view atIndex:2];
myWelcomeView = [[WelcomeView alloc] initWithNibName:#"WelcomeView" bundle:nil];
[self.view insertSubview:myWelcomeView.view atIndex:3];
}
When I add that to one of the view controllers in this same method, it doesnt work, as described above.
myLoginVC = [[loginViewController alloc] initWithNibName:#"loginViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myLoginVC];
[self.view insertSubview:navController.view atIndex:1];
How can I make it work? Please help. Thanks!
ADDING MORE INFO ABOUT THE VIEW WHERE THE KEYBOARD IS ON BY DEFAULT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if( cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
cell.textLabel.text = [[NSArray arrayWithObjects:#"",#"",nil]
objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 280, 21)];
textField1.delegate = self;
textField1.placeholder = #"example#EMAIL.com";
textField1.text = [inputTexts objectAtIndex:indexPath.row/2];
textField1.tag = indexPath.row/2;
cell.accessoryView = textField1;
textField1.returnKeyType = UIReturnKeyNext;
textField1.clearButtonMode = UITextFieldViewModeWhileEditing ;
[textField1 becomeFirstResponder];
textField1.tag = 1;
}
else
{
textField2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 280, 21)];
textField2.delegate = self;
textField2.placeholder = #"password";
textField2.text = [inputTexts objectAtIndex:indexPath.row/2];
textField2.tag = indexPath.row/2;
textField2.returnKeyType = UIReturnKeyDone;
textField2.clearButtonMode = UITextFieldViewModeWhileEditing;
cell.accessoryView = textField2;
[textField1 becomeFirstResponder];
textField1.tag = 2;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
I don't think you should be adding navController.view as a subview to your view controller. I suspect that's a source of the trouble. Instead use a UINavigationBar object.
With regards to your second question, if it was a question and not a remark, about the keyboard being by default, that happens because you set your textField1 as first responder.

subView in DetailViewController?

I have a main table view and a detail view. when i click cell detail view should show the details of that item. now what i wanna do is to create subview and put that subview in detailView instead of creating a detailview for every cell. it ll solve my some other problems. Code looks like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *nextController = [[DetailViewController alloc] init];
int storyIndex = [indexPath indexAtPosition:[indexPath length] -1];
[nextController initWithObjectAtIndex:storyIndex inArray:stories];
NSString *storyTitle = [[stories objectAtIndex:storyIndex] objectForKey:#"title"];
nextController.title = #"Details";
UIBarButtonItem *tempButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
tempButtonItem.title = #"Tillbaka";
self.navigationItem.backBarButtonItem = tempButtonItem ;
[self.navigationController pushViewController:nextController animated:YES];
[nextController release];
}
now how can i create a subview with a label and change text of that label to storyTitle.
thanx in advance.
how can i create a subview with a label and change text of that label to storyTitle
Given a parent view, you don't need to create a new subview just to add a label to the parent. But, assuming you really do want to add a subview with a label to your parent view, then do this:
...
UIView *parentView;
...
UIView *subView = [[[UIView alloc] initWithFrame:mySubViewFrame] autorelease];
UILabel *lbl = [[[UILabel alloc] initWithFrame:myLabelFrame] autorelease];
lbl.text = storyTitle;
[subView addSubview: lbl];
[parentView addSubview: subView];

can i display alert view with the table view with segmented controls in every cell

i want to display the table view in alert view. This table view contains segmented control.
these segmented controls are for on/off the audio, image, text. so there will be 3 cells with segmented controls.
how to do this
please help me out
Thank u
Adding UISegmentedControls (or UISwitches, which I would personally recommend for a simple on/off option) is easy enough to put in table view cells using the accessoryView property.
Your view controller (or whatever object you're using to control this thing) must implement the UITableViewDataSource protocol.
#interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {}
#end
Add the controls in tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* const SwitchCellID = #"SwitchCell";
UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:SwitchCellID];
if( aCell == nil ) {
aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SwitchCellID] autorelease];
aCell.textLabel.text = [NSString stringWithFormat:#"Option %d", [indexPath row] + 1];
aCell.selectionStyle = UITableViewCellSelectionStyleNone;
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
aCell.accessoryView = switchView;
[switchView setOn:YES animated:NO];
[switchView addTarget:self action:#selector(soundSwitched:) forControlEvents:UIControlEventValueChanged];
[switchView release];
}
return aCell;
}
Or, if you're set on segmented controls, replace the UISwitch stuff above with something like:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"On", #"Off", nil]];
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.frame = CGRectMake(75, 5, 130, 30);
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:#selector(controlSwitched:) forControlEvents:UIControlEventValueChanged];
aCell.accessoryView = segmentedControl;
[segmentedControl release];
I would personally put these options in a regular view (probably accessible from an options button which does the flip transition, like many iPhone apps do). I don't think putting it in an alert view is a very good user experience.
However, I have written a few blog posts showing how to put various views into alert views (text field, which is kinda useful, and web view, which is arguably not).
So, if you're really dead set on putting this in an alert view, you can totally do that, like this:
- (void) doAlertWithListView {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Preferences"
message:#"\n\n\n\n\n\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
UITableView *myView = [[[UITableView alloc] initWithFrame:CGRectMake(10, 40, 264, 150)
style:UITableViewStyleGrouped] autorelease];
myView.delegate = self;
myView.dataSource = self;
myView.backgroundColor = [UIColor clearColor];
[alert addSubview:myView];
[alert show];
[alert release];
}
It will look something like this:
The answer above works great, but on the iPad the line myView.backgroundColor = [UIColor clearColor];
doesn't seem to remove the grey rectangular area around the table. I find that the following does the job: tableView.backgroundView.alpha = 0.0;

Can I programmatically fire the "switch to number pad" button on iPhone keyboard

I want to find a way to programmatically fire the selector that causes the iPhone keyboard to switch from letters to numbers. I am aware that I can switch the keyboard type, but I want to know if there is a way to do this without switching the keyboard type.
You cannot switch keyplanes (alphabetic keyplane to numeric keyplane to symbol keyplane) programatically, at least not in any way that is publicly supported. As you mentioned, you can change the keyboard type or appearance of the text input traits of the first responder, but this is different than switching between the different keyplanes, which only the user should be able to do.
Just a workaround:
When you want to change the keyplane of an active keyboard like from alphabet to number pad while editing an UITextField, first set the new value to the keyboardType property of the textfield, next send a resignFirstResponder, finally a becomeFirstResponder message to the textfield. E.g.
textfield.keyboardType = UIKeyboardTypeNumberPad;
[textField resignFirstResponder];
[textField becomeFirstResponder];
Swift:
textField.keyboardType = .numberPad
textField.resignFirstResponder()
textField.becomeFirstResponder()
Hope it helps ;-D
For custom switching keyboard types you can use accessoryView of Keyboard
Code to make the keyboard accessoryView for type switching... (hopefully the snipped code is understandable)
// You can call this in viewDidLoad (initialization of the ABC/Done view)
- (void)setAccessoryViewForKeyboard{
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 44)];
UIBarButtonItem *changeKeyboard = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:self action:#selector(switchKeyboardTypes)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *choose = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Done", #"") style:UIBarButtonItemStylePlain target:_textField action:#selector(resignFirstResponder)];
[toolbar setItems:#[changeKeyboard, space, choose]];
[self.textField setInputAccessoryView:toolbar];
}
// changing the left button text ('ABC' and '123')
- (void)setTitleForSwitchingKeyboardButton{
NSString *firstButtonText = self.textField.keyboardType == UIKeyboardTypeDefault ? NSLocalizedString(#"123", #"") : NSLocalizedString(#"ABC", #"");
[[[(UIToolbar *)self.textField.inputAccessoryView items] firstObject] setTitle:firstButtonText];
}
- (void)switchKeyboardTypes{
if (self.textField.keyboardType == UIKeyboardTypeDefault){
[self setTextFieldKeyboardType:UIKeyboardTypeNumberPad];
} else {
[self setTextFieldKeyboardType:UIKeyboardTypeDefault];
}
}
- (void)setTextFieldKeyboardType:UIKeyboardTypeNumberPad:(UIKeyboardType)keyboardType {
[self.textField setKeyboardType:keyboardType];
if ([_textField isFirstResponder]) {
_changingKeyboardType = YES;
[self.textField resignFirstResponder];
[self.textField becomeFirstResponder];
_changingKeyboardType = NO;
}
[self setTitleForSwitchingKeyboardButton];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (!_changingKeyboardType) {
// you can set the default keyboard type here:
// [self setTextFieldKeyboardType:UIKeyboardTypeNumberPad];
// [self setTextFieldKeyboardType:UIKeyboardTypeDefault];
[self setTitleForSwitchingKeyboardButton];
}
}
Have a look at this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = [ [ [UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0f];
cell.textLabel.textColor = [UIColor whiteColor];
}
UITextField * textField = [ [ UITextField alloc] initWithFrame:CGRectMake(55.0f, 10.0f, 230.0f, 31.0f)];
textField.textColor = [UIColor whiteColor];
textField.delegate = self;
c this care fully ///////////if(indexPath.row == 0)
c this care fully // textField.keyboardType = UIKeyboardTypeDefault;
c this care fully //else
c this care fully // textField.keyboardType = UIKeyboardTypePhonePad;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
[cell.contentView addSubview:textField];
if(indexPath.row == 0)
{
self.sender = textField;
cell.textLabel.text = #"From:";
}
else
{
self.mobileNumber = textField;
cell.textLabel.text = #"To:";
}
[textField release];
if(indexPath.row == 1)
{
UIButton * contactsButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
[contactsButton addTarget:self action:#selector(addContact:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = contactsButton;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Check where I commented "c this care fully"
This will help you to switch keyboards programmatically.
Maybe
keyboardType = .numbersAndPunctuation
will help. It starts the keyboard in numeric mode and you can switch back to alphabet.

Can't get keyboard to resign consistently with UITextView in UITableViewCell

I have a UITextView within a UITableView cell. I have been unable to get the keyboard to consistently resign after editing. Detecting DidEndEditing hasn't worked. Adding my own "done" button to the toolbar brings intermittent results. Advice?
(Note: This is UITextView not UITextField. Thanks)
Do you dismiss the table view's controller after you're done editing? I've encountered a non-deterministic crash that occurred when performing [textView resignFirstResponder] plus a call (something like [self doneClicked:nil]) that would dismiss the view controller that hosted the UITableView.
It would release the UITextView and when the call came back into the UITextView method that originated the didEndEditing call, it would crash or behave inconsistently (since the view had been released)..
The solution was to call everything after some delay:
[self performSelector:#selector(doneClicked:) withObject:nil afterDelay:0.5]
adding the textview to the cell:
cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
managedTextView = [[[UITextView alloc] initWithFrame:CGRectMake(7,8,260, 30)] autorelease];
managedTextView.delegate = self;
managedTextView.scrollEnabled = YES;
managedTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
managedTextView.text=thought.managedthought;
[cell.contentView addSubview: managedTextView];
cell.accessoryType = UITableViewCellAccessoryNone;
done button code:
- (void)saveTextView:(id)sender
{
[managedTextView resignFirstResponder];
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(save:)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
...
}
(the "new" save button is used when saving the entire UITableViewController)