gpt4 book ai didi

iphone - 如何重新加载当前的 UIView

转载 作者:行者123 更新时间:2023-11-28 19:20:40 25 4
gpt4 key购买 nike

我有一个从我的主视图调用的方法,它设置我想在 ScrollView 中显示的图像的图像名称。

 - (void)loadImage:(NSString *)myImageName
{
if (myImageName == @"one") {
imageName = myImageName;
}
if (myImageName == @"two") {
imageName = myImageName;
}
if (myImageName == @"three") {
imageName = myImageName;
}

//Reloads view here???
}

我像这样在我的 viewdidload 方法中加载图像

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//Create scrollview
scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scrollView.delegate = self;
scrollView.bounces = NO;

//Create scrollviewimage
if (imageName == @"one") {
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ha.png"]];
}
if (imageName == @"two") {
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"haha.png"]];
}
if (imageName == @"three") {
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hahaha.png"]];
}



containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 550)];
//Add subview
[containerView addSubview:image];
//initViews
scrollView.contentSize = containerView.frame.size;
[scrollView addSubview:containerView];

//scrolling
scrollView.minimumZoomScale = 1.0;
scrollView.maximumZoomScale = 31.0;
[scrollView setZoomScale:scrollView.minimumZoomScale];

//highrachy
[self.view addSubview:scrollView];

}

当我从 tableviewcell 选择的父 View 中设置图像名称时会发生什么我将 nsstring 传递到 loadImage.. 然后加载图像在 viewdidload 中设置名称.. 但是发生的事情是只有第一个选择会做任何事情..所以你总是会看到你选择的第一张图片..所以如果你选择第二张图片,你选择的每隔一张图片都会显示第二张图片..

任何帮助都会很棒。

这是父 View 单元格选择的样子

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Sets the back button for the new view that loads (this overrides the usual parentview name with "Back")
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil];

if (!self.detailViewController) {
self.detailViewController = [[ICDDetailViewController alloc] initWithNibName:@"ICDDetailViewController" bundle:nil];

if (indexPath.section == 0) {
_detailViewController.imageName = @"one";
// NSLog(@"%@", indexPath);
}
if (indexPath.section == 1) {
_detailViewController.imageName = @"two";
// NSLog(@"%@", indexPath);
}
if (indexPath.section == 2) {
_detailViewController.imageName = @"three";
// NSLog(@"%@", indexPath);
}
}
[self.navigationController pushViewController:self.detailViewController animated:YES];

}

最佳答案

不要使用 myImageName == @"three"。有一种比较字符串的特殊方法。

试试这个:

if ([myImageName isEqualToString:@"three"]) {
[image setImage:[UIImage imageNamed:myImageName]];
}

如果有文件扩展名,执行此操作:

NSString *imagePath = [NSString stringWithFormat:@"%@.png",myImageName];
[image setImage:[UIImage imageNamed:imagePath]];

顺便说一句,您仍在发布您在其他问题中的旧代码。您将 detailView 保留在原始类中,而不是释放和创建新实例。取出 if (!detailView) 语句。

更新:唯一的其他选择是从大的 if() block 中取出赋值。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Sets the back button for the new view that loads (this overrides the usual parentview name with "Back")
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil];

if (!self.detailViewController) {
self.detailViewController = [[ICDDetailViewController alloc] initWithNibName:@"ICDDetailViewController" bundle:nil];


//MY CHANGES HERE:
//If you left these if () statements inside the original code, they would never
//fire if the detailView was already instantiated once. Does this make sense?
} // <--- moved the bracket up here
if (indexPath.section == 0) {
_detailViewController.imageName = @"one";
// NSLog(@"%@", indexPath);
}
if (indexPath.section == 1) {
_detailViewController.imageName = @"two";
// NSLog(@"%@", indexPath);
}
if (indexPath.section == 2) {
_detailViewController.imageName = @"three";
// NSLog(@"%@", indexPath);
}
//} <--- commented this one out
[self.navigationController pushViewController:self.detailViewController animated:YES];

}

唯一的另一种方式是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Sets the back button for the new view that loads (this overrides the usual parentview name with "Back")
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil];
//commented out the next line, don't need to check to see if it's already present
//just create a new instance to push on the stack
//if (!self.detailViewController) {
self.detailViewController = [[ICDDetailViewController alloc] initWithNibName:@"ICDDetailViewController" bundle:nil];

if (indexPath.section == 0) {
_detailViewController.imageName = @"one";
// NSLog(@"%@", indexPath);
}
if (indexPath.section == 1) {
_detailViewController.imageName = @"two";
// NSLog(@"%@", indexPath);
}
if (indexPath.section == 2) {
_detailViewController.imageName = @"three";
// NSLog(@"%@", indexPath);
}
//} <--- get rid of this bracket since the original if () was commented out
[self.navigationController pushViewController:self.detailViewController animated:YES];

}

关于iphone - 如何重新加载当前的 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9087401/

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