2013-07-10 109 views
0

我試圖通過使用prepareForSegue方法數據到webViewController,它看起來像:故障傳遞數據通過PrepareForSegue

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([segue.identifier isEqualToString:@"RBOM"]) 
    { 
     WebViewController *destViewController = segue.destinationViewController; 
     destViewController.url = [NSURL URLWithString:@"https:myURL.com/Articles.asp?ID=274"]; 
    } 
    if([segue.identifier isEqualToString:@"Home"]) 
    { 
     WebViewController *destViewController = segue.destinationViewController; 
     destViewController.url = [NSURL URLWithString:@"http://www.myURL.com"]; 
    } 
} 

這是爲什麼我webViewController類的樣子:

#import "WebViewController.h" 

@interface WebViewController() 

@end 

@implementation WebViewController 
@synthesize webView; 
@synthesize url; 

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

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.navigationController setNavigationBarHidden:NO]; 
    self.title = @"MyURL.com"; 
    self.webView.delegate = self; 
    NSLog(@"URL: %@", self.url); <---- This is null 


    NSURLRequest *requestURL = [NSURLRequest requestWithURL:url]; 
    [self.webView loadRequest:requestURL]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

@end 

這裏是webViewController.h

#import <UIKit/UIKit.h> 

@interface WebViewController : UIViewController <UIWebViewDelegate> 
@property (weak, nonatomic) IBOutlet UIWebView *webView; 
@property (weak, nonatomic) NSURL *url; 



@end 

T他的問題是我的網址爲空。有人能告訴我爲什麼嗎?

+0

你的url屬性是如何聲明的?強大?弱? –

+0

弱,看到更新的代碼。 – BlackHatSamurai

+0

@NicholasHart,你修好了!如果你添加這個答案,我會接受。 – BlackHatSamurai

回答

1

對於您的UI元素,通常可以聲明它們較弱,因爲您的視圖會保留它們。對於其他ivars,您希望將它們聲明爲強大的,以便您的類實例將保留它們。在這個特殊情況下,你的url屬性很弱,所以NSURL沒有被保留,並且在prepareForSegue:範圍消失之後被設置爲零。

+1

我基本上說過同樣的事情:( – JohnVanDijk

0

你一定會遇到prepareForSegue這種麻煩。 我建議你把你想在你的源傳遞的數據的強勁實例:

NSURL *tempURL = [NSURL URLWithString:@"https:myURL.com/Articles.asp?ID=274"]; 

然後在prepareForSegue只能這樣做:

destViewController.url = tempURL; 

我以前也遇到過類似的問題,這似乎解決了它。我對這個原因沒有清晰的描述。