简体   繁体   中英

MySecondViewController won't display correctly

I have programmatically created a second view controller (MySecondViewController) which was all done and hooked up without using the visual object editor.

My problem is that when I run the app, only the original view controller (HelloWorldViewController) comes up; the second view controller does not show up. When I click on the view for HelloWorldViewController, nothing comes up.

Can you tell me what's wrong?

windowBasedAppAppDelegate.h

#import <UIKit/UIKit.h>

//-- Add a forward reference to the HelloWorldViewController class --

@class HelloWorldViewController;

@interface windowBasedAppAppDelegate : NSObject <UIApplicationDelegate> {
    //-- create an instance of the view controller--
    HelloWorldViewController *viewController;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

//--Expose the view controller as a property--
@property (nonatomic, retain) IBOutlet HelloWorldViewController *viewController;

@end

windowBasedAppAppDelegate.m

#import "windowBasedAppAppDelegate.h"
#import "HelloWorldViewController.h"
#import "MySecondViewController.h"

@implementation windowBasedAppAppDelegate


@synthesize window;
@synthesize viewController;

//-- a second view controller object--
MySecondViewController *mySecondViewController;

- (BOOL)application:(UIApplication *)application     didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    //-- add the new view to the current window--
    //-- instantiate the second view controller --
    mySecondViewController = [[MySecondViewController alloc]
                          initWithNibName:nil
                          bundle:nil];

   // -- add the view from the second controller --
   //[window addSubview:mySecondViewController.view];

   [window addSubview:viewController.view];
   [self.window makeKeyAndVisible];
   return YES;
}

- (void)dealloc
{
    [MySecondViewController release];
    [viewController release];
    [window release];
    [super dealloc];
}

@end

MySecondViewController.h

#import <UIKit/UIKit.h>


@interface MySecondViewController : UIViewController {
    //-- create two outlets, label and button --
    UILabel *label;
    UIButton *button;

}

//-- expose the outlets as properties --

@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UIButton *button;

// -- declaring the IBAction ---
- (IBAction) buttonClicked: (id)sender;

@end

MySecondViewController.h

#import "MySecondViewController.h"


@implementation MySecondViewController

@synthesize label, button;

- (IBAction) buttonClicked: (id)sender {
    UIAlertView *alert =
    [[UIAlertView alloc] initWithTitle:@"Action Invoked!" message:@"Button clicked"     delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
    [alert release];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

HelloWorldViewController.h

#import <UIKit/UIKit.h>


@interface HelloWorldViewController : UIViewController {

}

@end

HelloWorldViewController.m

import "HelloWorldViewController.h"

@implementation HelloWorldViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
    }
    return self;
    }

- (void)dealloc
{
    [super dealloc];
}

Added code from MySecondViewController.m

//-- add the views to the view window --
    [self.view addSubview:label];
    [self.view addSubview:button];
    [view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
    self.view = view;
    [super viewDidLoad];

Since the view is being initialized and that it has a proper frame, the problem could be this sequence of code,

[window addSubview:mySecondViewController.view];
[window addSubview:viewController.view];

As the viewController 's view will have the same frame, it will be added over mySecondViewController 's view . So you will have to remove the second line for the view to show up.

You never actually push the new view controller onto the screen.

 mySecondViewController = [[MySecondViewController alloc]
                      initWithNibName:nil
                      bundle:nil];

// -- add the view from the second controller --
//[window addSubview:mySecondViewController.view];

   [window addSubview:viewController.view];

You add viewController.view, (which should be undefined...), but not mySecondViewController.view.

Also, programHasFinishedLaunching may not be the best possible place to push it. If I were you, I'd put a button on the front page that pushes the new view in, it's easier that way.

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