从另一个视图控制器创建 UIImage

发布于 2024-09-27 22:13:24 字数 144 浏览 6 评论 0原文

当我想要触发创建的操作是另一个视图控制器中的按钮时,如何在一个视图控制器中创建 UIImage?

所以我想在视图控制器中有一个按钮,当按下该按钮时,会关闭该视图并在另一个视图中创建一个 UIImage。

这是怎么做到的?

谢谢

How does one create a UIImage within one viewcontroller, when the action which I want to trigger the creation is a button in another viewcontroller?

So I would like to have a button in a viewcontroller, which when pressed, dismisses this view and create a UIImage in the other view.

How is this done?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久随 2024-10-04 22:13:24

您可以在按钮保持 VC 中保留对图像保持 VC 的引用。实例变量(例如 otherViewController)似乎很合适。

在第一个 VC 中,您实现了创建新 UIImage 的方法。

-(void)createNewImage {
    UIImage *image = [UIImage imageNamed:"image.png"];
}

在第二个 VC 中,您创建一个在点击按钮时调用的方法。在此方法中,您访问第一个 VC 并调用刚刚编写的方法。

-(IBAction)newImageButtonTapped:(id)sender {
    [self.otherViewController createNewImage];
    [self dismissModalViewControllerAnimated:YES];
}

You can keep a reference to the image-holding VC in the button-holding VC. An instance variable (e.g. otherViewController) seems suitable.

In the first VC, you implement a method that creates the new UIImage.

-(void)createNewImage {
    UIImage *image = [UIImage imageNamed:"image.png"];
}

In the second VC, you create a method that is called when the button is tapped. In this method, you access the first VC and call the just written method.

-(IBAction)newImageButtonTapped:(id)sender {
    [self.otherViewController createNewImage];
    [self dismissModalViewControllerAnimated:YES];
}
对岸观火 2024-10-04 22:13:24

你需要更具体。你如何展示下一个视图控制器?您是否正在启动它然后推送它?

如果您在该视图控制器中,则可以拥有一个 BOOL 属性 (BOOL makeImage;)。当您创建要推送的视图时,请执行以下操作:

TheNextViewController *page = [[TheNextViewController alloc] initWithNibName:nil bundle:nil];
[page setMakeImage:YES];
[self.navigationController pushViewController:page animated:YES];

然后在 TheNextViewControllerviewDidLoad 中:

- (void)viewDidLoad{
  if(makeImage){
    // make you image here and add it to the view
  }
}

您可能必须设置其他变量,例如 NSString *imageName;如果您希望按钮显示特定图像,请在 ...setMakeImage:YES] 时设置它们。

You need to be more specific. How are you showing the next view controller? Are you init'ing it and then pushing it?

If you are then in that viewcontroller you could have a BOOL property (BOOL makeImage;). When you create the view to push do this:

TheNextViewController *page = [[TheNextViewController alloc] initWithNibName:nil bundle:nil];
[page setMakeImage:YES];
[self.navigationController pushViewController:page animated:YES];

then in viewDidLoad of TheNextViewController:

- (void)viewDidLoad{
  if(makeImage){
    // make you image here and add it to the view
  }
}

You might have to set other variables up like NSString *imageName; and set those when you ...setMakeImage:YES] if you want the button to show a specific image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文