2012-01-31 50 views
0

我有一个方法,我从我的主视图调用,即设置我想要显示在滚动视图中的图像的图像名称。如何重新加载当前UIView

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

     //Reloads view here??? 
    } 

我加载在我viewDidLoad方法的相似图片时,我设置为从parentview的imagename从tableviewcell选择我NSString的向下传递到的LoadImage所以

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

} 

会发生什么事..然后载荷图像设置viewdidload中的名称..但是,发生的事情是,只有第一个选择剂量任何东西..所以你总是会看到你选择的第一个图像..所以,如果你选择图像两个你选择的每个其他图像将显示图像二..

an Ÿ帮助会很好。

这里是parentview小区选择是什么样子

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

} 

回答

3

请勿使用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]; 

} 
+0

opps现在这样做。 – 2012-01-31 22:19:29

+0

它工作?为了更加清晰,我更新了答案。 – Justin 2012-01-31 22:57:57

1

这听起来像你是不是曾经释放你的UIScrollView的实例。所以,当你第二次重用它时,viewDidLoad不会再被调用。这意味着你的'图像'变量永远不会被重新分配一个新的图像。

要检查是否属实,请尝试在您的viewDidLoad方法中放入NSLog调用,并查看它是否在第二次从tableViewCell中选择时调用。

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

+0

我使用ARC .. – 2012-01-31 22:16:43

+0

我只是把一个NSLog的在每个如果里面viewDidLoad中和喜欢的形象,返回是唯一一个statments第一个被选中的只有一次。 – 2012-01-31 22:17:46

+1

太棒了,所以现在你知道问题是什么,你只需要修复它。我建议直接在你的loadImage:方法的末尾用[image setImage:[UIImage imageNamed:imageName])来为你的'image'类设置image属性。 – AtkinsonCM 2012-01-31 22:28:00

0

您是否尝试过更新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]; 
}