【问题标题】:UINavigationController not working as intended (iPhone)UINavigationController 未按预期工作(iPhone)
【发布时间】:2010-11-13 12:11:05
【问题描述】:

我的应用程序的主视图没有UINavigationController,但是当点击按钮时,它会转到视图以创建事件。这个 CreateEvent 视图确实有一个 UINavigationController 来移动创建新事件所需的不同视图。 UINavigationController 显示了但是不能设置标题或者添加任何导航按钮,看代码你能找出原因吗?谢谢

CreateNewEventViewController.h

@interface CreateNewEventViewController : UIViewController {

    UINavigationController *navigationController;
    NewEventTableViewController *tableViewController;
}

CreateNewEventViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];


    tableViewController = [[NewEventTableViewController alloc] init];
    tableViewController.view.center = CGPointMake(tableViewController.view.frame.size.width/2,
                                                  tableViewController.view.frame.size.height/2 + 44);
    [self.view addSubview:tableViewController.view];

    navigationController = [[UINavigationController alloc] init];
    //without this instruction, the tableView appears blocked
    navigationController.view.frame = CGRectMake(0, 0, 320, 44);
    navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    //title appears empty and buttons don't appear
    self.navigationItem.title = @"Create event";

    [self.view addSubview:navigationController.view];

}

NewEventTableViewController.h

@interface NewEventTableViewController : UITableViewController {

}

PS:我忘了告诉我我没有任何.xib 文件。

【问题讨论】:

    标签: iphone uinavigationcontroller uinavigationitem


    【解决方案1】:

    假设您希望表格视图出现在导航控制器中,您需要执行以下操作:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
    
        tableViewController = [[NewEventTableViewController alloc] init];
        tableViewController.view.center = CGPointMake(tableViewController.view.frame.size.width/2,
                                                      tableViewController.view.frame.size.height/2 + 44);
    
     // remove this:   [self.view addSubview:tableViewController.view];
    
        navigationController = [[UINavigationController alloc] initWithRootViewController:tableViewController]; // <-- do this instead
        //without this instruction, the tableView appears blocked
        navigationController.view.frame = CGRectMake(0, 0, 320, 460); // <-- nav controller should fill the screen
        navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    
        //title appears empty and buttons don't appear
        tableViewController.navigationItem.title = @"Create event"; // <-- something like this
    
        [self.view addSubview:navigationController.view];
    
    }
    

    【讨论】:

    • 谢谢!它终于奏效了,我真的应该更深入地了解 UINavigationController 主题。只需评论从 tableViewController.navigationItem.title = @"Create event"; 设置标题即可没有用,但它是从 NewEventTableViewController.m 中的 viewDidLoad 完成的
    猜你喜欢
    • 1970-01-01
    • 2020-04-13
    • 2021-06-04
    • 2022-01-24
    • 2015-05-11
    • 2020-05-15
    • 2014-10-31
    • 2018-02-12
    • 2014-01-20
    相关资源
    最近更新 更多