1

I would like to print out the indexPath.row number on the label.text in the detailview by passing the data between the UITableView and the detailview but I am not able to do it correctly.

ViewController.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"


@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
    NSArray *tableItems;
    NSArray *images;

}

@property (nonatomic,retain) NSArray *tableItems;
@property (nonatomic,retain) NSArray *images;

@end

ViewController.m

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize tableItems,images;
- (void)viewDidLoad
{
    [super viewDidLoad];
    tableItems = [[NSArray alloc] initWithObjects:@"Item1",@"Item2",@"Item3",nil];
    images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"clock.png"],[UIImage imageNamed:@"eye.png"],[UIImage imageNamed:@"target.png"],nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return tableItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //Step 1:Check whether if we can reuse a cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    //If there are no new cells to reuse,create a new one
    if(cell ==  nil)
    { 
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        UIView *v = [[UIView alloc] init];
        v.backgroundColor = [UIColor redColor];
        cell.selectedBackgroundView = v;
        //changing the radius of the corners
        //cell.layer.cornerRadius = 10;

    }

    //Set the image in the row
    cell.imageView.image = [images objectAtIndex:indexPath.row];

    //Step 3: Set the cell text content
    cell.textLabel.text = [tableItems objectAtIndex:indexPath.row];

    //Step 4: Return the row
    return cell;

}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    cell.backgroundColor = [ UIColor greenColor];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    [secondViewController printRowNumber:indexPath.row+1];

    NSString *selectedRow = [tableItems objectAtIndex:indexPath.row+1];

    secondViewController.selectedRow = selectedRow;

    //[self.navigationController pushViewController:secondViewController animated:YES];
    [self presentViewController:secondViewController animated:YES completion:NULL];
}
@end

SecondViewController.h

#import <UIKit/UIKit.h>
#import "ViewController.h"


@interface SecondViewController : UIViewController{
    IBOutlet UILabel *lbl;
    NSString *selectedRow;

}
@property (nonatomic,retain) UILabel *lbl;
@property (nonatomic,retain) NSString *selectedRow;

- (IBAction)goBack:(id)sender;

-(void) printRowNumber:(int)num;
@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize lbl,selectedRow;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //[self changeLabel:@"Hello"];
    //lbl.text = @"Hello";
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)goBack:(id)sender{

    //ViewController *firstViewController = [[ViewController alloc] initWithNibName:nil bundle:nil];
    //firstViewController.delegate = self;
    [self dismissViewControllerAnimated:YES completion:NULL];
}

-(void) printRowNumber:(int)num{
    lbl.text = selectedRow;
    NSLog(@"%@",lbl.text);
    NSLog(@"%d",num);
}
-(void) changeLabel:(NSString*)str{
    lbl.text = str;
    //lbl.text = @"Hello";
}
@end

Not sure how to solve this problem.. Need some help on this..

lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • If you put breakpoints, does it go to your `printRowNumber:` method? – user427969 Oct 29 '12 at 05:04
  • Also, I think the `completion` should be `nil` in [self presentViewController:secondViewController animated:YES completion: nil]`; – user427969 Oct 29 '12 at 05:15
  • There is nothing wrong in using `NULL`, it's just that it is valid to send a message to `nil` but not `NULL`. Have a look at this - http://stackoverflow.com/questions/5766208/which-is-the-right-one-nil-or-null-to-mark-no-objective-c-block – user427969 Oct 29 '12 at 22:12

5 Answers5

1

I would like to suggest you to just pass the value from current view controller to the next view controller, don't call the function of next controller from current controller. It will lead to deallocation of object.

just pass the value and push that view controller instantly and enjoy the value. Hope it works for you.

Rajan Balana
  • 3,775
  • 25
  • 42
  • `- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; secondViewController.selectedRow = [tableItems objectAtIndex:indexPath.row+1]; //[self.navigationController pushViewController:secondViewController animated:YES]; }` **Now call that print number function in your viewdidload function of your secondviewcontroller" Give it a try. – Rajan Balana Oct 29 '12 at 05:51
  • Ah.. Understand.. Meaning to say: Calling the function from it's own viewcontroller instead of the parentViewController. But I have a question: Since printNumber has a parameter called num, how do i pass it to SecondViewController? – lakshmen Oct 29 '12 at 05:54
  • You already have that value in **selectedRow** property of secondviewcontroller, isn't it ? – Rajan Balana Oct 29 '12 at 06:05
  • meaning to say, i should just pass it without parameters? Ok,let's say i do not have the selectedRow property, how do i pass the parameter? Sorry to trouble you... Interested to learn more that's all... – lakshmen Oct 29 '12 at 06:13
  • 1
    Okay, I got it, You need to declare one more property in secondviewcontroller which will store the row number. As I can see, you need two values for the second page then you need to have two variables on the second page and pass them the value. Then you can remove that passing parameter from the function. – Rajan Balana Oct 29 '12 at 06:23
1
[secondViewController printRowNumber:indexPath.row+1];
NSString *selectedRow = [tableItems objectAtIndex:indexPath.row+1];
secondViewController.selectedRow = selectedRow;

If you look these three lines of code in you didSelect method, you are calling the method printRowNumber without setting your selectedRow string, so it will not work, try switching the order as:

NSString *selectedRow = [tableItems objectAtIndex:indexPath.row+1];
secondViewController.selectedRow = selectedRow;

[secondViewController printRowNumber:indexPath.row+1];

Manish Agrawal
  • 10,958
  • 6
  • 44
  • 76
1

Call the -(void) printRowNumber:(int)num method in viewDidLoad in SecondViewController and remove [secondViewController printRowNumber:indexPath.row+1]; from rowDidSelect in firstViewController.

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self printRowNumber:[selectedRow intValue]];
}

try this it may helps you....

Venk
  • 5,949
  • 9
  • 41
  • 52
1

Se below, you need to change a little piece of code.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

  SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

  NSString *selectedRow = [tableItems objectAtIndex:indexPath.row+1];

  secondViewController.selectedRow = selectedRow;

  //[self.navigationController pushViewController:secondViewController animated:YES];
  [self presentViewController:secondViewController animated:YES completion:NULL];

  //this is the change you should do.
  [secondViewController printRowNumber:indexPath.row+1];

}
mcabral
  • 3,524
  • 1
  • 25
  • 42
Kamar Shad
  • 6,089
  • 1
  • 29
  • 56
  • 2
    @lakesh actually you were calling the `[secondViewController printRowNumber:indexPath.row+1];` before passing the any value to `secondViewController.selectedRow = selectedRow`. as `Challenger` explained it too. – Kamar Shad Oct 29 '12 at 05:37
  • one more qn: when I click item 1, it shows item 1? how to print the value of the index of the table cell clicked? – lakshmen Oct 29 '12 at 05:40
  • @lakesh just change the piece of code `[secondViewController printRowNumber:indexPath.row]`; – Kamar Shad Oct 29 '12 at 05:43
  • hey @lakesh just chnage the value you were passing to the `printRowNumber` method .you should do in this manner. `[secondViewController printRowNumber:indexPath.row]`; – Kamar Shad Oct 29 '12 at 05:48
  • @lakesh no they are different see you were using `[secondViewController printRowNumber:indexPath.row+1]` and i suggested you `[secondViewController printRowNumber:indexPath.row]` you were adding `+1` value to printNumber method – Kamar Shad Oct 29 '12 at 05:59
  • that i understood... What i am asking is getting the label to print a number like 1 instead of text in the form of Item 1? How do i do that? – lakshmen Oct 29 '12 at 06:01
  • @lakesh actually dude you are aslo getting the number see your method. -(void) printRowNumber:(int)num{ lbl.text = selectedRow; NSLog(@"%@",lbl.text); NSLog(@"%d",num); } here `num` have the same value which you want – Kamar Shad Oct 29 '12 at 06:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18697/discussion-between-lakesh-and-kamarshad) – lakshmen Oct 29 '12 at 06:04
1
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    NSString *selectedRow = [tableItems objectAtIndex:indexPath.row+1];

    secondViewController.selectedRow = selectedRow;

    [self presentViewController:secondViewController animated:YES completion:^{
        [secondViewController printRowNumber:indexPath.row+1];
    }];
}

View in SecondViewController will be created after you present the controller. So if you call -printRowNumber: before you present the controller, your label is still not created.

Oscar
  • 651
  • 5
  • 13