NavigationController не отображается на UIViewController, представляющем модально - PullRequest
1 голос
/ 21 января 2012

Имейте mainviewcontroller, и на нем есть UIToolbar, который имеет UIBarButtonItem Info, который показывает UIViewcontroller модально.

Теперь при нажатии кнопки Infobutton модально отображается UIViewController, который имеет UITextView, но не показывает UINavigationController с кнопкой Done.

Я не могу понять, что мне не хватает в моем коде.

Вот как я показываю UITextView и NavigationController в UIViewController модально.

#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 ]

1 голос
/ 21 января 2012

Это похоже на излишне запутанный способ отображения нового контроллера.В моих приложениях я делаю это так:

// эта функция отображает (модально) вид контроллера, вложенного в 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];
}

Теперь вы хотите выполнить графическую настройку (цвет navbarи т. д.) initWithNib и / или viewDidLoad функций YourViewController .

1 голос
/ 21 января 2012
@synthesize navigationController;

поэтому navigationController - это переменная члена вашего класса.

в функции

- (void) viewDidLoad

вы объявляете локальную переменную

UINavigationController *navigationController

Обратите внимание, что вторая navigationController отличается от вашей переменной-члена navigationController.

Итак, внутри viewDidLoad вам нужно создать объект вашей переменной-члена navigationController. Не локальная переменная navigationController.

Не объявлять повторно navigationController в viewDidLoad. Вместо этого создайте объект, используя переменную-член, например:

navigationController = [[UINavigationController alloc]init];
0 голосов
/ 13 января 2015

Попробуйте, у меня это отлично работает.У меня была точно такая же проблема

- (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
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...