在视图控制器segue(ios)中将此字符串传递给标签时,我做错了什么?

时间:2014-01-21 04:58:40

标签: ios uilabel uistoryboardsegue

我有一个表视图控制器segueing(带有标识符feedToPollQuerySeg)到另一个视图控制器。 我正在尝试打印出后者标签中所选行的索引号。

Feed视图:

@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];

    }

民意调查视图

@interface PollQueryController : UIViewController <UITableViewDataSource, UITableViewDelegate>

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


@implementation PollQueryController
@synthesize parentRowSelected;

-(void)viewDidLoad{
    [super viewDidLoad];

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

...
}

但标签没有更新......?

    这是行选择:1
    这是行发送:1
    选择父行:

2 个答案:

答案 0 :(得分:3)

因为当您prepareForSegue FeedController3时,PollQueryController's视图未初始化。 在NSString中的PollQueryController设置NSString值中添加prepareForSegue

viewWillAppear PollQueryController parentRowSelected将文字分配给- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqual:@"feedToPollQuerySeg"]) { PollQueryController *pvc = [segue destinationViewController]; pvc.myString = stringNum; } } 标签。

喜欢这个 -

在viewController中将呈现另一个viewController:

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

    self.parentRowSelected.text = self.myString;
}

在呈现的viewController里面:

{{1}}

答案 1 :(得分:2)

这是实现您想要的更简洁的方式:

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