Create a UIImage from another view controller

女生的网名这么多〃 提交于 2019-12-12 04:10:03

问题


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


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/3932021/create-a-uiimage-from-another-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!