Objective c 为什么房地产会基本消失?可可粉
我很困惑。我有一个应用程序,它有一个用于存储项目详细信息的类。调用Objective c 为什么房地产会基本消失?可可粉,objective-c,cocoa,properties,Objective C,Cocoa,Properties,我很困惑。我有一个应用程序,它有一个用于存储项目详细信息的类。调用LEItem。这些项目存储在标有LEItemStore的类的存储中。我有一个包含所有项目的表的视图。这个很好用。如果您点击一行,它会将此消息发送到LogbookFirstViewController LogbookFirstViewController *logController = [[LogbookFirstViewController alloc] initForNewItem:NO]; NSArray *it
LEItem
。这些项目存储在标有LEItemStore
的类的存储中。我有一个包含所有项目的表的视图。这个很好用。如果您点击一行,它会将此消息发送到LogbookFirstViewController
LogbookFirstViewController *logController = [[LogbookFirstViewController alloc] initForNewItem:NO];
NSArray *items = [[LEItemStore sharedStore] allItems];
LEItem *selectedItem = [items objectAtIndex:[indexPath row]];
NSString *description = [selectedItem description];
NSLog(@"%@", description);
[logController setItem:selectedItem];
[self dismissViewControllerAnimated:YES completion:nil];
这是在TableView类中。在日志中FirstViewController.m我有
-(void)setItem:(LEItem *)i{
item = i;
NSString *t = [item description];
NSLog(@"In LogbookFirstViewController, returning %@", t);
}
这就是奇怪的地方。这很有效。它输出正确的项目,因此我认为一切都会好起来。但事实并非如此<代码>项是类级属性,因此它应该保留,但它不是。在同一个类中,我重写了这个方法
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//NSString *string = [item description];
//NSLog(@"Item = %@", string);
NSLog(@"View did Appear:animated");
int glucoseValue = [item glucose];
NSString *glucoseString = [NSString stringWithFormat:@"%d", glucoseValue];
[glucoseField setText:glucoseString];
int proteinValue = [item protein];
NSString *proteinString = [NSString stringWithFormat:@"%d", proteinValue];
[proteinField setText:proteinString];
int carbsValuue = [item carbs];
NSString *carbsString = [NSString stringWithFormat:@"%d", carbsValuue];
[carbsField setText:carbsString];
int insulinValue1 = [item insulin];
NSString *insulin1String = [NSString stringWithFormat:@"%d", insulinValue1];
[insulinField1 setText:insulin1String];
int insulinValue2 = [item insulin2];
NSString *insulinString2 = [NSString stringWithFormat:@"%d", insulinValue2];
[insulinField2 setText:insulinString2];
//NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
//[dateFormatter setDateStyle:NSDateFormatterShortStyle];
//[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
//NSLog(@" The item was created on %@", [dateFormatter stringFromDate:[item dateCreated]]);
//[dateButton setTitle:[dateFormatter stringFromDate:[item dateCreated]] forState:UIControlStateNormal];
NSString *t = [item description];
NSLog(@"Loading view... Returns: %@", t);
}
我知道这不是最干净的代码,但想法是一样的。它使用与setItem:方法完全相同的代码。但是,它总是返回(null)。为什么?属性在ViewWillDisplay处似乎丢失
谢谢
编辑
我解决了这个问题。正如你所见,下面的核对答案确实给出了正确的想法,以下是我为解决这个问题所做的。问题是,当我发送setItem时:我使用此代码获取LogbookFirstViewController
LogbookFirstViewController *logController = [[LogbookFirstViewController alloc] initForNewItem:NO];
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *items = [[LEItemStore sharedStore] allItems];
LEItem *selectedItem = [items objectAtIndex:[indexPath row]];
NSString *description = [selectedItem description];
NSLog(@"%@", description);
NSLog(@"Setting controller: %@", logController);
[logController setItem:selectedItem];
[self dismissViewControllerAnimated:YES completion:nil];
}
如我所知,这创建了LogbookFirstViewController的一个新实例,因此,现有实例没有更改其Item属性,因为属性被分配给一个实例。因此,我只是更改了这个“无形”属性的项的值
要解决此问题,必须获取viewController的现有实例。为此,我做了以下工作:
在LogbookFirstViewController.h中,我添加了此属性
@property (assign) LogbookFirstViewController *instance;
然后,在您的.m中合成实例,并在我将其添加到viewDidLoad的相同位置
- (void)viewDidLoad
{
instance = self;
...
然后,在另一个viewController entriesViewController中,我也在.h中添加了这个
@property (nonatomic, strong) LogbookFirstViewController *logController;
合成它。然后,我以同样的方式使用didSelectRowAtIndexPath,只使用现有的logController
LogbookFirstViewController *logController = [[LogbookFirstViewController alloc] initForNewItem:NO];
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *items = [[LEItemStore sharedStore] allItems];
LEItem *selectedItem = [items objectAtIndex:[indexPath row]];
NSString *description = [selectedItem description];
NSLog(@"%@", description);
NSLog(@"Setting controller: %@", logController);
[logController setItem:selectedItem];
[self dismissViewControllerAnimated:YES completion:nil];
}
那就行了 将对象指定给不带圆弧的特性时,应保留:
-(void)setItem:(LEItem *)i{
_item = [i retain];
...
}
如果将属性与ARC一起使用,则编写\u item=i代码>:
-(void)setItem:(LEItem *)i{
_item = i;
...
}
您可以在一行中创建LogbookFirstViewController
,但实际上不会使它显示任何内容(推送或显示)。因为它是一个局部变量,所以无论该控制器的哪个实例正在加载它的视图,它似乎都不是您在所显示的代码中初始化的那个实例
您可以通过添加两行NSLog来验证这一点,例如:
NSLog(@"Setting controller: %@", logController); // Insert before existing line
[logController setItem:selectedItem];
……还有
[super viewWillAppear:animated];
NSLog(@"Viewing controller: %@", self); // Insert after existing line
为了使事情按您想要的方式工作,必须打印相同的地址。ARC禁止发送“retain”的明确消息retain不可用:在自动引用计数模式下不可用。self.item=1不会留下任何生成错误。但是抛出了一个巨大的异常。这是我头中的属性@属性(非原子,强)LEItem*项;对的我的意思是self.item=我抛出了一个异常。是的,它们打印的地址略有不同。查看控制器:和设置控制器:修复它取决于如何创建和显示正在执行的视图的LogbookFirstViewController
。基本上,您需要获取对它的引用,并在调用setItem:
时使用它。。。或者,如果一个已经被移除,推送或呈现你刚刚创建的一个。你如何获得对它的引用?就像我说的,这取决于它最初是如何创建的。它是来自故事板还是xib或代码?它与包含表格的控制器(例如iPad主控/详细信息、不同的选项卡、其他内容等)有何关系?嗯,控制器是在故事板中制作的。LogbookFirstViewController是此应用程序的“主”视图。表视图控制器是一个带有自己类的.xib。按下LogbookFirstViewController上的一个按钮,它会将您切换到另一个。表格视图是通过模式转换创建的。