开发者

Create a UIImage from another view controller

开发者 https://www.devze.com 2023-01-19 19:38 出处:网络
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?

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 viewcontr开发者_如何学Pythonoller, which when pressed, dismisses this view and create a UIImage in the other view.

How is this done?

Thanks


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消