change Subview of superview through IBAction of Subview ios
I tried to find a solution here but was unable to, basically I have an interactive subview that I need to switch to a new interactive subview from within the subview. I am able to change subviews easily with an IBAction in the main view, but I have been unsuccessful changing the view from within the subview. Here's the code... which should make what I am trying to accomplish more clear.
in .h of MainViewController
@interface MainViewController : UIViewController {
IBOutlet OneViewController *oneviewController;
IBOutlet TwoViewController *twoViewController;
}
-(IBAction)goToView;
@property (nonatomic, retain) IBOutlet OneViewController *oneviewController;
@property (nonatomic, retain) IBOutlet TwoViewController *twoViewController;
@end
in .m
@implementation MainViewController
@synthesize oneviewController;
@synthesize twoViewController;
-(IBAction)goToView {
[self.view addSubview:twoViewController.view];
}
开发者_高级运维
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:oneviewController.view];
}
Ok this all works great, i am able to load both ViewControllers with xib and all their` buttons work etc. ..
But when I try...in .m of OneViewController
#import "MainViewController"
...
[MainViewController.view addSubview:twoViewController.view]; ...
this does not work, I have tried a variety of things, by adding the twoViewController to the .h of oneViewController, but it crashed... I am still very new to all this so It is likely I am missing the obvious. Thanks for help!!
I also tried this in the .m of OneViewController
but it still crashed... although the build succeeded
MainViewController* mySuperView = (MainViewController*)self.view.superview;
TwoViewController *twoViewController;
[mySuperView.view addSubview:twoViewController.view];
This action is being done in the wrong place. If you are trying to add a subview to your MainViewController, you should really handle that inside of MainViewController.m. You are calling:
[MainViewController.view addSubview:twoViewController.view];
but MainViewController.view is a static instance of view. If you want to keep it as is, and assuming OneViewController is already a subview on MainViewController, you should do something like this:
UIView *main = [OneViewController.view superview]; [main addSubview:twoViewController.view];
This is pretty dangerous though because you're not guaranteed to have a superview for this view. I'd just handle all of this inside MainViewController. No controller should have the power to add a subview to another controller unless it had an instance of MainViewController.
the following code works, however it gives me 1 warning...
in my subview, OneViewController.m (under an IBAction) ...
{for (UIView* next = [self.view superview]; next; next = next.superview) {
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
[(UIViewController*)nextResponder resetSubView];
}`
in my MainViewController.m
- (void) resetSubView {
[self.view addSubview:twoViewController.view];
}
精彩评论