无法在视图控制器之间传递数据

时间:2013-09-24 13:11:41

标签: ios objective-c nsstring nsarray

有人能够告诉我为什么我无法在视图控制器之间传递数据吗?

非常感谢任何帮助!

我从NSMutableArray获取对象,然后访问placeId属性。然后我期待将其传递给下一个视图控制器(BIDCCreateViewController),但我无法。 Null始终从下一个viewcontroller记录。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BIDDCCreateViewController *biddccCreateViewController = [[BIDDCCreateViewController alloc]init];
    BIDBusinessModel *tempObjStore = [self.linkedBusinessParseModelArray objectAtIndex:indexPath.row];
    biddccCreateViewController.placeId = tempObjStore.placeId;
    NSLog(@"tempObjStore in didselectrow: %@", tempObjStore.placeId);

}

#import <UIKit/UIKit.h>

@interface BIDDCCreateViewController : UIViewController
@property (strong, nonatomic) NSString *placeId;
@end

#import "BIDDCCreateViewController.h"

@implementation BIDDCCreateViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"SUCCESSFULLY PASSED PLACE ID: %@", self.placeId);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

我的输出来自日志如下:

2013-09-24 14:01:49.208 CouponLocation[827:a0b] tempObjStore in didselectrow: 1f068b8d2b6a13daf9be5eddee5cb32585b76377
2013-09-24 14:01:49.209 CouponLocation[827:a0b] SUCCESSFULLY PASSED PLACE ID: (null)

什么都没有通过。当我在BIDCCCreateViewConroller中设置字符串并记录它时,它工作正常。正如你可以看到我试图传递的viewcontroller中的字符串很好。这在'tempOjcStore in didselectrow'日志中得到了证明。所以有一个字符串,但它没有被传递,并且在另一个viewcontroller中的字符串似乎没有任何问题,因为我能够从内部和日志中更改它。

不知怎的,它没有传播。

有什么建议吗?

5 个答案:

答案 0 :(得分:2)

您正在为错误的对象设置值:biddccCreateViewController是您的方法的本地,它不是稍后显示的控制器,当segue被触发时(或者您打开的方法是什么)实际视图控制者)。

不要将代码放在didSelectRowAtIndexPath中,而是将代码放在prepareForSegue中。您可以从那里访问目标视图控制器,如下所示:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
   if([segue.identifier isEqualToString:@"openDetails"]){
       UITableViewCell *cell = (UITableViewCell *) sender;
       NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
       BIDDCCreateViewController *biddccCreateViewController = (BIDDCCreateViewController*)segue.destinationViewController;
       BIDBusinessModel *tempObjStore = [self.linkedBusinessParseModelArray objectAtIndex:indexPath.row];
       biddccCreateViewController.placeId = tempObjStore.placeId;
   }
}

最后,显示placeId值的正确位置是viewWillAppear,而不是viewDidLoad,因为在您可以在控制器上设置placeId之前加载视图

答案 1 :(得分:1)

我认为当你从BIDBusinessModel将tempObjStore.placeId的值传递给BIDDCCreateViewController时,可能是在打开BIDDCCreateViewController之前传递的值,这就是为什么你在BIDDCCreateViewController中获得NULL值。

按照示例代码

在BIDDCCreateViewController中

- (void)ViewDidLoad

{ [self performSelector:@selector(getValue)withObject:self afterDelay:5.0];

}

- (void)getValue

{

NSLog(@“成功通过的地址ID:%@”,self.placeId);

}

答案 2 :(得分:0)

你在哪里推动BIDDCCreateViewController视图?

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BIDDCCreateViewController *biddccCreateViewController = [[BIDDCCreateViewController alloc]init];
    BIDBusinessModel *tempObjStore = [self.linkedBusinessParseModelArray objectAtIndex:indexPath.row];
    biddccCreateViewController.placeId = tempObjStore.placeId;
    NSLog(@"tempObjStore in didselectrow: %@", tempObjStore.placeId);


   [self.navigationController pushViewController:biddccCreateViewController];
}

并刷新视图控制器它将起作用:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    // Do any additional setup after loading the view.
    NSLog(@"SUCCESSFULLY PASSED PLACE ID: %@", self.placeId);
}

答案 3 :(得分:0)

您创建BIDDCCreateViewController作为局部变量,但在导航堆栈中推送的实际控制器可能是不同的。也许您正在为错误的对象设置属性。

答案 4 :(得分:0)

您必须在 - (void)tableView :( UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath中将biddccCreateViewController推送到本地创建它。添加一个步骤以在此方法中推送视图控制器。

相关问题