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

时间:2016-12-07 18:05:12

标签: ios objective-c uiimageview

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

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的背景。

先谢谢你的帮助!!

1 个答案:

答案 0 :(得分:0)

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

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

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

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

另外,正如评论中所提到的,MapViewController.m中没有实现configureWithImage方法

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

更多评论:MapViewController不是UIImageView。在代码中设置它的方式,你是UIViewController的子类。另外,请命名以小写字母开头的变量(根据Apple惯例)。只应使用大写的第一个字母命名类。