1

Hello friends please solve my problem

I have two xib , one xib has textfield and other has tableview that contain data from sqlite.

when i click on cell of tableview its store the data into string object and that string object used in .m file that contain textfield.

but textfield not shown text ..

i check all the things like text color ,background etc etc ...but still there are problem..

i assign simple text like

  nametextfield.text=@"hello";

But this above code is also didn't work .

my real coding in my apps is here:

//tableviewcontroller.m

 -(UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc]
            initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text=[[data objectAtIndex:indexPath.row] valueForKey:@
                     "name"];
    cell.detailTextLabel.text=[[data objectAtIndex:indexPath.row]
                           valueForKey:@"salary"];
    return cell;
}


- (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,salaryStr); //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 value here 
    name.text=nm;
    salary.text=sal;

   //but after this code textfield not shown the text ... :(
}

What should i do ??????

Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38
MAC113
  • 1,444
  • 12
  • 19

2 Answers2

2

@user3007462 you are doing in wrong way.

I assume your Viewcontroller (SomeAddress 0x456456dffg) is a RootViewController and Tableviewcontroller (SomeAddress 0x4563ghg5656) is childViewController.

In this case why are you again intiliazing Viewcontroller? If you again initiliaze you Viewcontroller it will create new intance(some address 0x345345hfh54). And you not showing new instance of Viewcontroller. just moving to previous instace of Viewcontroller

So better use protocol concept to achieve this.

Bhumeshwer katre
  • 4,671
  • 2
  • 19
  • 29
  • Please tell me how can i used ... because i am fresher – MAC113 Dec 02 '13 at 08:46
  • Check this **http://stackoverflow.com/questions/6168919/how-do-i-set-up-a-simple-delegate-to-communicate-between-two-view-controllers** – Kumar KL Dec 02 '13 at 08:47
  • An also this http://stackoverflow.com/questions/10578281/how-to-pass-data-back-from-detail-view-controller-to-uitableview – Bhumeshwer katre Dec 02 '13 at 08:48
  • @user3007462 Above link should explained cleary how to ceate protocol and their delegate. Declare one protocol in Tableviewcontroller and declare one delegate method. While moving to Tableviewcontroller assign that delegate method to Viewcontroller, and define delgate method in ViewController class. Before calling PopToRootViewController from TableViewController call delegate and pass data with in that delegate. – Bhumeshwer katre Dec 02 '13 at 09:35
0

Your need is to set a string to textfield in the previous view controller (here it is the rootviewcontroller) which is already created(initialised).

The problem is that your trying to create a new object of the viewcontroller(which is not the exact object of the rootviewcontroller) and set the textfield of that viewcontroller.

In your case there is need to set the textfield of an existing viewcontroller

one way of achieving this is passing data to previous view-controller using the protocols.

please refer this post for example.

Community
  • 1
  • 1