开发者

removefromsuperview and addsubview not changing display

开发者 https://www.devze.com 2023-03-15 00:18 出处:网络
I\'m pretty new to iOS development, and I\'m trying to develop a simple app in which a button changes the subviews. I have a base RootViewController, which displays MiddleView correctly on init. Middl

I'm pretty new to iOS development, and I'm trying to develop a simple app in which a button changes the subviews. I have a base RootViewController, which displays MiddleView correctly on init. MiddleView has a single button, labeled "First," which is connected (in Interface Builder) to RootViewController's -openFirstView.

Here's how MiddleView is displayed within RootViewController's -viewDidLoad

MiddleViewController *middleTemp = [[MiddleViewController alloc] initWithNibName:@"MiddleView" bundle:nil];
    self.middle = middleTemp;
    self.middle.rootViewController = self;
    [self.view addSubview:middle.view];
    [middleTemp release];
开发者_StackOverflow

So I have the following ViewControllerss: MiddleViewController and FirstController which control MiddleView and FirstView respectively, and a RootViewController which switches between the two.

I've linked this by placing a RootViewController reference in MiddleViewController, and adding

self.middle.rootViewController = self; 

to RootViewController's -viewDidLoad.

-(IBAction)openFirstView:(id)sender{
    [middle.view removeFromSuperview];
    [self.view addSubview:firstController.view];
}

Note: I've tried initializing firstController within -openFirstView, and when it initially didn't run, I moved the initialization to -viewDidLoad and have proven that it is initializing from the nib correctly by displaying FirstView directly in -viewDidLoad

Where firstController is loaded to a reference earlier in code. However, when I run the code and click the button, nothing in the view changes.

I've done some more diagnosing. I've found specifically that -ViewDidLoad in rootViewController is being called twice, once on the original load and once on the first click of the button, and I'm not sure exactly why.


It seems you haven't inialize your firstView in the below method

-(IBAction)openFirstView:(id)sender{
    [middle.view removeFromSuperview];
    //First initialize the firstController
    [self.view addSubview:firstController.view];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消