Setting textField Delegate in other controller? - iphone

i have created custom Cell in a controller "CustomCellController". The custom Cell Contains a text Field.
I am Using that CustomCell in another controller "TableViewController"
How can i Set delegate method for the the textField present in "customCellController" in "TableViewController"

if you want the textfield text only ..you can make a new model singleton class and keep the text stored in it for retrieving..
If you want to be the delegate only..One way to do is.
Add a property to the TableViewController of UITextField
add a function such as
-(void) setTextField:(UITextField *) textField
{
self.textProperty = textfield;
self.textProperty.delegate = self;
}
then where you initialize TableViewController
call the method and pass the textfield to it.

Related

How can I dismiss the keyboard programmatically?

I need a way of determining the UITextField that is currently selected in a view. Is this possible without passing a reference or tag?
To be more specific I need to be able to tell which UITextField is selected so that I can hide the keyboard. The reason I need this is because I want to create a UIToolbar to add to all the UITextField's as an input accessory. On this UIToolbar I will add a 'Done' button, when pressed this should hide the keyboard for the currently selected UITextField.
I assume you mean you want to know which UITextField is the first responder (which is the text field that gets input from the keyboard).
There is no public API for this (though there is a private API). You can track which text field is the first responder manually using the textFieldDidBeginEditing: method of each text field's delegate, or you can use a little trickery to find the first responder at any time.
Here's the trick. The UIApplication object knows which object is the first responder, and can send a message to it. So you write a category like this on UIResponder:
UIResponder+firstResponderHack.h
#import <UIKit/UIKit.h>
#interface UIResponder (firstResponderHack)
+ (UIResponder *)firstResponderByHack;
#end
UIResponder+firstResponderHack.m
#import "UIResponder+firstResponderHack.h"
#interface FirstResponderFinder : NSObject
#property (strong, nonatomic) UIResponder *firstResponder;
#end
#implementation FirstResponderFinder
#synthesize firstResponder = _firstResponder;
#end
#implementation UIResponder (firstResponderHack)
- (void)putFirstResponderIntoFinder:(FirstResponderFinder *)finder {
if (self.isFirstResponder)
finder.firstResponder = self;
}
+ (UIResponder *)firstResponderByHack {
FirstResponderFinder *finder = [FirstResponderFinder new];
// Sending an action to nil sends it to the first responder.
[[UIApplication sharedApplication] sendAction:#selector(putFirstResponderIntoFinder:) to:nil from:finder forEvent:nil];
return finder.firstResponder;
}
#end
Then you can find the first responder, and check whether it's a UITextField, like this:
UIResponder *firstResponder = [UIResponder firstResponderByHack];
if (firstResponder && [firstResponder isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)firstResponder;
// do something with textField
}
There is an easy way to dismiss the keyboard without having to track the currently active control, or iterating through all the available controls, or using a UITextFieldDelegate.
[self.view endEditing:YES]
From the docs:
endEditing:
Causes the view (or one of its embedded text fields) to
resign the first responder status.
- (BOOL)endEditing:(BOOL)force
Parameters
force
Specify YES to force the first responder to resign, regardless of whether it wants to do
so.
Return Value
YES if the view resigned the first responder status or NO if it did not.
Discussion
This method looks at the current view and its subview
hierarchy for the text field that is currently the first responder. If
it finds one, it asks that text field to resign as first responder. If
the force parameter is set to YES, the text field is never even asked;
it is forced to resign.
There is a delegate method:
- (void)textFieldDidBeginEditing:(UITextField *)textField
Apple Docs:
This method notifies the delegate that the specified text field just
became the first responder. You can use this method to update your
delegate’s state information. For example, you might use this method
to show overlay views that should be visible while editing.
There is also a property:
#property(nonatomic, readonly, getter=isEditing) BOOL editing
Apple Docs:
A Boolean value indicating whether the text field is currently in edit
mode. (read-only)
Just make an ivar for the UITextView in your header file:
UITextField *editingField;
#property (nonatomic, copy) UITextField *editingField;
Then,
- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
editingField = textField;
// Whatever else you want to do
}
I'm thinking that you need to diff the textFields without reference.
So, the recommended why is using ObjectiveC runtime.
It's pretty straight forward.
Firstly:
#import <objc/runtime.h>
Then, define a char for its address:
static char UITextFieldViewIdentifier;
Then set the identifier with something like this:
objc_setValue(textField, &UITextFieldViewIdentifier, #"Identifier") //typing on a phone, not so sure about the expression
In the delegate method:
NSString *identifier = objc_getObject(textField, &UITextFieldViewIdentifier)
Just call this line where you want to dismiss the keyboard:
[self.view endEditing:YES];

self.tableView insertRowsAtIndexPaths from within tableView delegate

So I thought I'd have a go at building my own simple app. Please go easy on me I'm new to all this! The idea is this. For iPad have a single view controller with a text box and a text field. Text box takes a title, and text field takes the body of a report. There's a button on the page to submit the report, which bundles the two texts into an object and adds it to a table view within the same view controller. I have set the view controller as a delegate with <UITableViewDelegate, UITableViewDataSource> in my header file. My table view works fine for adding items in the viewDidLoad method. But adding items from the text inputs via a UIButton connected to -(IBAction) addItem falls over with: Property 'tableView' not found on object of type 'ReportsViewController'
- (IBAction)addReportItem
{
int newRowIndex = [reports count];
ReportObject *item = [[ReportObject alloc] init];
item.title = #"A new title";
item.reportText = #"A new text";
[reports addObject:item];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}
I understand that I'm trying to call a method within my object but I have other method calls to tableView which work fine. i.e.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [reports count];
}
I thought this was the point of delegation. I know I'm missing something, but as I say I am new to all this and have looked everywhere for an answer before posting. What do I need to do to send my IBAction message to tableView?
Do you have a tableView instance variable setup in your .h file of the view controller?
The reason you are able to access it in the delegate and data source methods is because they are passed in as part if the methods.
You will need to add the IBOUTLET tableView ivar and connect it to the tableView in your .xib.
Or perhaps your ivar for the tableView is named something else?
Good luck.
I had the same problem.
What helped was to inherit the View Controller from UITableViewController, instead of UIViewController. Not using the protocol names in angled brackets.
The TableView is then linked to the dataSource and delegate via the storyboard (resp. InterfaceBuilder).
The parent class UITableViewController has an IBOutlet tableView defined.
MyViewController.h:
#interface MyViewController : UITableViewController

Custom keyboard for input in a custom uitableview cell

I have a view which contains a tableview, (i call this view the EquationView, and its controller the EquationViewController). The table view cells are custom cells with a label and a textfield, representing a variable and its value, receptively. I have been able to get my custom keyboard to apear when editing begins in the textfield.
However, I haven't been able to get the keystrokes to apear in the correct cell, do to the fact that I haven't been able figure out how to send the indexPath of the cell with textfield being edited to the custom keyboard controller class. I have included my EquationViewController.h file in my custom keyboard controller class and have been able to change the text in the tableview's textFields by manually setting the indexPath to a specified row. Here is the keyboard class definition:
#import <UIKit/UIKit.h>
#import "EquationViewController.h"
#interface CustomNumberPadViewController : UIViewController {
EquationViewController *equationViewController;
}
#property (nonatomic, retain) EquationViewController *equationViewController;
-(IBAction)buttonPressed:(id)sender;
#end
My action method looks like this (so far):
-(IBAction)buttonPressed:(id)sender{
VariableCell *cell = (VariableCell *)[equationDetailViewController.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.variableValue.text = #"test";
}
This get the word "test" to appear on whichever row I set in the index path.
So what I'm wondering is, is there a way to pass/get the indexPath of the cell whose textField is being edited to the action method in my custom keyboard controller class?
Why don't you set your UIViewController as the delegate of your UITextFields and catch the textFieldDidBegin: delegate method?

How to pass values from one page to other page and display values in the page's textlabel of cell in iPhone?

I have an application in which when I click on the table item a new page opens, which allows me to enter details in textfield and textview, and clicking on the submit button it should navigate to the previous page which is a tableview and populate the the textlabel of the cell of the table view with the value which are entered in the textfield and textview.
How is this possible? How could I populate the tableview cell with the values entered in the textfield and textview?
Everything depends on how the data are stored in your TableView. For example if you have a plist file you must re-write this file when you submit new value.
After that, on :
- (void)viewWillAppear:(BOOL)animated {}
You must reload plist file and invok[self.tableView reloadData];
However there is an other method. If you have just few values you can use NSUserDefault class Reference to store data in your app.
You Need to use following delegate function:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
objInf.nameLable =[NSString stringWithFormat:#"Hello %# Have a Good Day :)",[listData objectAtIndex:indexPath.row]];
NSLog(#"Next View Value =%#",[listData objectAtIndex:indexPath.row]);
NSLog(#"Next View Value =%#",objInf.nameLable);
[self presentModalViewController:objInf animated:YES];
}
As
objInf is second class's object and nameLable is NSString which you can set as text of label of your second class where u want to display the data..
By using property you can do this. string properties of textfield strings on input page then simply by making object of that page you can access these property.
remember you need singletone object if you make new object then it reintialise the properties.
so use this for making object of that input class.
YourClass *obj= (YourClass *)[self.navigationController.viewControllers objectAtIndex: [self.navigationController.viewControllers count]-2];
this gives object from navigation stack.
That can be achieved by 2 ways.
One is, You can have 2 NSString property in another ViewController. 1 Method to assign data in that. like
#property (nonatomic,retain) NSString *textfieldValue; and Synthesize it.
#property (nonatomic,retain) NSString *textviewValue; and Synthesize it.
-(void) contentToDisplay: (NSString *)pTxtFieldValue txtViewValue(NSString*)pTxtViewValue{
self.textfieldValue = pTxtFieldValue;
self.textviewValue = pTxtViewValue;
}
While switching to another ViewController by submitting use its object to assign text like
[objViewController contentToDisplay:#"textfieldvalue" txtViewValue:#"textviewValue"];
Hope it helps.

UITextField Delegate in UITableView

G'day guys, I'm building an application that allows users to rapidly input numbers into a UITableView.
I've currently built the framework, but I'm having a bit of a hitch hooking up the view to have the keyboard load and make the text in the cell editable from a user action.
I remember there being an iOS example in the dev code somewhere, (like a year ago so it wouldn't be under NDA) where you added items in and edited them within the context of the UITableView without going to a detailed subview.
Just need a hint on how to hook up the delegates, or how to structure the code.
I have code where I create custom cells for a UITableView which have UITextField and UITextView controls on them. In the UITableViewController code I do this:
Interface
#interface MyTableViewController:
UITableViewController <UITextFieldDelegate, UITextViewDelegate> {
....
}
Implementation
-(UITableViewCell *) tableView:(UItableView *) tableView
cellForRowAtIndexPath: (NSIndexPath *) indexPath {
....
UITableViewCell * cell = ....
cell.myTextField.delegate = self;
cell.myTextField.tag = 1; //This should be unique.
return cell;
}
-(void) textFieldDidEndEditing: (UITextField * ) textField {
// Decide which text field based on it's tag and save data to the model.
}
-(void) textViewDidEndEditing: (UITextView * ) textView {
// Decide which text view based on it's tag and save data to the model.
}
You can add a UITextField or UITextView to any cell. If you have custom cells, make them a delegate for their text view, or if you compose the cells in your table view delegate, then make this the text fields' delegates.