gpt4 book ai didi

ios - 从另一个 View Controller 更新 UILabel

转载 作者:行者123 更新时间:2023-11-28 21:27:15 25 4
gpt4 key购买 nike

您好,我现在有点卡住了,无法将 UILabel 数据从一个 View Controller 发送到另一个来自 Collection View 单元格的 View Controller 。这是我当前的代码,我没有收到任何错误?对于知道自己在做什么的人来说,这似乎是显而易见的,但我是否需要以某种方式在第二个 View Controller 上声明我正在从另一个 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;
}

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

最佳答案

因为此时 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 ];

然后在 GroupsHomeViewControllerviewDidLoad 中,像这样在 UILabel 上设置字符串 self.groupLabel.text=self.valueToPass。尝试这样做。

关于ios - 从另一个 View Controller 更新 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37599014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com