2013-12-03 202 views
0

我有2個視圖控制器,我將數據從1傳遞到2,但2中的NSString是空的(null)我做錯了什麼?從視圖控制器傳遞的數據返回null

視圖控制器1 .M

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    NSLog(@"Method 1"); 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"Method 2"); 
    NSLog(@"%lu", (unsigned long)[allPosts count]); 
    return [[[allPosts objectForKey:@"posts"] valueForKey:@"title"] count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"Method 3"); 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text = [NSString stringWithFormat:@"%@", [[[allPosts objectForKey:@"posts"] valueForKey:@"title"] objectAtIndex:indexPath.row]]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    OldPostViewController *vc = [[OldPostViewController alloc] init]; 
    NSString *url = [NSString stringWithFormat:@"server/app/post.php?id=%@", [[[allPosts objectForKey:@"posts"] valueForKey:@"id"] objectAtIndex:indexPath.row]]; 
    vc.postURL = url; 
    NSLog(@"%@", vc.postURL); 
    [self performSegueWithIdentifier:@"viewPost" sender:self]; 
} 

視圖控制器2·H

#import <UIKit/UIKit.h> 

@interface OldPostViewController : UIViewController <UIWebViewDelegate> 
@property (weak, nonatomic) IBOutlet UIWebView *blogView; 
@property (strong, nonatomic) NSString *postURL; 

@end 

視圖控制器2的.m

NSLog(@"URL: %@", _postURL); 

NSString *fullURL = _postURL; 
NSURL *url = [NSURL URLWithString:fullURL]; 
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
[blogView loadRequest:requestObj]; 

請任何人告訴我我可以做什麼,因爲我嘗試了所有可以在Google上找到的解決方案& Stackoverflow

回答

3

你不能這樣做。當您撥打performSegueWithIdentifier:時,不會使用您本地初始化的vc

執行prepareForSegue:這樣做。像:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"viewPost"]) 
    { 
     OldPostViewController *vc= (OldPostViewController *)[segue destinationViewController]; 
     vc.postURL = yourURL; 
    } 
} 

或者

你可以不喜歡它:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    OldPostViewController *vc = [[OldPostViewController alloc] init]; 
    NSString *url = [NSString stringWithFormat:@"server/app/post.php?id=%@", [[[allPosts objectForKey:@"posts"] valueForKey:@"id"] objectAtIndex:indexPath.row]]; 
    vc.postURL = url; 
    NSLog(@"%@", vc.postURL); 
    [self presentViewController:vc animated:YES completion:nil]; 
} 
+0

您好,感謝快速回復,我已經實現了didSelectRowAtIndexPath方法方法和一切工作只是視圖不不顯示屏幕只是黑色是什麼意思。 –

+0

@ user2802926:聲明'vc'屬性,而不是本地實例,然後嘗試 –

+0

嗨,我將它初始化爲本地屬性,並且在嘗試呈現視圖控制器時崩潰 –

0

您需要將didSelect方法中的URL作爲屬性,然後在prepareSegue方法中訪問它

對不起,我現在不能寫完整的代碼:)

相關問題