我有两个VC,我想将resourceName从HomeViewController传递给SingleWebViewController。但是resourceName变为空。
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。这是什么原因。请建议。
答案 0 :(得分:2)
在prepareForSegue
中,您要为singleWebViewController
分配新内容。如果此控制器与tableView: didSelectRowAtIndexPath:
中已分配的控制器不同(我认为是这样,那么您的属性将被重置)。
相反,这样做:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
singleWebViewController = segue.destinationViewController;
singleWebViewController.resourceName = @"intro";
}
答案 1 :(得分:0)
您在调用resourceName
后设置viewDidLoad
。添加自定义初始化工具initWithResourceName:(NSString *)resourceName
,然后将其设置在那里。或者将代码从viewDidLoad
移至viewDidAppear:
。
答案 2 :(得分:0)
在prepareforsegue
中尝试此值编辑:如果你想让不同的情况传递不同的值,那么就这样做...我假设你想在这里使用相同的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];
}
答案 3 :(得分:0)
尝试:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
HomeViewController *obj = segue.destinationViewController;
if([segue.identifier isEqualToString:@"toSingleWebView"]){
obj.singleWebViewController = singleWebViewController;
}
}