我有2个视图控制器,我将数据从1传递到2但是2中的NSString为空(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和Google上找到的所有解决方案。计算器
答案 0 :(得分: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];
}
答案 1 :(得分:0)
您需要将didSelect方法中的URL作为属性,然后在prepareSegue方法中访问它
抱歉,我现在无法编写完整的代码:)