简体   繁体   中英

iphone switching between one UIViewController to another

I have one main view controller named GuideViewController and another view controller named RestauranterViewController. In GuideViewController i have a UIView hierarchy where guideParentView resides under mainView. Now i want to switch to the other view controller named RestauranterViewController and here the i am loading RestauranterViewController.xib over GuideViewController.xib where mainView resides but im making guideParentView hidden. Now i want to switch from RestauranterViewController to the GuideViewController. But i can not. Can anyone please help me?

@class RestauranterViewController;
@interface GuideViewController : UIViewController {
    IBOutlet UIView *mainView;
    IBOutlet UIView *guideParentView;
}
@property(nonatomic,readonly,retain) IBOutlet UIView *mainView;
@property(readwrite,assign) IBOutlet UIView *guideParentView;

Now when i am clicking on the switch button then the method like below is calling

#import "GuideViewController.h"
#import "RestauranterViewController.h"
@implementation GuideViewController

-(IBAction)select:(id)sender{
    guideParentView.hidden = YES;
    RestauranterView = [[RestauranterViewController alloc] initWithNibName:@"RestauranterViewController" bundle:[NSBundle mainBundle]];
    RestauranterView.view.frame = CGRectMake(0, 100, 299, 220);

    [mainView addSubview: RestauranterView.view];
}

And in the RestauranterView when i clicking on a button to make the hidden view visible but it fails.

@interface RestauranterViewController : UIViewController {
    GuideViewController *GuideView;
}


#import "RestauranterViewController.h"
#import "GuideViewController.h"
@implementation RestauranterViewController

-(IBAction)back:(id)sender{
    GuideView = [[GuideViewController alloc] init];
    GuideView.guideParentView.hidden = NO;
    [self.view removeFromSuperview];
}

Seems to be that it can not call the parent method. Please help me...

You are creating new instances of both controllers every time you intend to move back and forth. It also creates a leak.

You should really look at UINavigationController. It will really simplify your problem if in future you need to add more controllers. There are loads of tutorials on how to create one. You can start with this .

At last i have solved it by following code I can use either objectAtIndex or viewWithTag

-(IBAction)back:(id)sender{
    superView = (UIView *)self.view.superview;
    //UIView *perentView = [[topView subviews] objectAtIndex:6];//Its the Object Index in the XIB file
    UIView *parentView = (UIView *)[superView viewWithTag:177];//I have put a Tag of 177 in the tag field in the XIB.
    parentView.hidden = NO;
    [self.view removeFromSuperview];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM