我是 Objective C 的新手。我正在做的是在 prepeareSegue 中为目标视图控制器设置一些值。奇怪的是,如果我在函数中注释掉 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;