views:

53

answers:

2

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  A: 

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];
}
muffix
self.otherViewController causes an error request for membet not a union or structure. I import the viewcontroller I need so thats not the problem. Thanks
alJaree
You need to declare otherViewController in your .h file and create the properties.
muffix
Will this not add the image to a newly created view? I already have a view I want the image to appear on.
alJaree
It creates a UIImage variable, which is not a view. You can then display that UIImage in a UIImageView and add it as a subview to an existing view. Place the code in -createNewImage.
muffix
A: 

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.

Thomas Clayson
I init the views and present them.
alJaree
present them modally? `presentModalViewController:animated:`? Then use the same method as above but instead of `[self.navigationController pushViewController:page animated:YES];` do `[self presentModalViewController:page animated:YES];`
Thomas Clayson
View 1 has a button which opens view2. view2 has a button which when selected should create a UIImage in view1. So view1 already exists and might hold other data like other UIImages holding images. If I create a new one, init, then setMakeImage wont that create a new one and then I will lose all my other data?
alJaree