SingleWebViewController

SingleWebViewController

我有两个VC,我想将ResourceName从HomeViewController传递到SingleWebViewController。但是resourceName变为null。

HomeViewController.m

#import "HomeViewController.h"
#import "SingleWebViewController.h"

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    singleWebViewController = segue.destinationViewController;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectedRow = indexPath.row;

    switch (selectedRow)
    {

        case 0:
        {
            singleWebViewController.resourceName=@"intro";
            NSLog(@"HtmlFileName:%@" , singleWebViewController.resourceName);
            [self performSegueWithIdentifier:@"toSingleWebView" sender:self];
            break;
        }
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
     }


SingleWebViewController.h具有以下行

@property (nonatomic,strong)NSString *resourceName;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

        NSString *htmlFile = [[NSBundle mainBundle] pathForResource:self.resourceName ofType:@"html" inDirectory:nil] ;
        NSLog(@"%@se:" , self.resourceName);

        NSURL *url = [NSURL fileURLWithPath:htmlFile    ];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [_webView loadRequest:request];
        _webView.delegate=(id)self;

}


我确实注意到在prepareForSegue之前调用didSelectRowAtIndexPath。是什么原因造成的。请提出建议。

最佳答案

在prepareforsegue中尝试this..pass值

编辑:如果您要为不同的情况传递不同的值,则执行此操作..i m,前提是您想将网关连接到同一ViewController,即此处的SingleWebViewController。

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
        {
            if ([segue.identifier isEqualToString:@"toSingleWebView"]) {
            SingleWebViewController  *singleWebViewController = segue.destinationViewController;
            NSIndexPath *indexPath = [self.tableView indexPathOfSelectedRow];
            if(indexpath.row == 0){
            singleWebViewController.resourceName=@"intro";
            }
            else if(indexPath.row == 1){
               singleWebViewController.resourceName=@"some other value";
            }
            else{
               singleWebViewController.resourceName=@"something else";
            }
            NSLog(@"HtmlFileName:%@" , singleWebViewController.resourceName);
          }
        }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        selectedRow = indexPath.row;

        switch (selectedRow)
        {

            case 0:
            {

                [self performSegueWithIdentifier:@"toSingleWebView" sender:self];
                break;
            }
            case 1:
            {
                [self performSegueWithIdentifier:@"toSingleWebView" sender:self];
                break;
             }
          [tableView deselectRowAtIndexPath:indexPath animated:YES];
     }

10-08 09:18