MySecondViewController无法正确显示

| 我已经以编程方式创建了第二个视图控制器(MySecondViewController),无需使用可视对象编辑器即可完成所有工作并进行连接。 我的问题是,当我运行该应用程序时,只有原始视图控制器(HelloWorldViewController)出现;第二个视图控制器不显示。当我单击HelloWorldViewController的视图时,什么都没有出现。 你能告诉我怎么了吗? 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 导入\“ 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];
}
从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];
    
已邀请:
        由于ѭ7正在初始化且帧正确,因此问题可能出在此代码序列上,
[window addSubview:mySecondViewController.view];
[window addSubview:viewController.view];
由于
viewController
view
具有相同的帧,因此将其添加到
mySecondViewController
view
上。因此,您必须删除第二行才能显示该视图。     
        您实际上从未将新的视图控制器推到屏幕上。
 mySecondViewController = [[MySecondViewController alloc]
                      initWithNibName:nil
                      bundle:nil];

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

   [window addSubview:viewController.view];
您添加了viewController.view(应该未定义...),但没有添加mySecondViewController.view。 另外,programHasFinishedLaunching可能不是最好的推送位置。如果我是您,我会在首页上放置一个按钮以将新视图推入,这样更容易。     

要回复问题请先登录注册