我正在尝试实现-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
到目前为止,这是我在第一个UITableViewController中所做的:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    secondviewcontroller *vc = [[secondviewcontroller alloc]init];
    BudgetPlan *tempBudget = [self.budgetElements objectAtIndex:indexPath.row];
    vc.budgetPlan = tempBudget;
}我的第二个视图控制器有ff:
// secondviewcontroller.h
@property (strong, nonatomic) BudgetPlan *budgetPlan;
//secondviewcontroller.m
@synthesize budgetPlan = _budgetPlan
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"%@ was passed with %@",self.budgetPlan.name, self.budgetPlan.amount);
self.budgetName.text = _budgetPlan.name;
    self.amountBudgeted.text = [NSString stringWithFormat:@"%.02f", _budgetPlan.amount];
}不幸的是,NSLog显示为空。因此,UILabels budgetName.text和amountBudgeted.text也为空。
我已经设置了数据源并委托给包含UITableView元素(这不是UITableViewController)的自定义UIViewController。看起来我正在传递对象,但它似乎并没有传递...
我哪里错了?
发布于 2012-03-27 08:10:16
谢谢大家。问题似乎是我对故事板的使用。这
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法似乎只有在不使用故事板的情况下才有效。
我用Segues来回答这个问题。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
/*
 When a row is selected, the segue creates the detail view controller as the destination.
 Set the detail view controller's detail item to the item associated with the selected row.
 */
    if ([[segue identifier] isEqualToString:@"showDetailsOfBudget"]) 
    {
        NSIndexPath *indexPath = [self.budgetsTable indexPathForSelectedRow];
        BudgetDetailsViewController *detailsViewController = [segue destinationViewController];
        detailsViewController.budget = [self.budgetPlan.budgets objectAtIndex:indexPath.row];
    }
}发布于 2012-03-23 08:11:44
您正在创建一个budgetPlan对象,但在您的代码中从未设置属性name和amount。
在viewDidLoad中,您实际上记录的正是这些属性,它们仍然是nil和NSLog日志(null)。
NSLog(@"%@ was passed with %@",self.budgetPlan.name, self.budgetPlan.amount);您可以尝试记录budgetPlan本身。你应该得到一个对象内存地址。
发布于 2012-03-25 22:13:29
首先,尝试上面的建议(注销budgetPlan对象本身,看看是否为nil)。
如果它不是nil,那么您必须在代码的其他地方查看为什么它上的属性为nil。
如果它是零,那么问题就出在你使用viewDidLoad了。
您不知道何时会调用viewDidLoad。你有两个选择:
1. Don't use viewDidLoad to do that - you could use viewWillAppear instead
2. If second view controller is only ever associated with one budget plan, then don't set the property like that but make a custom init method:
    -(id) initWithBudgetPlan:(BudgetPlan *)plan
    {
       if (self = [super init])
       {
         self.budgetPlan = plan;
       }
       return self;
    }https://stackoverflow.com/questions/9831573
复制相似问题