数据不会传输到下一个视图控制器

时间:2013-11-11 08:44:08

标签: ios objective-c segue uivewcontroller

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog((@"This is didSelectRowAtIndexPAth"));
    DetailViewController *detailViewController = [[DetailViewController alloc] init];
    detailViewController.myDictionary = [self.placeArray objectAtIndex:[indexPath row]];

    /* The below NSlog is showing the content , its not null still in next viewcontroller same variable showing null content*/

    NSLog(@"My Dictionary: This is a MasterView%@",detailViewController.myDictionary);
    [self performSegueWithIdentifier:@"showDetail" sender:self];
}

7 个答案:

答案 0 :(得分:3)

这是因为您DetailViewController的呈现实例与您手动创建的实例不同。你应该自己呈现新的ViewController(模态或推送),或者实现prepareForSegue方法并在那里设置字典。

修改

2个解决方案:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailViewController = [[DetailViewController alloc] init];
    detailViewController.myDictionary = [self.placeArray objectAtIndex:[indexPath row]];

    // If it is a modal
    [self presentViewController:detailViewController animated:YES];
}

OR

@implementation MyClass {
    NSIndexPath *selectedCellIndexPath;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog((@"This is didSelectRowAtIndexPAth"));
    selectedCellIndexPath = indexPath;

    [self performSegueWithIdentifier:@"showDetail" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showDetail"]) {
        DetailViewController *detailViewController = (DetailViewController *)segue.destinationViewController;
        detailViewController.myDictionary = [self.placeArray objectAtIndex:selectedCellIndexPath.row];
    }
}

答案 1 :(得分:3)

try this

声明一个NSInteger变量全局地将该varble分配给indexpath.row

@interface DashbordViewController ()
    {

        NSInteger       SelectedRownum;

    }
    @end

    @implementation DashbordViewController
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
               }
        return self;
    }

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

         - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
        {

                SelectedRownum=indexPath.row
                [self performSegueWithIdentifier:@"showDetail" sender:self];
        }

        - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
            {

                if ([[segue identifier] isEqualToString:@"showDetail"])
                {

                    NSLog((@"This is didSelectRowAtIndexPAth%@",SelectedRownum));
                    DetailViewController *detailViewController = [segue  destinationViewController];
                    detailViewController.myDictionary = [self.placeArray objectAtIndex:SelectedRownum];


            }


        }

答案 2 :(得分:1)

在DetailViewController中检查myDictionary是否是声明为弱或强的属性..

答案 3 :(得分:1)

试试这个。 。 。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
      NSLog((@"This is didSelectRowAtIndexPAth"));

      UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:@"myStoryBoardName" bundle:nil];
      self.detailsViewController = [mystoryboard instantiateViewControllerWithIdentifier:@"detailviewcontroller"]

     /* The below NSlog is showing the content , its not null still in next viewcontroller same variable showing null content*/

     NSLog(@"My Dictionary: This is a MasterView%@",detailViewController.myDictionary);
     [self performSegueWithIdentifier:@"showDetail" sender:self];
}

你必须在storyBoard中设置detailViewController的标识符。

答案 4 :(得分:1)

您需要在prepareForSegue:method中分配字典。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    DetailViewController *viewController = = (DetailViewController *)[segue destinationViewController];
    detailViewController.myDictionary = //YOUR Dictionary here;

}

这样可以在viewDidLoad中获取相同的字典:DetailViewController。

注意:确保分配字典。

希望它有所帮助。

答案 5 :(得分:0)

由于我无法看到你的控制器的实现(是否是UIViewController的子类),我不能说很多,但请记住initWithNibName:bundle:是UIViewController的指定初始值设定项而不是{{ 1}}。 所以,这是第一个问题。在任何情况下,请在设置属性之前检查实例。实例很可能是零。

编辑:

它们很可能是您从现有实例化新的View Controller。在这种情况下,您可以添加新视图控制器作为子控制器或从现有控制器提供新控制器。在这两种情况下,现有的属性都可以从新的属性到达initparentViewController,这使得故事相反:您将现有控制器的属性值转换为新控制器。

答案 6 :(得分:0)

您正在didSelectRowAtIndexPath中创建新的视图控制器,最后您正在调用prepareForSegue方法...这将创建新的实例,因此您必须设置要发送的数据在这个prepareForSegue方法中的新视图控制器中。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // 
    if ([[segue identifier] isEqualToString:@"name_of_ur_segue"])
    {
        // Get reference to the destination view controller
        DetailViewController *detailViewController = [segue  destinationViewController];
         NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        // Pass any data here ...
        detailViewController.myDictionary = [self.placeArray objectAtIndex:[indexPath row]];
    }
}

编辑:

当您在ViewDidLoad方法和iOS 7中访问数据时,在ViewDidLoad方法中无法访问此类传递的数据。由于视图控制器表示逻辑现在有点改变。现在我们可以在initMethod中传递数据,如果我们想在viewDidLoad中访问它。 或者在viewWillAppearviewDidApepar中访问此权限。 如果你只想在viewDidAppear或ViewWillApepar中调用这段代码,只需设置一个布尔标志,它可以告诉你是否要访问它。