如何在另一个视图控制器中更改UIImageView的图像?

时间:2013-04-17 02:47:43

标签: iphone sdk uiimageview

我有一个非常简单但令人沮丧的问题。在我的didSelectRowAtIndexPath:方法中,我尝试更改另一个视图中的UIImageView,但不幸的是,它无法正常工作,UIImageView保持不变。

我做错了什么?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MainViewController *demoController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    demoController.title = [NSString stringWithFormat:@"Demo Controller #%d-%d", indexPath.section, indexPath.row];

    NSArray *controllers = [NSArray arrayWithObject:demoController];
    self.sideMenu.navigationController.viewControllers = controllers;
    [self.sideMenu setMenuState:MFSideMenuStateClosed];

    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"server"] isEqualToString:@"imap.mail.me.com"]){
        if (indexPath.row == 0)
            theAppDelegate.folder = @"INBOX";
            UIImage *image = [UIImage imageNamed:@"inbox.png"];
            demoController.imageBG.image = image;

        if (indexPath.row == 1)
            theAppDelegate.folder = @"Sent Messages";
        UIImage *image2 = [UIImage imageNamed:@"sentmail.png"];
        demoController.imageBG.image = image2;

        if (indexPath.row == 2)
            theAppDelegate.folder = @"Drafts";
        UIImage *image3 = [UIImage imageNamed:@"drafts.png"];
        demoController.imageBG.image = image3;

        if (indexPath.row == 3)
            theAppDelegate.folder = @"Deleted Messages";
        UIImage *image4 = [UIImage imageNamed:@"trash.png"];
        demoController.imageBG.image = image4;

        if (indexPath.row == 4)
            theAppDelegate.folder = @"Archive";
        UIImage *image5 = [UIImage imageNamed:@"archive.png"];
        demoController.imageBG.image = image5;

        if (indexPath.row == 5)
            theAppDelegate.folder = @"Junk";
        UIImage *image6 = [UIImage imageNamed:@"junk.png"];
        demoController.imageBG.image = image6;

    }
}

2 个答案:

答案 0 :(得分:0)

根据此行if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"server"] isEqualToString:@"imap.mail.me.com"])将获得真实的意思,您的demoController.imageBG.image始终拥有junk.png

我认为您在{}正文之间遗漏了if

必须是这样的

if (indexPath.row == 0)
{
    theAppDelegate.folder = @"INBOX";
    UIImage *image = [UIImage imageNamed:@"inbox.png"];
    demoController.imageBG.image = image;    
}
 if (indexPath.row == 1)
     .........

注意:在编写此类编码时,请更好地使用else-ifswitch-case

答案 1 :(得分:0)

对于{}条件,你错过了if,所以每次junk.png UIImageView上都有NSString张图片。

此外,您可以在MainViewController ...

中传递图像名称,而不是您的逻辑

只是属性 - 合成字符串并传入MainViewController.h类,如下所示...

=> @property (nonatomic, retain) NSString *strImageName;

MainViewController.m

=> @synthesize strImageName;

   if (indexPath.row == 0){
    theAppDelegate.folder = @"INBOX";
    demoController.strImageName = @"inbox.png";
    [demoController.strImageName retain];
   }
   else if (indexPath.row == 1){
        theAppDelegate.folder = @"Sent Messages";
    demoController.strImageName = @"sentmail.png";
    [demoController.strImageName retain];
   }

并且像上面的代码一样在这个变量中传递该名称......

MainViewController

并在 - (void)viewDidLoad { [super viewDidLoad]; UIImage *image = [UIImage imageNamed:strImageName]; imageBG.image = image; } 的viewDidLoad方法中,只需将此图像指定为贝娄..

{{1}}