UIImage未通过第一个视图控制器接收

时间:2016-05-26 20:28:05

标签: ios objective-c uiimage

您好,我现在只是“认为我已经破解了通过准备segue发送内容”,但问题是我的图像没有出现在第二个视图控制器中。因为我对此有点新意,我想知道是否有一些我不知道的东西,因为无论我怎么看起来都无法将我的第一个视图控制器的图像显示在我的第二个视图控制器中。

GroupsViewController.h

#import <UIKit/UIKit.h>
@interface GroupsViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate> 
@property (weak, nonatomic) IBOutlet UICollectionView *GroupsCollectionView;

- (IBAction)cellToggleAction:(id)sender;

@end

GroupsViewController.m

#import "GroupsViewController.h"
#import "GroupsHomeViewController.h"
#import "CustomCell.h"


@interface GroupsViewController ()
{
    NSArray *arrayOfImages;
    NSArray *arrayOfDescriptions;
}

@end

@implementation GroupsViewController
{
    NSString *reuseIdentifier;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[self GroupsCollectionView]setDataSource:self];
    [[self GroupsCollectionView]setDelegate:self];
    reuseIdentifier= @"SmallIcon"; //set inital value of reuse identifier

    arrayOfImages = [[NSArray alloc]initWithObjects:@"a.png", nil];

    arrayOfDescriptions = [[NSArray alloc]initWithObjects:@"A", nil];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [arrayOfDescriptions count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    [[cell IconImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
    [[cell IconLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"GroupsHomeSegue" sender:indexPath];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"GroupsHomeSegue"])
    {
        NSIndexPath* indexPath = [[self.GroupsCollectionView indexPathsForSelectedItems]firstObject];
        if(indexPath !=nil)
        {
            //NSString *selectedDescription = arrayOfDescriptions[indexPath.item]; //collects description from cell
            NSString *selectedImage = arrayOfImages [indexPath.item]; //collects image from cell

            GroupsHomeViewController *groupsHomeViewController = segue.destinationViewController;
            groupsHomeViewController.logoImage = [UIImage imageNamed: selectedImage];
            //groupsHomeViewController.groupLabel = [:selectedDescription];

        }
    }
}


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

@end

GroupsHomeViewController.h

#import <UIKit/UIKit.h>

@interface GroupsHomeViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UIImage *logoImage;
@property (strong, nonatomic) IBOutlet UILabel *groupLabel;

@end

GroupsHomeViewController.m

#import "GroupsHomeViewController.h"

@interface GroupsHomeViewController ()

@end

@implementation GroupsHomeViewController

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

@end 

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

Lee Sugden,

传递图像是不够的:)如果你想让它在你的屏幕上显示你应该在ImageView中加载它是不是:)

现在以编程方式创建UIImageView或在故事板中添加imageView绘制IBOutlet并将图像设置为

self.yourImageView.image = self.logoImage;

以编程方式创建UIImageView,

UIImageView *yourImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,self.logoImage.size.width,self.logoImage.size.height)];
  yourImageView.image = self.logoImage;
  [self.view addSubview:yourImageView];

答案 1 :(得分:0)

问题在于您的GroupsHomeViewController。您需要UIImageView作为IBOutlet而不是UIImage,然后您需要将图片视图上的image属性设置为所需的图像。为此,您需要更新GroupsHomeViewController.h文件,如下所示:

@interface GroupsHomeViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *logoImageView;
@property (strong, nonatomic) UIImage *logoImage;
@property (strong, nonatomic) IBOutlet UILabel *groupLabel;

@end

然后在GroupsHomeViewController.m中,在viewWillAppear中设置图片视图的图片:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.logoImageView.image = self.logoImage;
}

现在,当您在segue中设置logoImage时,图片视图会在视图出现之前加载图像。

假设您的故事板上已经有正确连接插座的图像视图。

答案 2 :(得分:0)

我认为上面的人可能已经解决了这个问题,建议将图像设置为任何图像视图。但是,如果仍然没有解决它,在prepareForSegue中,由于iOS voodoo的变量未设置,我会遇到问题。因此,请确保覆盖GroupHomeViewController.m中的setter,以确保将对象正确分配给image属性。

- (void)setLogoImage:(UIImage *)logoImage
{
    _logoImage = logoImage;
}