I would like to know how can I load a new nib file after finishing with UIImagePicker Controller. So after the user takes a picture from the camera or albums, i would like to load a new nib for the user to do some editing. Below is the didFinishPickingMediaWithInfo. I can load an image to the current UIImageView or even send out and alertview, but even if I tried loading a new nib, it just goes back to currentview nib and no errors. Bear in mind that I'm a total ios noob, so if the method use is wrong, please let me know. Thanks in advance.
p.s. Just to add, even if I manage to load new nib, how do I pass the information from the previous nib? E.g. if I choose an image from nib2, how to pass it to nib3?
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
imageOverlay.image = [UIImage imageNamed:@"mask_overlay1.png"];
[imageOverlay release];
// Displaying image to the imageView
imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
// Tried to load a new nib
UIView *currentView = self.view;
// Get the underlying UIWindow, or the view containing the current view.
UIView *theWindow = [currentView superview];
//remove the current view and replace with myView1
[currentView removeFromSuperview];
// set up an animation for the transition between the views
CATransition *animation = [CATransition animation];
[a开发者_高级运维nimation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[theWindow layer] addAnimation:animation forKey:@"createPhotoView"];
}
I have no idea what your app's workflow is like, but if you need to display a new view after an image is chosen, you should probably do it by either pushing a new view controller on to a navigation controller, or by presenting it as a modal view. This allows you to easily return to the previous screen where you were asked to pick a photo.
Let's say that for your app, you decide to use a UINavigationController
.
You would initialize a navigation controller with your first view controller as the root view controller.
Then, in your UIImagePicker delegate method, you would have something like this:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// Initialize your new view controller
CustomViewController *controller = [[CustomViewController alloc] initWithNibName:@"SecondView" bundle:nil];
// Pass your custom view controller the image you just selected
controller.valueToPass = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
You create a subclass of UIViewController
called CustomViewController, and pass it values by creating properties you can assign to (like valueToPass in my example).
EDIT:
It is possible to just switch the visible view of a view controller. It is as simple as changing the value of self.view
. You could put two UIViews in the same XIB file, and put IBOutlets for both in your controller. You could have a header that looks like this:
@interface MyController : UIViewController {
IBOutlet UIView *view1;
IBOutlet UIView *view2;
}
// Follow this with properties for all of those IBOutlets as well
Then, in your controller code:
- (IBAction)switchViews {
if ([self.view isEqualTo:view1]) {
self.view = view2;
} else {
self.view = view1;
}
}
This works, but as far as I am aware, does not allow transitions. You could instead do this:
- (IBAction)switchViews:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];
if ([[secondaryView superview] isEqual:mainView]) {
[secondaryView removeFromSuperview];
} else {
[self.view addSubview:secondaryView];
}
[UIView commitAnimations];
}
Both of these options work, and allow you to use the same view controller to control both views. However, if the views perform different tasks, they should be associated with different controllers. Using different controllers is similar if you set up a controller as a property in your header:
- (IBAction)switchViews:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];
if (self.secondaryController) {
[self.secondaryController.view removeFromSuperview];
self.secondaryController = nil;
} else {
self.secondaryController = [[SecondaryController alloc] initWithNibName:@"SecondView" bundle:nil];
self.secondaryController.valueToPass = @"I am passing this value!";
[self.view addSubview:self.secondaryController.view];
}
[UIView commitAnimations];
}
精彩评论