以编程方式完成UIImageView无法正常工作

时间:2011-06-13 05:01:12

标签: ios cocoa-touch uipickerview

我有两个观点。在第一个视图中,我有一个表,在选择一行时,我调用第二个视图,如下面的代码。还给出了被调用类的代码。但不知何故,我只能看到chnaged背景,而不是那里的其他UIImageView。有谁可以请帮助我?感谢。

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    details.movieName2=movieName;
    self.title=@"Back";
    [[self navigationController] pushViewController:details animated:NO];    
}

视图类标题:

#import <UIKit/UIKit.h>


@interface detailedView : UIViewController {
    NSString *movieName2;
    UIImageView *myImageView;
}

@property (nonatomic, retain) UIImageView *myImageView;
@property (nonatomic, retain) NSString *movieName2;
@end

视图类代码是:

#import "detailedView.h"

@implementation detailedView

@synthesize movieName2;
@synthesize myImageView;

- (id) init {
    self=[super init];
    movieName2=@"";    
    myImageView=[[UIImageView alloc] init];

return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }

    return self;
}

-(void) viewDidAppear:(BOOL)animated
{
    self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    self.view.frame=CGRectMake(0, 0, 355, 615);
    self.view.backgroundColor=[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background-retina.png"]];

    self.title=self.movieName2;    

    [myImageView setImage:[UIImage imageNamed:@"sample.png"]];
    [myImageView setFrame:CGRectMake(5, 5, 50, 50)];
    [self.view addSubview:myImageView]; 
}

- (void)dealloc
{
    [myImageView release];
    [movieName2 release];
    [super dealloc];
}

@end

1 个答案:

答案 0 :(得分:0)

按下细节控制器后尝试设置电影名称

[[self navigationController] pushViewController:details animated:NO]; 
details.movieName2=[movieName retain];

<强>更新

@Ahsan无论你做什么都是错的。当您点击tableViewCell时,您需要分配第二个视图,或者如果您已经分配了detailsView,那么为什么要在viewDidAppear中分配newView。尝试评论该行

self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

看看它是否有效。背景是有效的,因为您在detailsView本身中设置背景颜色。问题在于分配新视图并将该视图分配给self.view。