self.view insertSubView .... not supposed to be self.view, but what?
I have a UIViewController which shows different UIViewController sub-classes. In the main UIViewController.m, I have a sub-class called 'Home' load on app start.
Now, which the Home view loaded, I have a button which I want to use to switch to another view called 'PreGameInfo'. I'm trying to use the code below:
- (IBAction)showPreGameInfo:(id)sender {
[self.view insertSubview:preGameInfo.view atIndex:0];
}
It doesn't work, and I know it's because the 'self.view' needs to refer to the main UIViewController rather than the self of the 'Home' view. Does anyone know how to inser开发者_JAVA百科tSubView to the main UIViewController when using a UIButton whilst in a SubView???
Thank you!
You can use a delagate. It very easy
So implement this in your information view controller;
In the InformationViewController.h
@protocol InformationViewControllerDelegate;
@interface InformationViewController : UIViewController {
id <InformationViewControllerDelegate> delegate;
}
@property (nonatomic, assign) id <InformationViewControllerDelegate> delegate;
- (IBAction)returnBack:(id)sender;
@end
@protocol InformationViewControllerDelegate
- (void)InformationViewControllerDidFinish:(InformationViewController *)controller;
@end
in the InformationViewController.m
- (IBAction)returnBack:(id)sender {
[self.delegate InformationViewControllerDidFinish:self];
}
And use the delegate in any view controller you need it like this :
In the HomeViewController.h
#import "InformationViewController.h"
@interface HomeViewController : UIViewController <InformationViewControllerDelegate> {
}
Write the method to change the view from Home view to Information view
- (IBAction)goToInformationView:(id)sender;
In the HomeViewController.m
- (IBAction)goToInformationView:(id)sender {
InformationViewController *controller = [[InformationViewController alloc] initWithNibName:nil bundle:nil];
controller.delegate = self;
// You can chose the transition you want here (they are 4 see UIModalTransitionStyle)
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
And the last but not least the delegate method it inform the HomeViewController when the InformationViewController had finished
- (void)InformationViewControllerDidFinish:(InformationViewController *)controller {
[self dismissModalViewControllerAnimated:YES];
}
I hope it helps
- (IBAction)showPreGameInfo:(id)sender {
[superview insertSubview:preGameInfo.view atIndex:0];
}
Does this code work?
[self.parentViewController.view addSubview:myView];
Just do it in the modalView:
[self presentModalViewController:preGameInfo animated:YES]
or you can do something like this...
This line will add your new view to windows rootViewControllers view
[self.view.window.rootViewController.view addSubview:preGameInfo.view];
精彩评论