2012-01-20 112 views
1

有一個mainviewcontroller並且它有UIToolbar,它有一個UIBarButtonItem信息,以模態方式顯示UIViewcontroller。NavigationController在UIViewController上不顯示模態

現在當按Infobutton它顯示UIViewController模態,它具有UITextView但沒有顯示UINavigationController與完成按鈕。

我無法弄清楚我在代碼中缺少的東西。

這就是我如何以模態方式在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]; 

} 

我將不勝感激,如果你能找出我在做錯誤的或者在我的代碼失蹤。

非常感謝。

回答

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]; 
} 

現在你要做的圖形化安裝(導航欄顏色等)內的控制器initWithNib和/或viewDidLoad功能YourViewController

+0

我正在以編程方式處理它。 – user1120133

1
@synthesize navigationController; 

因此navigationController是您的類成員變量。

在功能

- (void) viewDidLoad 

聲明一個局部變量

UINavigationController *navigationController 

請注意,第二navigationController是從你的成員變量navigationController不同。

因此,在viewDidLoad裏面,你需要創建你的成員變量navigationController的對象。不是本地變量navigationController

請勿重新申報navigationControllerviewDidLoad。相反,使用如下成員變量創建對象:

navigationController = [[UINavigationController alloc]init]; 
+0

你能告訴我,如何創建對象的成員變量vieweDidLoad與代碼。 – user1120133

+0

我現在沒有MAC。但是,它應該像我上面寫的那樣。不過,您可能需要使用不同的init方法。 – haberdar

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 
    } 
} 
相關問題