[英]UICollectionView:didSelectItemAtIndexPath: How to populate new view with data based on selection?
Here's the deal: 这是交易:
I have a UICollectionView which populates itself from an array of objects. 我有一个UICollectionView,它从对象数组中填充自身。 I want to be able to tap one of the views representing an object in my UICollection view and be taken to a new view displaying details of the object I selected in the UICollectionView (like the selected object's ID for example). 我希望能够在UICollection视图中点按表示一个对象的视图之一,并转到显示我在UICollectionView中选择的对象的详细信息的新视图(例如,所选对象的ID)。
Unfortunately, whatever I try to set is always null! 不幸的是,我尝试设置的值始终为null!
Here is the object: 这是对象:
@interface obj : NSObject
@property (nonatomic, strong) NSString *objId;
...
@property (nonatomic, strong) NSString *imageURL;
@end
Here is the main view controller: 这是主视图控制器:
@interface ViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *badgesCollectionView;
@property (strong, nonatomic) NSArray *objArray;
...
@end
Here is the detail view: 这是详细视图:
@interface DetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *objId;
@end
Here is where I'm trying to populate the detail view (in my main ViewController.m) 这是我要填充详细信息视图的地方(在我的主ViewController.m中)
// Implement action for object selection
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailView = [[DetailViewController alloc] init];
detailView.objIdLabel.text = [[self.objArray objectAtIndex:indexPath.row] objId];
[self.navigationController pushViewController:detailView animated:YES];
NSLog(@"Object Selection: Report");
NSLog(@"Label Text : %@", detailView.objId.text);
NSLog(@"Id of object : %@", [[self.objArray objectAtIndex:indexPath.row] objId]);
}
When I select an object in UICollectionView, I get a transition to a detailView without anything populated. 当我在UICollectionView中选择一个对象时,我将过渡到detailView而没有填充任何内容。 Here is what's logged: 这是记录的内容:
Object Selection: Report
Label Text : (null)
Id of object : f0f47920-f449-4fd0-a74e-804546905437
Any insight into this would be fantastic. 任何对此的见解都是很棒的。 Thanks! 谢谢!
The problem can come from the DetailViewController implementation. 该问题可能来自DetailViewController实现。
DetailViewController *detailView = [[DetailViewController alloc] init];
only init the Controller 仅初始化控制器
but you declare 但你声明
@interface DetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *objId;
@end
wich means you must use a xib. 至极意味着您必须使用XIB。
So you first have to use 所以你首先必须使用
DetailViewController *detailView = [[DetailViewController alloc] initWithNibName:@"DetailViewXIBName" bundle:nil];
then try to affect the value to objIdLabel
once the controller is pushed: 然后在按下控制器后尝试影响objIdLabel
的值:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailView = [[DetailViewController alloc] initWithNibName:@"DetailViewXIBName" bundle:nil];
[self.navigationController pushViewController:detailView animated:YES];
detailView.objIdLabel.text = [[self.objArray objectAtIndex:indexPath.row] objId];
NSLog(@"Object Selection: Report");
NSLog(@"Label Text : %@", detailView.objId.text);
NSLog(@"Id of object : %@", [[self.objArray objectAtIndex:indexPath.row] objId]);
}
You have to affect the text after [super viewDidLoad];
您必须在[super viewDidLoad];
之后影响文本[super viewDidLoad];
is called. 叫做。
These are just assumptions as we only have limited information 这些只是假设,因为我们的信息有限
The output of the UILabel
's text field is null because the label property is not actually linked to anything until the view is loaded. UILabel
的文本字段的输出为null,因为在加载视图之前,label属性实际上没有链接到任何东西。 In this case, it wont happen until after you push the view. 在这种情况下,直到您按下视图后,它才会发生。 In order to get around this, you can just have a property on your DetailViewController
that holds the string until the view appears. 为了解决这个问题,您可以在DetailViewController
上拥有一个属性,该属性保存字符串,直到出现视图为止。
Then in viewDidLoad
or viewWillAppear
in DetailViewController
, you can set the text on objIdLabel
to the property that contains the string 然后,在DetailViewController
viewDidLoad
或viewWillAppear
中,可以将objIdLabel
上的文本设置为包含字符串的属性。
It may push a blank view controller because you are not strongly holding onto the detailView. 它可能会推入空白视图控制器,因为您没有强烈按住detailView。
Try adding: 尝试添加:
@property (nonatomic, strong) DetailViewController *detailView;
to the main ViewController, then in the didSelectItemAtIndexPath method: 到主ViewController,然后在didSelectItemAtIndexPath方法中:
// Implement action for object selection
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
self.detailView = [[DetailViewController alloc] init];
self.detailView.objIdLabel.text = [[self.objArray objectAtIndex:indexPath.row] objId];
[self.navigationController pushViewController:self.detailView animated:YES];
}
It turns out this comes down to a xib's lifecycle. 事实证明,这归结到xib的生命周期。 It is true, as many of you suggested, that the detailView is not actually linked with all of the items associated with it (like the objId UILabel
) until it's loaded. 正如您中许多人所建议的那样,在加载detailView之前,它实际上并未与与其关联的所有项目(例如objId UILabel
)链接。
I ended up fixing this by creating a new singleton model object to hold onto the selected object. 最后,我通过创建一个新的单例模型对象来保留选定的对象来解决此问题。 I was then able to set the selected object on the singleton from my main view controller and call it from the detailView to set my objId on ViewDidLoad
. 然后,我可以在主视图控制器上的单例中设置选定的对象,并从detailView调用它以在ViewDidLoad上设置我的ViewDidLoad
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.