textfield not display the text when i click on table cell? - iphone

when i click on tableview cell, its not display the text of that cell into textfield .
i have two xib.
viewcontroller that contain two field name and salary.and that name and salary insert into sqlite ,all, perform well.
next is tableviewcontroller contain table that display sqlite data , when i click on cell its not display in name and slary textfield.
see code ...of tableviewcontroller.m
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewController *vc=[[ViewController alloc]init];
nameStr =[[data objectAtIndex:indexPath.row] valueForKey:#"name"];
salaryStr=[[data objectAtIndex:indexPath.row] valueForKey:#"salary"];
NSLog(#"%#",nameStr);//i got the value here
[vc fillTextfield:nameStr:salaryStr];
[self.navigationController popToRootViewControllerAnimated:YES];
}
viewcontroller.m
-(void)fillTextfield:(NSString*)nm:(NSString*)sal
{
NSLog(#"string : %# %# ",nm,sal);//i also got the perfect value of name and salary here
//name.text=tblViewController.nameStr;
// salary.text=tblViewController.salaryStr;
name.text=nm;
salary.text=sal;
}
but it cant display the text in both textfield....
i also make the outlets and delegate of both textfield .
what can i do here?

you are firstly popping view controller then setting name and salary instead do this:
[vc fillTextfield:nameStr:salaryStr];
[self.navigationController popToRootViewControllerAnimated:YES];
Also check : if name and salary text field are through xib or programmatically , if through XIB check if you have connected viw iboutlet else put any hard coded value in name.text=#"test name"; salary.text=#"testsal"; also check textcolor property if background and text color are same (thats why it may be invisible)
Thanks,

Related

iOS cannot set UITextField text property in UITableViewCell

I have a UITableView of 'people' for my iOS app. I have created an 'add' screen to add new people to my persistent store, which works perfectly.
This add view uses a form created with a UITableView and subclassed UITableViewCells, with a UITextField and UILabel, which although works well, I am very new to iOS programming and feel that this may not be the most efficient way.
I am trying to re-use this add view to be my detail, add, and edit view, and I can successfully set a 'Person' entity as a property in the detail view. I have the following code in my viewDidLoad method, where the adding property is set in the prepareForSegue method in the previous ViewController :
if (self.adding)
{
self.editing = YES;
self.title = #"Add Person";
}
else
{
self.title = self.person.name;
[self setDetail];
}
My problem is that when I try to pre-populate my detail view's fields (my setDetail method), I am unable to set my UITextField text with the name property from my person entity. Here's the code I'm using to retrieve the UITextField and set it's text property with:
form is the UITableView;
UITableViewCell *nameCell = [form cellForRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
UITextField *nameField = (UITextField *)[nameCell viewWithTag:100];
nameField.text = self.person.name;
If I NSLog nameCell it returns (null)
I hope that's enough explanation. Any pointers would help a lot!
Instead of setting in viewDidLoad:, do it in your table view data source methods. i.e
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Deque the nameCell and prepare the cell if its not available.
UITextField *nameField = (UITextField *)[nameCell viewWithTag:100];
nameField.text = self.person.name;
return nameCell.
}
I don't really understand your question, but cellForRowAtIndexPath may return nil because
Return Value
An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range.
Official Document: cellForRowAtIndexPath:

Change label text depending on what Table View Cell was pressed (Storyboard)

I have some cells in a table view that when pressed leads to a view controller that has a label that I want to change.
Example: I have cells representing the biggest citys in America, I press Los Angeles and I get sent to the view controller with the label that changes to a number representing LA. If I go back and press New York the label now displays a number representing New York.
I would guess I give the label and cells identifiers and make some kind of if/else if.
Something like this:
"if 'newyorkcell' isPressed setText 'numbercell' = "12345";"
But with real code.
Thanks in advance!
For this you'll have to make a public outlet of your label in your view controllers header file:
#interface YourViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *yourLabel;
#end
Then you can set that labels text via didSelectRow, for this you have to give your detail view controller a storyboard id (in the storyboard) to be able to access it here in code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"VC"];
detailViewController.yourLabel.text = [self.yourArray objectAtIndex:indexPath.row];
[self.navigationController presentViewController:detailViewController animated:YES completion:nil];
}
Instead of using if/elses you could use an array or something where you have all your data in and access it using its index.

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

how to send a value from view2 to view1

My problem is how to reload the tableview
I have 2 viewcontrollers.
In first Viewcontroller I have one tableview. if I select any row in tableview it goes to second viewcontroller.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
NextPageController *nextView = [[NextPageController alloc] initWithNibName:#"NextPageView" bundle:nil];
[self.navigationController presentModalViewController:nextView animated:YES];
[nextView release];
}
in second view controller I have one textfield. If I enter any value into the textfield I need to disaplay that value into the first viewcontroller tableview.
can any one help me?
Thanks in advance.
give NextPageController a protocol and a delegate,just like this:
#protocol (NextPageControllerDelegate)
-(void)displayString:(NSString *)inputString;
#end
#interface FirstTableViewController : UITableViewController {
id<NextPageControllerDelegate> stringDelegate;
}
#property (nonatomic, assign) id<NextPageControllerDelegate> stringDelegate;
and in the .m file:
#implementation
#synthesize stringDelegate;
then, when you alloc the NextPageViewController, insert this:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
NextPageController *nextView = [[NextPageController alloc] initWithNibName:#"NextPageView" bundle:nil];
nextView.stringDelegate = self;
[self.navigationController presentModalViewController:nextView animated:YES];
[nextView release];
}
of course, your table view controller must conforms to the NextPageControllerDelegate protocol, and give the implementation of
-(void)displayString:(NSString *)inputString;
after that, when you are in nextView, and want to have the table view display that string, you may do this:
if(nil != self.stringDelegate)
[self.stringDelegate displayString:someString];
ant then it's done
Make Global String ,
Assign it in secondViewController and when you pop from secondViewController to FirstViewController , viewWillAppear will call.
make logic such it uses global variable and then reload table ..
you need to have a global NSString variable in appDelegate that stores your text of text filed in second view.
Now, when you come back to previous page, in
- (void) viewWillAppear
{
[yourDataSourceArray addObject:appDelegate.yourGlobalString];
[yourTableView reloadData];
}
and yes make sure that your yourDataSourceArray is NSMutableArray.
Hope it helps you.
You could create a property for an NSString in your first view, and a property for an instance of the first view controller (UITableViewController?) in your second view. Then upon saving or popping or whatever you're doing in the second view controller once you have entered the text you want, you could set the property of the NSString, pop the view, and reload the tableView in viewWillAppear. Alternatively you could use delegation as Lewen described.
create global NSMutableArray.
Store and add all data in that NSMutableArray.
In -(void) viewwillappear of first class, call [tablename reloadData];

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.