gpt4 book ai didi

ios - 将图像传递到另一个 View Controller 而不更改 View

转载 作者:行者123 更新时间:2023-11-29 03:26:37 25 4
gpt4 key购买 nike

我正在制作一个贺卡应用程序。一共有三个 View Controller 。在第一个 View 中,我有一些图像。选择图像应将该图像设置为第三 View Controller 中的背景。 第一个 View 有一个继续按钮,可以将我们带到第二个 View ,并且第二个 View 中有另一个继续按钮>第二个 View 将带我们到第三个 View 。现在我们应该能够看到第三 View 的背景图像作为在第一 View 中选择的图像。这怎么办?

这是我的代码:

ViewController.h(第一个 View )

@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(第三个 View )

@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];
}

最佳答案

不完全确定您的 View 层次结构。

如果它们都位于同一个parentView Controller 上并且它们被包含:- 使用自定义协议(protocol)和委托(delegate)来通知相应的 Controller 执行操作。

如果您使用 navigationController 作为父级:

  • 为第二个 View Controller 创建自定义 init,存储图像名称。将第二个 View Controller 推送到导航堆栈上。
  • 为第三个 View Controller 创建自定义 init,存储要显示的图像名称,从第二个 View Controller 获取。将第三个 View Controller 插入导航堆栈。
  • 在第三个 View Controller viewDidLoad上,将存储的图像设置为背景。

关于协议(protocol): https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html

关于导航 Controller : https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html

关于ios - 将图像传递到另一个 View Controller 而不更改 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20390327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com