简体   繁体   中英

What am i doing wrong in passing this string to label in a view controller segue (ios)?

I have a table view controller segueing (with identifier feedToPollQuerySeg ) to another view controller. I am trying to print out the index number of the row selected in a label in the latter.

Feed View:

@interface FeedController3 : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) IBOutlet UITableView* feedTableView;


-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(NSString*)stringNum{
        if([segue.identifier isEqualToString:@"feedToPollQuerySeg"]){


        PollQueryController *pqc = [[PollQueryController alloc] init];
        pqc = [segue destinationViewController];
        NSLog(@"This is row sending: %@", stringNum);
        pqc.parentRowSelected.text = stringNum;
        }

    }

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

            NSInteger rowSelected = indexPath.row;
            NSString *stringNum = [NSString stringWithFormat:@"%d", rowSelected];
            NSLog(@"This is row selected: %@", stringNum);
         [self performSegueWithIdentifier: @"feedToPollQuerySeg" sender: stringNum];

    }

PollQuery View

@interface PollQueryController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *parentRowSelected;


@implementation PollQueryController
@synthesize parentRowSelected;

-(void)viewDidLoad{
    [super viewDidLoad];

    NSLog(@"Parent Row Selected: %@", parentRowString);

...
}

But the label isn't updating...?

This is row selected: 1
This is row sending: 1
Parent Row Selected:

Its because when you are in prepareForSegue of FeedController3 , PollQueryController's view isn't initialized. Add NSString in PollQueryController set NSString value in prepareForSegue .

In viewWillAppear of PollQueryController assign text to parentRowSelected label.

Like This -

In the viewController that will present the another viewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqual:@"feedToPollQuerySeg"])
    {
        PollQueryController *pvc = [segue destinationViewController];
        pvc.myString = stringNum;
    }
}

And inside the presented viewController:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.parentRowSelected.text = self.myString;
}

Here is a neater way to achieve what you want:

FeedController3.m :

@interface FeedController3

@property (nonatomic) int rowSelected;

@end

@implementation

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     self.rowSelected = indexPath.row;
     NSLog(@"This is row selected: %@", [NSString stringWithFormat:@"%d", self.rowSelected]);
     [self performSegueWithIdentifier:@"feedToPollQuerySeg" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"feedToPollQuerySeg"])
    {
        // Get reference to the destination view controller
        PollQueryController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        vc.rowSelected = self.rowSelected;
    }
}

@end

PollQueryController.h :

@interface PollQueryController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic) int rowSelected;
@property (weak, nonatomic) IBOutlet UILabel *parentRowSelected;

@end

PollQueryController.m :

@implementation

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];    
    self.parentRowSelected.text = [NSString stringWithFormat:@"%d", self.rowSelected];
}

@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM