如何设置 UIImageView 控制器目标 c 的背景图像?

Posted

技术标签:

【中文标题】如何设置 UIImageView 控制器目标 c 的背景图像?【英文标题】:How to set a background image of a UIImageView controller objective c? 【发布时间】:2016-12-07 18:05:12 【问题描述】:

我的 comsci 班级正在开发一个应用程序,该应用程序将根据日期和楼层显示我们学校的地图。我们无法让从 SecondTableViewController 到 MapViewController 的 segue 工作,因为我们认为背景图像(地图)没有正确设置。

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondTableViewController : UITableViewController

@property(nonatomic, strong) NSString *daysName;
@property(nonatomic, strong) NSString *floorName;

@end

SecondViewController.m

#import "SecondTableViewController.h"
#import "MapViewController.h"

@interface SecondTableViewController ()


@end

@implementation SecondTableViewController 



    NSArray *ADay;
    NSArray *BDay;
    NSArray *CDay;
    NSArray *DDay;
    NSArray *EDay;
    NSArray *FDay;
    NSArray *GDay;
    NSArray *HDay;




/*-(void)configureWithImage:(UIImage *)paramImage

    self.capturedImage = paramImage;
*/

-(id)initWithStyle:(UITableViewStyle)style

    self = [super initWithStyle:style];
    if (self)
    
        //custom initalization
    
    return self;



- (void)viewDidLoad 
    [super viewDidLoad];

    ADay = [NSArray arrayWithObjects: @"1stFloor",@"2ndFloor", @"3rdFloor", nil];
    BDay = [NSArray arrayWithObjects: @"1stFloor",@"2ndFloor", @"3rdFloor", nil];
    CDay = [NSArray arrayWithObjects: @"1stFloor",@"2ndFloor", @"3rdFloor", nil];
    DDay = [NSArray arrayWithObjects: @"1stFloor",@"2ndFloor", @"3rdFloor", nil];
    EDay = [NSArray arrayWithObjects: @"1stFloor",@"2ndFloor", @"3rdFloor", nil];
    FDay = [NSArray arrayWithObjects: @"1stFloor",@"2ndFloor", @"3rdFloor", nil];
    GDay = [NSArray arrayWithObjects: @"1stFloor",@"2ndFloor", @"3rdFloor", nil];
    HDay = [NSArray arrayWithObjects: @"1stFloor",@"2ndFloor", @"3rdFloor", nil];

    //self.backImageView.image = self.capturedImage;






- (void)didReceiveMemoryWarning 
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

    return 1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section


    if ([_daysName isEqualToString:@"ADay"])
    
        return [ADay count];
    
    else if ([_daysName isEqualToString:@"BDay"])
    
        return [BDay count];
    

    else if ([_daysName isEqualToString:@"CDay"])
    
        return [CDay count];
    
    else if ([_daysName isEqualToString:@"DDay"])
    
        return [DDay count];
    
    else if ([_daysName isEqualToString:@"EDay"])
    
        return [EDay count];
    
    else if ([_daysName isEqualToString:@"FDay"])
    
        return [FDay count];
    
    else if ([_daysName isEqualToString:@"GDay"])
    
        return [GDay count];
    
    else if ([_daysName isEqualToString:@"HDay"])
    
        return [HDay count];
    
    return 0;


- (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath //:(NSString *)floorName

   // NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
    NSInteger rowIndex = indexPath.row;
    UIImage *background = nil;

    return background;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString *simpleTableIdentifier = @"Floors2Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) 
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    
    if ([_daysName isEqualToString:@"ADay"]) 
        cell.textLabel.text = [ADay objectAtIndex:indexPath.row];
    

    else if ([_daysName isEqualToString:@"BDay"]) 
        cell.textLabel.text = [BDay objectAtIndex:indexPath.row];
    
    else if ([_daysName isEqualToString:@"CDay"]) 
        cell.textLabel.text = [CDay objectAtIndex:indexPath.row];
    
    else if ([_daysName isEqualToString:@"DDay"]) 
        cell.textLabel.text = [DDay objectAtIndex:indexPath.row];
    
    else if ([_daysName isEqualToString:@"EDay"]) 
        cell.textLabel.text = [EDay objectAtIndex:indexPath.row];
    
    else if ([_daysName isEqualToString:@"FDay"]) 
        cell.textLabel.text = [FDay objectAtIndex:indexPath.row];
    
    else if ([_daysName isEqualToString:@"GDay"]) 
        cell.textLabel.text = [GDay objectAtIndex:indexPath.row];
    
    else if ([_daysName isEqualToString:@"HDay"]) 
        cell.textLabel.text = [HDay objectAtIndex:indexPath.row];
    

    // Assign our own background image for the cell
    UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];

    UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
    cellBackgroundView.image = background;
    cell.backgroundView = cellBackgroundView;

    return cell;




#pragma mark - Navigation
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    if([[segue identifier] isEqualToString:@"showFloorDetail"]) 
        NSArray *array = [NSArray arrayWithObjects: @"1stFloor",@"2ndFloor", @"3rdFloor", nil];

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        MapViewController *destViewController = segue.destinationViewController;

        UIImage *Background;

        if ([_daysName isEqual:@"ADay" ]) 
            if ([_floorName  isEqual: @"2ndFloor"]) 
                Background = [UIImage imageNamed:@"A2 copy.JPEG"];
            

            if ([_floorName  isEqual: @"3rdFloor"]) 
                Background = [UIImage imageNamed:@"A3 copy.png"];
            
            //background = [UIImage imageNamed:@"1stFloor"];
        

        else if ([_daysName isEqual:@"BDay" ]) 
            if ([_floorName  isEqual: @"2ndFloor"]) 
                Background = [UIImage imageNamed:@"B2 copy.png"];
            

            if ([_floorName  isEqual: @"3rdFloor"]) 
                Background = [UIImage imageNamed:@"B3 copy.JPEG"];
            
        

        else if ([_daysName isEqual:@"CDay" ])

            if ([_floorName  isEqual: @"2ndFloor"]) 
                Background = [UIImage imageNamed:@"C2 copy.JPEG"];
            

            if ([_floorName  isEqual: @"3rdFloor"]) 
                Background = [UIImage imageNamed:@"C3 copy.JPEG"];
            
        

        else if ([_daysName isEqual:@"DDay" ]) 
            if ([_floorName  isEqual: @"2ndFloor"]) 
                Background = [UIImage imageNamed:@"D2 copy.JPEG"];
            

            if ([_floorName  isEqual: @"3rdFloor"]) 
                Background = [UIImage imageNamed:@"D3 copy.JPEG"];
            
        

        else if ([_daysName isEqual:@"EDay" ]) 
            if ([_floorName  isEqual: @"2ndFloor"]) 
                Background = [UIImage imageNamed:@"E2 copy.JPEG"];
            

            if ([_floorName  isEqual: @"3rdFloor"]) 
                Background = [UIImage imageNamed:@"E3 copy.JPEG"];
            
        

        else if ([_daysName isEqual:@"FDay" ]) 
            if ([_floorName  isEqual: @"2ndFloor"]) 
                Background = [UIImage imageNamed:@"F2 copy.JPEG"];
            

            if ([_floorName  isEqual: @"3rdFloor"]) 
                Background = [UIImage imageNamed:@"F3 copy.JPEG"];
            
        

        else if ([_daysName isEqual:@"GDay" ]) 
            if ([_floorName  isEqual: @"2ndFloor"]) 
                Background = [UIImage imageNamed:@"G2 copy.JPEG"];
            

            if ([_floorName  isEqual: @"3rdFloor"]) 
                Background = [UIImage imageNamed:@"G3 copy.JPEG"];
            
        

        else 
            if ([_floorName  isEqual: @"2ndFloor"]) 
                Background = [UIImage imageNamed:@"H2 copy.JPEG"];
            

            if ([_floorName  isEqual: @"3rdFloor"]) 
                Background = [UIImage imageNamed:@"H3 copy.JPEG"];
            
        

        [destViewController configureWithImage:Background];
        destViewController.floorName = [array objectAtIndex:indexPath.row];
        self.floorName = [array objectAtIndex:indexPath.row];
        destViewController.title = destViewController.floorName;
    


@end

然后引出MapViewController,这是一个UIImageView

MapViewController.h

        #import <UIKit/UIKit.h>
//#import "SecondTableViewController.m"

@interface MapViewController : UIViewController

@property(nonatomic, strong)NSString *floorName;
@property(nonatomic, strong)NSString *daysName;

@property (nonatomic, strong) UIImage *background;

@end

MapViewController.m

#import "MapViewController.h"
#import "SecondTableViewController.h"

@interface MapViewController ()

-(void)configureWithImage:(UIImage *)paramImage;

@end

@implementation MapViewController


- (void)viewDidLoad 
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.daysName = _daysName;


// Assign our own background image for the cell
   UIImage *Background;

    if ([_daysName isEqual:@"ADay" ]) 
        if ([_floorName  isEqual: @"2ndFloor"]) 
            Background = [UIImage imageNamed:@"A2 copy.JPEG"];
        

        if ([_floorName  isEqual: @"3rdFloor"]) 
            Background = [UIImage imageNamed:@"A3 copy.png"];
        
        //background = [UIImage imageNamed:@"1stFloor"];
    

    else if ([_daysName isEqual:@"BDay" ]) 
        if ([_floorName  isEqual: @"2ndFloor"]) 
            Background = [UIImage imageNamed:@"B2 copy.png"];
        

        if ([_floorName  isEqual: @"3rdFloor"]) 
            Background = [UIImage imageNamed:@"B3 copy.JPEG"];
        
    

    else if ([_daysName isEqual:@"CDay" ])

        if ([_floorName  isEqual: @"2ndFloor"]) 
            Background = [UIImage imageNamed:@"C2 copy.JPEG"];
        

        if ([_floorName  isEqual: @"3rdFloor"]) 
            Background = [UIImage imageNamed:@"C3 copy.JPEG"];
        
    

    else if ([_daysName isEqual:@"DDay" ]) 
        if ([_floorName  isEqual: @"2ndFloor"]) 
            Background = [UIImage imageNamed:@"D2 copy.JPEG"];
        

        if ([_floorName  isEqual: @"3rdFloor"]) 
            Background = [UIImage imageNamed:@"D3 copy.JPEG"];
        
    

    else if ([_daysName isEqual:@"EDay" ]) 
        if ([_floorName  isEqual: @"2ndFloor"]) 
            Background = [UIImage imageNamed:@"E2 copy.JPEG"];
        

        if ([_floorName  isEqual: @"3rdFloor"]) 
            Background = [UIImage imageNamed:@"E3 copy.JPEG"];
        
    

    else if ([_daysName isEqual:@"FDay" ]) 
        if ([_floorName  isEqual: @"2ndFloor"]) 
            Background = [UIImage imageNamed:@"F2 copy.JPEG"];
        

        if ([_floorName  isEqual: @"3rdFloor"]) 
            Background = [UIImage imageNamed:@"F3 copy.JPEG"];
        
    

    else if ([_daysName isEqual:@"GDay" ]) 
        if ([_floorName  isEqual: @"2ndFloor"]) 
            Background = [UIImage imageNamed:@"G2 copy.JPEG"];
        

        if ([_floorName  isEqual: @"3rdFloor"]) 
            Background = [UIImage imageNamed:@"G3 copy.JPEG"];
        
    

    else 
        if ([_floorName  isEqual: @"2ndFloor"]) 
            Background = [UIImage imageNamed:@"H2 copy.JPEG"];
        

        if ([_floorName  isEqual: @"3rdFloor"]) 
            Background = [UIImage imageNamed:@"H3 copy.JPEG"];
        
    




- (void)didReceiveMemoryWarning 
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


@end

我们不知道为什么segue不起作用,为什么我们不知道如何设置MapViewController的背景。

提前感谢您的帮助!!

【问题讨论】:

“不起作用”这个短语没有传达任何信息。解释一下。 所有这些代码,没有实现 Map VC 上唯一重要的方法:configureWithImage: UIImageView 控制器? 【参考方案1】:

您需要将方法声明移至“MapViewController.h”,以便可以从第一个 viewController 访问它。 :

//MapViewController.h
@interface MapViewController : UIViewController
   -(void)configureWithImage:(UIImage *)paramImage;
@end

其次,您应该在原始视图控制器中调用 segue:

[self performSegueWithIdentifier:@"showFloorDetail" sender:self];

另外,如 cmets 中所述,在 MapViewController.m 中没有实现您的 configureWithImage 方法

-(void)configureWithImage:(UIImage *)paramImage 
    // Process your image here
    [yourImageView setImage:paramImage];

更多 cmets:MapViewController 不是 UIImageView。它在代码中的设置方式是 UIViewController 的子类化。此外,请以小写字母开头命名变量(根据 Apple 约定)。只有类的首字母大写。

【讨论】:

感谢您的建议,但您所说的一切我都做了。如果您对为什么它不起作用有任何其他想法,那将非常有帮助!它在 xcode 中。 你能告诉我们更多关于它为什么不起作用的信息吗?错误信息? NSlog 输出?

以上是关于如何设置 UIImageView 控制器目标 c 的背景图像?的主要内容,如果未能解决你的问题,请参考以下文章

如何在ios目标c的UIImageView中添加UIView

如何将图像设置为在另一个视图控制器中异步获取的 UIImageView?

如何简单地为 UIimageView 设置动画?

如何在目标 c 中创建程序化自动布局?

仅 UIImageView 顶部和底部的内部阴影(目标 C)

如何在按钮单击时获取 UIImageView 的高度、宽度、X 和 Y 并将其传递给其他 ViewController?