如何在 prepareForSegue 方法中访问对象数组中选定对象的不同属性或属性

Posted

技术标签:

【中文标题】如何在 prepareForSegue 方法中访问对象数组中选定对象的不同属性或属性【英文标题】:How to Access different Properties or attributes of Selected Object among an Array of Objects in prepareForSegue method 【发布时间】:2017-02-21 05:28:38 【问题描述】:

首先,我的问题很严重,不适用于那些只愿意分配负面反馈或只是为了编辑和获得奖励目的的人。

我的问题很难传达,但我正在尽力传达完整的问题。 实际上我在后端有两个表视图和两个 Json 文件,还有一个 AVPlayerViewController。首先,firstTableViewController 显示第一个包含以下 Json 数据的 json 文件

    [
    "id": "0",
    "name": "Religion"
, 
    "id": "1",
    "name": "News"
, 
    "id": "2",
    "name": "Sport"
, 
    "id": "3",
    "name": "Doc"
, 
    "id": "4",
    "name": "Kids"
, 
    "id": "5",
    "name": "Music"
, 
    "id": "6",
    "name": "Movie"
, 
    "id": "7",
    "name": "Entr"
, 
    "id": "8",
    "name": "Cooking"
, 
    "id": "9",
    "name": "All"
]

在 SecondViewController 中提取包含不同链接的 second Json file 以及上面第一个 json 文件中提到的类别。 在我的应用程序中成功检索了两个 Json 文件的数据。

当我单击我的 FirstTableViewController 的任何单元格(包含特定类别)时,它会将我带到 secondTableViewController,其中包含该特定类别的所有对象的 title 属性(在第二个 json 文件中命名为 category_name)选定类别。 到这里为止,一切都很酷。 但是当我曾经单击任何标题时出现的问题应该将我带到 AVPlayerViewController 并播放相关的直播链接。但它给了我以下错误,

* 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[NSArrayM objectAtIndex:]:索引 2097174 超出范围 [0 .. 32]' *** 第一次抛出调用堆栈:( 0 CoreFoundation 0x0000000104b0234b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010456321e objc_exception_throw + 48 2 核心基础 0x0000000104a33f1b -[__NSArrayM objectAtIndex:] + 203 3 AlbTVRadio 0x00000001022bcae1 -[第二次 prepareForSegue:sender:] + 321 4 UIKit 0x0000000102bbef5f -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 353 5 UIKit 0x0000000102bbedcd -[UIStoryboardSegueTemplate _perform:] + 82 6 UIKit 0x0000000102bbf08f -[UIStoryboardSegueTemplate 执行:] + 156 7 UIKit 0x000000010253b52a -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1769 8 UIKit 0x000000010253b78d -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 330 9 UIKit 0x00000001023f12eb _runAfterCACommitDeferredBlocks + 320 10 UIKit 0x00000001023ddf6f _cleanUpAfterCAFlushAndRunDeferredBlocks + 566 11 UIKit 0x000000010240f3da _afterCACommitHandler + 176 12 核心基础 0x0000000104aa6e17 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 13 核心基础 0x0000000104aa6d87 __CFRunLoopDoObservers + 391 14 CoreFoundation 0x0000000104a8bb9e __CFRunLoopRun + 1198 15 CoreFoundation 0x0000000104a8b494 CFRunLoopRunSpecific + 420 16 图形服务 0x0000000106784a6f GSEventRunModal + 161 17 UIKit 0x00000001023e4964 UIApplicationMain + 159 18 AlbTVRadio 0x00000001022cd65f 主 + 111 19 libdyld.dylib 0x000000010a00968d 开始 + 1 20 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: 终止于 NSException 类型的未捕获异常

我认为问题出在 segue 代码中,即没有选择准确的 ChannelObjectObject.livestream

这是我的第二个 viewController 的代码。其中包含第二个 json 文件。因此,如果可能,请更正此代码。

#import "Second.h"
#import "Channel.h"
#import "UIImageView+AFNetworking.h"
#import "Type.h"
#define getDataUrl @"https://creativestudy.000webhostapp.com/webservices/test.json"

@interface Second ()

@end

@implementation Second
@synthesize tvArray,currentObject;

- (void)viewDidLoad 
    [super viewDidLoad];

    [self retrieveData];



    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;


- (void)didReceiveMemoryWarning 
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

    return 1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

    return tvArray.count;



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cells" forIndexPath:indexPath];

    Channel * ChannelObject;
    ChannelObject = [tvArray objectAtIndex:indexPath.row];

    cell.textLabel.text = ChannelObject.channelTitle;
    cell.detailTextLabel.text = ChannelObject.channelLivestream;
     cell.detailTextLabel.hidden = YES;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    NSURL *url = [NSURL URLWithString:ChannelObject.channelImage];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    UIImage *placeholderImage = [UIImage imageNamed:@"placeholder"];
    __weak UITableViewCell *weakCell = cell;
    [cell.imageView setImageWithURLRequest:request placeholderImage:placeholderImage success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) 
        weakCell.imageView.image = image;
        [weakCell setNeedsLayout];
     failure:nil];



    // Configure the cell...

    return cell;

-(void) getType:(id)typeObject

    currentObject = typeObject;



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
    // Return NO if you do not want the specified item to be editable.
    return YES;

*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
     else if (editingStyle == UITableViewCellEditingStyleInsert) 
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
       

*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 

*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
    // Return NO if you do not want the item to be re-orderable.
    return YES;

*/


// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
        if ([segue.identifier isEqualToString:@"playVideo"]) 

            AVPlayerViewController *playerViewController = [segue destinationViewController];

             NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
             Channel * ChannelObject;
            ChannelObject = [self.tvArray objectAtIndex:(int)indexPath];
             NSURL *liveUrl = [NSURL URLWithString:ChannelObject.channelLivestream];
             playerViewController.player = [AVPlayer playerWithURL:liveUrl];
        [playerViewController.player play];

             




-(void) retrieveData
    NSURL * url = [NSURL URLWithString:getDataUrl];
    NSData * data = [NSData dataWithContentsOfURL:url];
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    tvArray = [[NSMutableArray alloc] init];

    for (int i = 0; i < jsonArray.count; i ++)
    
        NSDictionary *aCategoryJSON = [jsonArray objectAtIndex:i];
        NSString *cId = [aCategoryJSON objectForKey:@"id"];
        NSString *cTitle = [aCategoryJSON objectForKey:@"title"];
        NSString *cCategory = [aCategoryJSON objectForKey:@"category_name"];
        NSString *cImage = [aCategoryJSON objectForKey:@"image"];
        NSString *cLivestream = [aCategoryJSON objectForKey:@"livestream"];
        NSString *cWebsite = [aCategoryJSON objectForKey:@"Website"];
        if (cCategory == currentObject.typeName)
        [tvArray addObject:[[Channel alloc] initWithChannelId:cId andChannelTitle:cTitle andChannelCategory:cCategory andChannelImage:cImage andChannelLivestream:cLivestream andChannelWebsite:cWebsite]];
        
        else if ([currentObject.typeName isEqualToString: @"All"])
        [tvArray addObject:[[Channel alloc] initWithChannelId:cId andChannelTitle:cTitle andChannelCategory:cCategory andChannelImage:cImage andChannelLivestream:cLivestream andChannelWebsite:cWebsite]];
        

    
    [self.tableView reloadData];


@end

【问题讨论】:

请发布完整的异常调用堆栈(带有由于未捕获的异常而终止应用程序...) 由于您使用的阵列超出了最大限制,您正面临问题。也许你应该检查你的 retrieveData 函数。 放置异常断点,查看异常发生在哪一行。 @Hina ChannelObject = [self.tvArray objectAtIndex:(int)indexPath]; 这是崩溃的确切代码行(我的猜测)。只有 32 个项目,您的索引超过 32 个。 是的,你是对的@Sachin_Vas,但我不知道如何解决这个问题。因为我有一个包含所有列表的 json 文件,如果您注意到,对于第二个视图控制器,& 在 retrieveData 方法中我必须只过滤选定的类别并在 UITableview 中发布。因此,如果只有 32 个项目具有相关类别。然后我点击第二个项目,它会引发错误,因为在实际的 Json 文件中它不在那个索引处,或者它会像我想不到的那样。 【参考方案1】:

你的问题在这里:

 ChannelObject = [self.tvArray objectAtIndex:(int)indexPath];

你已经将一个对象(NSIndexPath 的一个实例)转换为一个 int,而 Objective-C 允许你这样做,因为它是基于 C 的,尽管这没有任何意义。

indexPath 是指向内存中对象的指针,因此当您将其转换为 int 时,您会得到一个解释为整数的内存地址,这就是为什么您会看到尝试访问数组中的元素 2097174 的原因。大概您将转换添加到int,因为编译器告诉您不能在该函数中使用 indexPath。

您需要做的是使用indexPath.rowNSIndexPath 实例中获取行整数。您还可以使用更现代的数组访问语法和小写字母来开始变量(大写字母用于类):

Channel *channelObject = self.tvArray[indexPath.row];

【讨论】:

感谢您的回答,让我尝试并测试您在回答中所说和建议的内容,但是如果您尝试注意,我已经在 NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 行中指出了所选索引的行它就在本讨论中的ChannelObject = [self.tvArray objectAtIndex:(int)indexPath]; 行的两行上方。但是,感谢您的建议,让我尝试一下,希望无论结果如何都能恢复。 . . indexPathForSelectedRow 为您提供NSIndexPath,顾名思义。然后您需要使用indexPath.row 来获取选定的行。您正在尝试将 indexPath 直接用作数组索引,这就是为什么您必须将其转换为 int 以及为什么您会得到一个带有疯狂索引的数组边界异常 @Paul11 ,非常酷,它像专业人士一样工作......为您的解决方案竖起大拇指。

以上是关于如何在 prepareForSegue 方法中访问对象数组中选定对象的不同属性或属性的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 prepareForSegue 方法从视图控制器推送到导航视图控制器?

您是不是在同一个 prepareForSegue 方法中为一个视图控制器的所有 segue 实现行为?

如何在 iOS 中的 prepareForSegue 之前执行按钮按下操作?

在prepareForSegue方法中防止segue?

Swift:如何在 UICollectionViewCell 中使用“prepareForSegue”?

如何在 prepareForSegue 中嵌入 UINavigationController?