I have two UILabels in a view that display my band and song name as strings. I am working on also adding the option to change either of these strings manually. I want to keep it as is, and I've added 2 buttons to manually enter a song name or band name. The thing is, all the text editing as far as I understand it needs to have actual TextField or TextView to bring up the keyboard etc.
I just want to touch one button for "enter song name" and be given a keyboard and when enter is hit, replace the string in the uilabel with that string, and the same for enter band name uibutton, and change the uilabel again.
Problem is in the docs I don't really understand how to do this. Does anyone have an idea about text entry in ios and can give me a pointer/tip on how to do this?
Just Declare your label Global and implement in implementation method
Take all song and all album data in array
and display all data in table view
now by using Delegate method of
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
change the text of Label as u want
#interface songViewController ()
{
UILabel *bandLabel;
UILabel *songLabel;
NSArray *bandData;
NSArray *songData;
}
#end
#implementation songCreateViewController
- (void)viewDidLoad {
[super viewDidLoad];
{
bandLabel=[[UILabel alloc]init];
bandLabel.frame=CGRectMake(40, 580, 500, 30);
bandLabel.text=#"Join group as";
[self.view addSubview:bandLabel];
songLabel=[[UILabel alloc]init];
songLabel.frame=CGRectMake(40, 580, 500, 30);
songLabel.text=#"Join group as";
[self.view addSubview:songLabel];
}
#pragma mark Table view Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==albumTableView)
{
bandLabel.text=[bandData objectAtIndex:indexPath.row];
}
else
if(tableView==songTableView)
{
songLabel.text=[songData objectAtIndex:indexPath.row];
}
#end
What you are going to have to do is replace the label with a text field and set that as the first responder when the user presses the button. You will need a class that is the delegate of the text field so you handle enter in the textFieldShouldReturn method to resign first responder on the text field (to close the keyboard) and change the text field back to the label view and update its contents.
It can be achieve by many ways..
1) you can use hidden in your button methods to hide label and show textView.
2) you can use textField in place of label, make textfield.editable=NO and when button is pressed just clear the textfield by textfield.text=nil and make textfield editable.
Try these..
You want like when user touch on button you want to add text and button should be look like UILable. is it your objective?
If I am right you can achieve this thing to add gesture in uilable, you don't need to add button and handle tab method you can change the text.
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
myView.addGestureRecognizer(tap)
#objc func handleTap(_ sender: UITapGestureRecognizer) {
print("Hello World")
}
Related
Very beginner obj-c question.
I have plain UITableView with two sections, but I am interested only in first section now. This section have four custom cells (inherited from standard UITableViewCell), and they have a UITextField's as a property.
I need to improve custom Input Accessory View with buttons "Next", "Previous"(for switch between textFields in tableview) and "Done" (dismissimg of keyboard). http://uaimage.com/image/62f08045
In -textFieldShouldReturn i set tags for textFields from 0 to 3. My next plan is to add textFields into NSMutableArray in -viewDidLoad and then just set and resign first responder for the textFields. Approximate code listing for "Next" button:
- (void) inputAccessoryViewDidSelectNext:(FDInputAccessoryView *)view {
for (UITextField *textField in [self textFieldsArray]) {
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
UITextField *field = [[self textFieldsArray] objectAtIndex:textField.tag + 1];
[field becomeFirstResponder];
}
}
}
Questions:
Is this a right way or maybe there is a better approach to solve problem?
Do I need to tag textfields or use indexPath of cells in what they are built in? (or what is the best to track textFields?)
And the main question: what is the syntax to "get" textField from cell?
Sorry for the dumb questions, I am a very beginner.
Thanks,
Alex.
I think you have the right idea, but a few things come to mind:
Just to be safe, don't start with tag number 0. Every view has a tag number defaulted to 0, so you may get something unexpected.
Don't set the text view's tags inside of the textFieldShouldReturn, set the tags in cellForRowAtIndexPath or viewDidLoad, wherever you init the textFields.
Add the textFields to the cell's contentView, not the cell itself.
You don't have to resign first responder from the first text field, you can just becomeFirstResponder on the new one.
Make sure you're handling the last text view edge case: You could loop around to the first text field or simply dismiss the keyboard at the end.
If you want to get the textField in the cell:
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:
[NSIndexPath indexPathForRow:ROW_NUMBER inSection:1]];
UITextField *textField = (UITextField *)[cell.contentView viewWithTag:TEXT_FIELD_TAG];
I have a UIVIew which is a subview and it contains several UITextFields. One of these textfields (which is for DATE) should not be editable using the keyboard, instead of this I use a PopOver with a Datepicker inside.
I run a method when the UIControlEventEditingDidBegin is reached. This method calls the resignFirstResponder on the DateTextField.
Everything works fine if the DateTextField is the first field to edit, but when another textField is edited and of course shows the keyboard and then try to edit the DateField, the keyboard doesn't hide and everything goes normal but with the Keyboard doing anything.
I have tried to call the method endEditing:YES before the resignFirstResponder but it doesn't work. I have tried to run the endEditing:YES and resignFirstResponder on the didEndEditing text field method but theres no way to get that keyboard out.
here is my method:
- (void)showDatePopOver:(id)sender{
[self.view endEditing:YES];
UITextField *textField = (UITextField *)sender;
[sender resignFirstResponder]; // hide keyboard
/** POP OVER LINES**/
}
You should use the textFieldShouldBeginEditing: delegate method instead of resigning first responder in didBeginEditing:
This will allow editing on ALL BUT the dateTextField text field:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return (![textField isEqual:dateTextField]);
}
You should specify that your view controller is a text view delegate as well like so (in the interface declaration [.h file]):
#interface MyViewController : UIViewController <UITextFieldDelegate>
At the moment, I trigger a method on 'Did End On Exit' in my app (I'm aware that this may not be the greatest way of doing it but I'm very new to Objective C and Xcode for that matter and I'm simply doing what feels comfortable to me).
This method resigns the firstResponder from the current text field and applies it to a later text field.
The problem I'm facing is that the keyboard covers the next text field so that the use has no idea where the focus is and therefore what they are required to type.
How do I get it so that my keyboard shifts down and actually shows the text box that is currently active? Making something the firstResponder simply doesn't do what I want it to, unless there's part of the implementation I'm missing.
Here's my simple method:
- (IBAction)firstNameNext:(id)sender {
[firstNameTextField resignFirstResponder];
[surnameTextField becomeFirstResponder];
}
Any advice would be super.
Add UIScrollView in your main view then all contents as subview to UIScrollView
Now when specific UITextField needs to be able to visible in view use its delegate like this:
Note: add UITextFieldDelegate in .h file like this
#interface yourViewController : UIViewController<UITextFieldDelegate>
Also bind with File's Owner
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
{
if(textField == yourSpecficTextField) //one u want move upwards
{
yourScrollView.contentOffset = CGPointMake(0,200); //required offset
}
... //provide contentOffSet those who needed
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
yourScrollView.contentOffset = CGPointMake(0,0); //make UIScrollView as it was before
}
If you have keyboard input fields that will be covered by the virtual keyboard, then you need to move those fields out from under the virtual keyboard.
The normal way to do this is to have the controller's view be a scrollable view like UIScrollView. Moving Content That Is Located Under the Keyboard gives a very robust way of adjusting your scroll view and ensuring the required field shows.
I'm creating a custom table that has a button which allows a user to preview a song when pressed. Most of my code works but I haven't figured out how to pass the player a particular song corresponding to the row in which the button was pressed.
For instance: if I have two rows and #1 says Jay Z and #2 says Red Hot Chili Peppers, I want to press the button in #1 to play Jay and to press the button in #2 for the Peppers. Simple. My code is flawed and no matter which row's button I press I can only get the same song to play.
I know why that's happening, but I don't know how to solve it. I'm just wondering if anyone could hit me with a few lines that could point me in the right direction.
I can't use didSelectRowAtIndexPath because I want something else to happen when the row itself is selected.
Will I need to create a method for this or is there something I've overlooked?
Thanks!
You could also set the tag property of each button you create during tableView: cellForRowAtIndexPath:, then when your buttonTapped event is called, look up the sender and find its tag. The tag property of UIView was provided for just this sort of problem.
If you need more information than that, you could create a UIButton subclass that stores any or all information needed about the associated song. Once again, you set that information during cellForRowAtIndexPath, to be retrieved when the button is tapped.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
// Dequeue a cell and set its usual properties.
// ...
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[playButton addTarget:self action:#selector(playSelected:) forControlEvents:UIControlEventTouchUpInside];
// This assumes you only have one group of cells, so don't need to worry about the first index. If you have multiple groups, you'll need more sophisticated indexing to guarantee unique tag numbers.
[playButton setTag:[indexPath indexAtPosition:1]];
// ...
// Also need to set the size and other formatting on the play button, then make it the cell's accessoryView.
// For more efficiency, don't create a new play button if you dequeued a cell containing one - just set its tag appropriately.
}
- (void) playSelected:(id) sender;
{
NSLog(#"Play song number %d", [sender tag]);
}
Something like
- (void)buttonTapped:(UIView *)sender;
{
CGPoint pointInTableView = [sender convertPoint:sender.bounds.origin toView:self.tableView];
NSIndexPath *tappedRow = [self.tableView indexPathForRowAtPoint:pointInTableView];
// get song that should be played with indexPath and play it
}
something like in tableView: cellForRowAtIndexPath: give your button tag as index.row and bind the function below to button's touchup inside event
-(void)button_click:(UIView*)sender
{
NSInteger *index = sender.tag;
//play song on that index
}
I think this will help you!
I have not yet found any really good examples on how to do this. There is an image that I want to use as the accessory button and when I put it in and click on it doesn't work. So it looks correct but doesn't work...
Here is my code:
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
cell.accessoryView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"TableView_Green_Disclosure.png"]];
So how do I get my UIImageView to call accessoryButtonTappedForRowWithIndexPath whenever it is tapped?
A thorough reading of accessoryView and accessoryType would reveal that they are mutually exclusive ways to customize a cell.
Setting the accessoryType will cause the table view delegate method to be called when it is tapped.
Setting the accessoryView will ignore the setting of accessoryType and give you something to display. If you want to receive a callback from the custom view you've put in place, it should be a control that is wired up to do so. (Or any view with a gesture recognizer.)
If you use a button, and set its action to accessoryTapped:, you will receive the button as the "sender" argument. You can walk up the view hierarchy until you find a table view cell, and then ask your table view what the indexPath of that cell is. This will then get you an index into your model objects and you be able to act on it appropriately.
Alternate to the button, you can enable interaction on the UIImageView above, and add a gesture recognizer to it.
To make the button actually do something, you'll need to implement - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath from UITableViewDelegate.
When an accessory button is tapped in a row, this method will be called and you'll have the chance to act appropriately using the passed in index path to determine which row's accessory was tapped.
Check the blog post hdr->cmdline for creating custom accessory view for UITableView.
The author used UIButton objects with images for custom accessory view.
To make use of the accessoryView - you would need to set the cell's accessoryType to UITableViewCellAccessoryNone deposit a UIButton (with associated image) into the cell and then wire it up to receive user touches. You might use something like the code below as the IBAction response to the cell's UIButton being touched:
- (IBAction) accessoryButtonPressed:(id) sender
{
NSUInteger pathInts[] = { 0,0 };
pathInts[1] = self.currentselectedrow; // ivar set when tableview row last selected
NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:pathInts length:2];
[self tableView:mytableview accessoryButtonTappedForRowWithIndexPath:indexPath];
}
The UIButton would be wired to execute this glue code by way of a line inside your tableview's "cellForRowAtIndexPath:" function
[thecell setButtonTarget:self action:#selector(accessoryButtonPressed:)];
One thing I noticed is that the UIButton seems to want a 'swipe right' versus a simple 'tap' touch in order to trigger the event - but it could be my beta iOS that's the problem. Note that I had added a UIButton* object named 'cell_accessoryButton' to the Custom Cell source.
In the cell's source you'd support the 'setButtonTarget' call with code like this:
- (void) setButtonTarget:(MyViewController*)inTarget action:(SEL) inAction
{
[self.cell_accessoryButton addTarget: inTarget
action: (SEL) inAction
forControlEvents: UIControlEventTouchUpInside];
}
It's so much easier to just use the accessoryType reference and let iOS do the heavy lifting - but, if you want a custom graphic, etc - this is another path that works.