在该类型的对象上找不到属性

发布于 2025-01-05 03:42:49 字数 2024 浏览 3 评论 0原文

这看起来很奇怪,因为我无法解决它并坚持下去。我正在使用故事板在表格视图和详细视图之间导航。当我将单个(NewsRecord)对象从我的表视图类(TopStoriesViewController)传递到我的详细信息类(DetailNewsViewController)时,它工作正常。但现在我需要在移动到详细信息类时传递一组 (NewsRecord) 对象,而不是单个 (NewsRecord) 对象。但是,当我在详细信息类中创建 NSArray * 并尝试使用详细信息类的对象在prepareForSegue方法中的tableview类中访问它时,会出现以下错误——在“DetailNewsViewController *”类型的对象上找不到属性“items” ' 在编译时。 items 是一个 NSArray 对象,它从“entries”获取其内容,“entries”也是 TopStoriesViewController 类中的 NSArray。

我的问题是为什么我能够访问 TopStoriesViewController 中 DetailNewsViewController 的 getNewsDetails 而不是项目。

我的课程如下 - TopStoriesViewController.m

#import "DetailNewsViewController.h"

     some code here....

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
         if ([[segue identifier] isEqualToString:@"ShowDetailedNews"]) {
         DetailNewsViewController *detailNewsVC = [segue destinationViewController];
         [segue.destinationViewController setHidesBottomBarWhenPushed:YES];
         NSInteger indexForNewsSelectedFromTBV = [[self.tableView indexPathForSelectedRow] row];
         [detailNewsVC setGetNewsDetails:[entries objectAtIndex:indexForNewsSelectedFromTBV]]; //This is working fine...
         detailNewsVC.items=entries;  //Error is occurring here...
    }
   } 

DetailNewsViewController.h

#import "NewsRecord.h"
    @interface DetailNewsViewController : UIViewController {
       NewsRecord *getNewsDetails;

       some other declarations...

       NSArray *items;
   }
@property(nonatomic,retain) NewsRecord *getNewsDetails;
@property(nonatomic,retain) NSArray *items;
@end

DetailNewsViewController.m

#import "DetailNewsViewController.h"
@synthesize getNewsDetails,items;

NewsRecord.h

@interface NewsRecord : NSObject {
      NSString *newsTitle;
      NSString *newsDescription;
    }
    @property(nonatomic,retain) NSString *newsTitle;
    @property(nonatomic,retain) NSString *newsDescription;
    @end

this seems just bizarre as i am not able to resolve it and stuck over it. I am using storyboard to navigate between tableview and a detailview. It was working fine when i was passing a single (NewsRecord) object from my tableview class(TopStoriesViewController) to my detail class(DetailNewsViewController). But now i need to pass an array of (NewsRecord) objects when moving to the detail class instead of a single (NewsRecord) object. But when i create a NSArray * in my detail class and try to access it in my tableview class in prepareForSegue method using the object of detail class it gives the following error---property 'items' not found on object of type 'DetailNewsViewController *' at compile time. items is a NSArray object which get its contents from the 'entries' which is also an NSArray in TopStoriesViewController class.

My question is why am i able to access getNewsDetails of DetailNewsViewController in TopStoriesViewController and not items.

My classes are as follows -
TopStoriesViewController.m

#import "DetailNewsViewController.h"

     some code here....

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
         if ([[segue identifier] isEqualToString:@"ShowDetailedNews"]) {
         DetailNewsViewController *detailNewsVC = [segue destinationViewController];
         [segue.destinationViewController setHidesBottomBarWhenPushed:YES];
         NSInteger indexForNewsSelectedFromTBV = [[self.tableView indexPathForSelectedRow] row];
         [detailNewsVC setGetNewsDetails:[entries objectAtIndex:indexForNewsSelectedFromTBV]]; //This is working fine...
         detailNewsVC.items=entries;  //Error is occurring here...
    }
   } 

DetailNewsViewController.h

#import "NewsRecord.h"
    @interface DetailNewsViewController : UIViewController {
       NewsRecord *getNewsDetails;

       some other declarations...

       NSArray *items;
   }
@property(nonatomic,retain) NewsRecord *getNewsDetails;
@property(nonatomic,retain) NSArray *items;
@end

DetailNewsViewController.m

#import "DetailNewsViewController.h"
@synthesize getNewsDetails,items;

NewsRecord.h

@interface NewsRecord : NSObject {
      NSString *newsTitle;
      NSString *newsDescription;
    }
    @property(nonatomic,retain) NSString *newsTitle;
    @property(nonatomic,retain) NSString *newsDescription;
    @end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍暮颜 2025-01-12 03:42:49

您应该尝试明确地使用项目的设置器:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
     if ([[segue identifier] isEqualToString:@"ShowDetailedNews"]) 
     {

     DetailNewsViewController *detailNewsVC = [segue destinationViewController];
    ...
     [detailNewsVC setItems:entries];

     }
} 

You should try to explicitely use the setter for items:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
     if ([[segue identifier] isEqualToString:@"ShowDetailedNews"]) 
     {

     DetailNewsViewController *detailNewsVC = [segue destinationViewController];
    ...
     [detailNewsVC setItems:entries];

     }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文