【问题标题】:How to reload current UIView如何重新加载当前的 UIView
【发布时间】:2012-01-31 21:44:54
【问题描述】:

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

 - (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 选择中设置父视图的图像名称时会发生什么我将 nsstring 传递到 loadImage.. 然后加载图像在 viewdidload 中设置名称.. 但是发生的事情是只有第一个选择剂量任何东西..所以你总是会看到你选择的第一张图片..所以如果你选择第二张图片,你选择的每隔一个图片就会显示第二张图片..

任何帮助都会很棒。

这是父视图单元格选择的样子

- (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];

}

【问题讨论】:

    标签: iphone ios uitableview uiview


    【解决方案1】:

    不要使用 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( ) 块中取出。

    - (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];
    
    }
    

    【讨论】:

      【解决方案2】:

      听起来您从未发布过您的 UIScrollView 实例。因此,当您第二次重用它时, viewDidLoad 不会再次被调用。这意味着您的“图像”变量永远不会被重新分配新图像。

      要检查是否是这种情况,请尝试在 viewDidLoad 方法中调用 NSLog 并查看它是否在您第二次从 tableViewCell 中选择时被调用。

      如果是这种情况,有几个选项可以修复它。你在使用 ARC 吗?

      【讨论】:

      • 我只是在 viewdidload 内的每个 if 语句中都放了一个 nslog,就像图像一样,唯一返回的是第一个被选中的.. 只有一次。
      • 太好了,现在您知道问题所在了,您只需要解决它。我建议在 loadImage: 方法的末尾使用 [image setImage:[UIImage imageNamed:imageName] 直接为您的“图像”类设置图像属性。
      【解决方案3】:

      您是否尝试过仅更新UIImageView

      - (void)loadImage:(NSString *)myImageName
      {
          if (myImageName == @"one") {
              imageName = myImageName;
          }
          if (myImageName == @"two") {
              imageName = myImageName;
          }
          if (myImageName == @"three") {
              imageName = myImageName;
          }
      
          [image setImage:[UIImage imageNamed:imageName];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-22
        • 2014-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-24
        • 2017-03-19
        相关资源
        最近更新 更多