ViewController已加载,但视图未显示

时间:2011-09-09 17:42:58

标签: iphone objective-c ios uiviewcontroller

我有一个视图,其控制器正在实例化(NSLog这样说),但视图没有显示。如果我将其加载为模态视图,则会显示,但如果我将其分配则不会。

我有这种结构(MenuView是没有出现的视图):

// ViewController.h

#import "MenuViewController.h"
@class MenuViewController;
@interface ViewController : UIViewController<ASIHTTPRequestDelegate>{
    ...
    IBOutlet MenuViewController         *menuView;
}
...
@property(nonatomic, retain) MenuViewController *menuView;
@end

// ViewController.m

#import "MenuViewController.h"

@implementation ViewController

@synthesize menuView;

- (void)loadMenu{
  // THIS WORKS
  // [self presentModalViewController:menuView animated:YES];

  // THIS DOESN'T (VIEWCONTROLLER IS INSTANTIATED BUT VIEW DOESN'T APPEAR

  menuView = [[[MenuViewController alloc] initWithNibName:@"MenuView" bundle:Nil] autorelease];
  [self.navigationController pushViewController:menuView animated:YES];
}

1 个答案:

答案 0 :(得分:2)

一些想法:

在分配时尝试使用self.menuView:

self.menuView = [[MenuViewController alloc] initWithNibName:@"MenuView" bundle:Nil];

此外,可能不应该自动释放property。在dealloc中将其释放,并在nil中将其设置为viewDidUnload


确保self (ViewController)navigationController。被ViewController

推出/提出navigationController

是否从MainThread调用- (void)loadMenu{?查看[NSThread mainThread]


查看一些教程/示例:

Adding a Navigation Controller by Hand

NavigationController Application in iPhone

Tutorial: Introducing UINavigationController Part 1

iPhone View Switching Tutorial