Objective c 值在执行NSLog后传递到下一个屏幕

Objective c 值在执行NSLog后传递到下一个屏幕,objective-c,ios,ios6,segue,Objective C,Ios,Ios6,Segue,我是Objective C的新手。我正在做的是在prepeareSegue中为destination view controller设置一些值。奇怪的是,如果我在函数中注释掉NSLog,那么目标控制器的属性值就不会被赋值 我的代码是: -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"ShowItemOnMap"] )

我是Objective C的新手。我正在做的是在prepeareSegue中为destination view controller设置一些值。奇怪的是,如果我在函数中注释掉NSLog,那么目标控制器的属性值就不会被赋值

我的代码是:

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

   if ([segue.identifier isEqualToString:@"ShowItemOnMap"] ) {
       LocateItemViewController *lic = [segue destinationViewController];
       NSIndexPath *index = [self.tableView indexPathForSelectedRow];

       // self.itemsToBuy is a array of NSDictionary
       NSDictionary *selectedItem = [self.itemsToBuy objectAtIndex:[index row]];
       Item *theItem = [[Item alloc] init];
       NSString *theTitle = [[NSString alloc] initWithString:[selectedItem valueForKey:@"title"]];
       theItem.title = theTitle;
       lic.item = theItem;

       // commenting out NSLog make self.irem in LocateItemViewController nil
       // and no value is shown at screen
       NSLog(@"%@", lic.item.title);
   }

}
Item是具有属性的自定义类

@property (strong, nonatomic) NSString *title;
LocateItemController具有以下属性

@property (weak, nonatomic) Item *item;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
viewDidLoad只分配一个项目

self.titleLabel.text = self.item.title;

如果您需要保留该项,则应将其设置为强属性。

Adam是正确的。您的item属性必须是strong,否则一旦发布的代码运行完毕,它就会被释放

如果还没有,您还需要使Item对象的“title”属性变强

您需要仔细阅读对象所有权

交易是这样的:系统跟踪有多少对象对一个对象具有强引用。当该计数降至零时,对象被释放

默认情况下,局部变量是强的。当您创建“ITEEM”局部变量时,它是强的,并且只存在于prepareForSegue方法的范围内。当该方法结束时,变量ITEEM将超出范围,对Item对象的强引用也将消失

您使LocateItemController的项属性变弱。这意味着您的LocateItemController不拥有分配给其item属性的item对象的所有权

如果将LocateItemController中的项属性声明更改为strong,则当您将项对象指定给该属性时,LocateItemController将获得该项的所有权

最后,在LocateItemController的dealoc方法中,需要添加以下行:

self.item = nil;
这将导致在释放LocateItemController之前释放LocateItemController的item对象。

@property(强,非原子)item*item;