【问题标题】:create navigation bar for tableView programmatically in objective-C在objective-C中以编程方式为tableView创建导航栏
【发布时间】:2012-10-17 17:25:24
【问题描述】:

我想以编程方式为我的表格视图控制器类创建导航栏,您能帮帮我吗? 我无法修复它!

提前致谢! 我真的是 iOS 编程新手!

这是我的表格视图控制器类的代码

 @implementation CheckedInOut

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

}

- (void)viewDidUnload
{
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
 return 0;
 }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...

 return cell;
}
#pragma mark - Table view delegate

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib               name#>" bundle:nil];
 // ...
 // Pass the selected object to the new view controller.
 [self.navigationController pushViewController:detailViewController animated:YES];
 */
}

@end

【问题讨论】:

    标签: objective-c ios storyboard


    【解决方案1】:

    你不要为表格视图控制器创建导航栏,你应该做的是创建一个导航控制器并将表格视图控制器设置为其根

    UITableViewController *myTableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    UINavigationController *tableViewNavigationController = [[UINavigationController alloc] initWithRootViewController:myTableViewController];
    
    //use the navigation controller here instead of how you had used the table view controller
    

    【讨论】:

    • 它被放置在你创建表格视图控制器的地方。例如如果它是您的根控制器,则在应用程序委托中您将 tableViewNavigationController 设置为 Windows rootViewController 而不是 myTableViewController
    • 12 对不起,我对 iOS 很陌生,但我把我的代码放在了我的表视图控制器类
    • 您不能在该视图控制器内为一个 viwe 控制器设置导航栏,它需要在显示该视图控制器之前完成。您必须在代码的某处创建 CheckedInOut 的实例。您需要创建一个 UINavigationController 并使用 CheckedInOut 作为 rootViewController 并改用导航控制器
    • apples view controller guide 是一本很好的读物,可以了解为什么需要这样设置
    【解决方案2】:

    这三行

    ViewController *vc = [[ViewController alloc]init];// or UITableVC as which VC you have in your file
    UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:vc];
        [self.window addSubview:navController.view]; // this is an important line if missed out dont show navController
    

    应该加在

    之后
     [self.window makeKeyAndVisible];
    

    appDelegate.m didFinishLaunchingWithOptions 方法中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      相关资源
      最近更新 更多