从另一个View Controller更新UILabel

时间:2016-06-02 17:58:12

标签: ios objective-c uicollectionview

嗨,我暂时将UILabel数据从一个视图控制器发送到另一个来自集合视图单元的另一个视图控制器。这是我目前的代码,我没有错误?对于知道自己在做什么的人来说,这似乎很明显,但我是否需要以某种方式在第二个视图控制器上声明我从另一个VC设置UILabel?

以下是我目前的代码如下:

GroupsViewController.m

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

- (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 *selectedImage = arrayOfImages [indexPath.item]; //collect image
            GroupsHomeViewController *groupsHomeVC = segue.destinationViewController; //set D.V.C
            groupsHomeVC.logoImage = [UIImage imageNamed: selectedImage]; //set image
            groupsHomeVC.groupLabel.text = arrayOfDescriptions[indexPath.item ]; //set text

        }
    }
}

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

#import "GroupsHomeViewController.h"

@interface GroupsHomeViewController ()

@end

@implementation GroupsHomeViewController

-(void)viewDidLoad{ 
    [super viewDidLoad];
}

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

非常感谢您的时间和耐心。

2 个答案:

答案 0 :(得分:1)

在GroupsHomeViewController.h中,首先要创建一个属性为

@property (nonatomic, strong)NSString *descriptionName;

然后将您的方法更新为

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

    if ([segue.identifier isEqualToString:@"GroupsHomeSegue"])
    {
        NSIndexPath* indexPath = [[self.GroupsCollectionView indexPathsForSelectedItems]firstObject];
        if(indexPath !=nil)
        {
            NSString *selectedImage = arrayOfImages [indexPath.item]; //collect image
            GroupsHomeViewController *groupsHomeVC = segue.destinationViewController; //set D.V.C
            groupsHomeVC.logoImage = [UIImage imageNamed: selectedImage]; //set image
            groupsHomeVC.descriptionName = arrayOfDescriptions[indexPath.item ]; //set text

        }
    }
} 

现在在GroupsHomeViewController.m中更新此

#import "GroupsHomeViewController.h"

@interface GroupsHomeViewController ()

@end

@implementation GroupsHomeViewController
@synthesize descriptionName;
-(void)viewDidLoad{ 
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self. groupLabel.text = descriptionName;
}

这肯定会有所帮助。在您的情况下,文本分配给标签但是当您转移到生命周期时,GroupsHomeViewController的方法被调用。所以这里你的UILabel正在初始化,你正在设置文本,所以它不显示它。

创建NSString属性肯定会帮助你。

答案 1 :(得分:1)

因为此时,GroupsHomeViewController尚未加载到内存中,这就是您遇到此问题的原因。 不要直接将值传递给UILabel,而是尝试使用NSString传递文本。声明

@property (strong,nonatomic) NSString *valueToPass;
GroupsHomeViewController中的

。然后使用下面的代码

将值传递给字符串
NSString *selectedImage = arrayOfImages [indexPath.item]; //collect image
GroupsHomeViewController *groupsHomeVC = segue.destinationViewController; //set D.V.C
groupsHomeVC.logoImage = [UIImage imageNamed: selectedImage]; //set image
// pass string to next viewController's NSString object.            
groupsHomeVC.valueToPass = arrayOfDescriptions[indexPath.item ]; 

然后在viewDidLoad的{​​{1}}中,在GroupsHomeViewController上设置字符串UILabel。试着这样做。