NavigationController没有在UIViewController上显示模态

时间:2012-01-20 22:22:20

标签: iphone

有一个mainviewcontroller,并且它上面有UIToolbar,它有一个UIBarButtonItem信息,以模态方式显示UIViewcontroller。

现在当按下Infobutton时,它显示UIViewController模态,它有UITextView但没有显示UINavigationController和Done按钮。

我无法弄清楚我的代码中缺少的是什么。

这就是我在UIViewController中以模态方式显示UITextView和NavigationController的方法。

#import "ModalViewController.h"
#import <QuartzCore/QuartzCore.h>

@implementation ModalViewController

@synthesize textView;
@synthesize navBar;
@synthesize navigationController;
@synthesize delegate;

-(void)dealloc
{
  [textView release];
[navBar release];
[navigationController release];
[super dealloc];
 }

- (void) viewDidLoad
{
    [super viewDidLoad];

self.title = @"Info";

UINavigationController *navigationController = [[UINavigationController alloc]init];
                                                 //initWithRootViewController:viewController];

self.navigationController.navigationBar.tintColor = [UIColor brownColor];

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(Done:)] autorelease];

self.textView = [[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)]autorelease];

self.textView.textColor = [UIColor whiteColor];

self.textView.font = [UIFont fontWithName:@"Georgia-BoldItalic" size:14];

//self.textView.delegate = self;

self.textView.backgroundColor = [UIColor brownColor];

self.textView.layer.borderWidth = 1;

self.textView.layer.borderColor = [[UIColor whiteColor] CGColor];

self.textView.layer.cornerRadius = 1;

self.textView.textAlignment =  UITextAlignmentCenter;

self.textView.text = @"This is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.

self.textView.editable = NO;

 //[self.view addSubview:navigationController.view];

[self.view addSubview: self.textView]; 

//[navigationController release];

}

这就是UIViewController以模态方式呈现的方式

//Create a final modal view controller

    UIButton *modalViewButton = [UIButton buttonWithType:UIButtonTypeInfoLight];

    [modalViewButton addTarget:self action:@selector(modalViewAction:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *modalBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:modalViewButton]; 

   self.navigationItem.rightBarButtonItem = modalBarButtonItem;

- (void) modalViewAction:(id)sender

{
    self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];  

 //self.viewController = [[ModalViewController alloc] init];

 [self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];


 //ModalViewController *myModalViewController = [[ModalViewController alloc] init];

  //UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myModalViewController];

    //navigationController.navigationBar.tintColor = [UIColor brownColor];

    _viewController = [[ModalViewController alloc] init];

   //[navigationController pushViewController:_viewController animated:YES];

  [self presentModalViewController:self.viewController animated:YES];

   //[self.view addSubview:navigationController.view];

    //[navigationController release];



    [myModalViewController release];

}

如果您能在我的代码中弄清楚我做错了什么或遗漏了什么,我将不胜感激。

非常感谢。

3 个答案:

答案 0 :(得分:1)

@synthesize navigationController;

所以navigationController是你的类成员变量。

中的函数

- (void) viewDidLoad

你声明一个局部变量

UINavigationController *navigationController

请注意,第二个navigationController与您的成员变量navigationController不同。

因此,在viewDidLoad内,您需要创建成员变量navigationController的对象。不是局部变量navigationController

请勿在{{1​​}}中重新声明navigationController。而是使用成员变量创建对象,如:

viewDidLoad

答案 1 :(得分:1)

这似乎是一种显示新控制器的不必要的复杂方式。在我的应用中,我这样做:

//此函数显示(模态)视图控制器嵌套在navcontroller

- (void) showModalController
{
    YourViewController * ecreator = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];

    UINavigationController * navcontrol = [[UINavigationController alloc] initWithRootViewController: ecreator];

    [self presentModalViewController: navcontrol animated:YES];
    [navcontrol release];
    [ecreator release];
}

现在,您希望在 YourViewController initWithNib 和/或 viewDidLoad 函数中进行图形设置(导航栏颜色等)。 / p>

答案 2 :(得分:0)

试试这个,它对我很有用。我有完全相同的问题

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@“segue_name"])
    {
        UINavigationController *nav = [segue destinationViewController];
        ExampleViewController *exampleVC = (ExampleViewController *) nav.topViewController;

        //Setup any properties here and segue
    }
}