我正在制作贺卡应用。有三个视图控制器。在第一个视图中,我有一些图片。选择图像应将该图像设置为第三个视图控制器中的背景。 第一个视图有一个继续按钮,将我们带到第二个视图,另一个继续按钮>第二个视图将带我们进入第三个视图。现在,我们应该能够将第三个视图的背景图像视为在第一个视图中选择的图像。怎么办呢?
这是我的代码:
ViewController.h (第一个视图)
@interface ViewController : UIViewController {
GreetingCard *theme;
IBOutlet UIImageView *Bday_1;
IBOutlet UIImageView *Bday_2;
}
@property (nonatomic , retain) GreetingCard *theme;
- (IBAction)themeSelect:(UIButton *)sender ;
@end
ViewController.m
- (IBAction)themeSelect:(UIButton *)sender {
if (sender.tag == 0) {
self.theme.passedImage = Bday_1.image; //Passing the image to the second view
}
if (sender.tag == 1) {
self.theme.passedImage = Bday_2.image;
}
}
GreetingCard.h (第三视图)
@interface GreetingCard : UIViewController {
IBOutlet UIImageView *background;
UIImage *passedImage;
}
@property (nonatomic , retain) UIImage *passedImage;
@end
GreetingCard.m
- (NSString *) saveFilePath {
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[path objectAtIndex:0] stringByAppendingPathComponent:@"savefile.plist"];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSArray *values = [[NSArray alloc] initWithObjects: background.image,nil];
[values writeToFile:[self saveFilePath] atomically:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
background.image = passedImage; //The background image is set as the passedImage, which is the image chosen by the user in the first viewcontroller
/*** Loading the saved data ***/
NSString *myPath = [self saveFilePath];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];
if (fileExists)
{
NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
background.image = [values objectAtIndex:0];
}
UIApplication *myApp = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:myApp];
}
答案 0 :(得分:1)
不完全确定您的视图层次结构。
如果它们都位于同一个parentView控制器上并且包含它们: - 使用自定义协议和委托通知相应的控制器执行操作。
如果您使用navigationController作为父级:
答案 1 :(得分:0)
ViewControllerA
上创建一个私有属性以存储所选图像。segue
以获取“继续”按钮,并为其指定一个描述性名称(例如“segueContinue”)。ViewControllerB
上创建公共属性以存储所选图像(baackground)。prepareForSegue:segue sender:sender
方法。将目标视图控制器实例化为ViewControllerB
,并在B上设置属性以匹配来自A的选定属性。viewDidLoad
上,(或viewWillAppear
可能更好),将背景图像设置为您在该属性中放置的图像。在第一个视图控制器的.m
文件中,添加以下行:
@interface ViewController()
@property (nonatomic,retain) UIImage *selectedImage;
@end
高于@implementation
。
每当用户选择图像时,该图像应分配给self.selectedImage
。现在,还要将@property (nonatomic,retain) UIImage *background;
添加到第二个视图的.h
。
现在:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"segueContinue"]) {
GreetingCard *vc = [segue destinationViewController];
vc.background = self.selectedImage;
}
}
你应该好好去(假设你在第二个视图控制器viewDidLoad
或viewWillAppear
中正确地做了一切。