objective-c - 需要一个非常简单的导航 Controller ,在选项卡栏 Controller 内带有表格 View

标签 objective-c ios uitableview uinavigationcontroller

我有一个带有选项卡栏 Controller (2 个选项卡)的应用程序。在一个选项卡 View Controller 中,一个按钮会导致一个警报窗口。我希望警报窗口的一个按钮可以调用包含可能答案的表格 View 。我希望该 TableView 有一个“完成”按钮和一个“标题”。我认为这意味着必须使用导航 Controller 。但我在导航 Controller 上找到的大多数内容都假设了更为复杂的情况。这是警报窗口逻辑的一部分:

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{ 
    if (buttonIndex == 2) {
        AnswersViewController *aVC = [[AnswersViewController alloc] init];
        [self presentViewController:aVC
                           animated:YES
                         completion:NULL];    
    }
} 

AnswersViewController 看起来像这样:

@interface AnswersViewController : UITableViewController
@end

@implementation AnswersViewController

- (id) init
{
    self = [super initWithStyle:UITableViewStylePlain];
    return self;
}


- (id) initWithStyle:(UITableViewStyle)style
{
    return [self init];   

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[self view] setBackgroundColor:[UIColor redColor]];

}

@end

此代码全部按预期工作(出现一个空的红色 UITableView)。

我猜有两个问题:1.是否可以对我所拥有的内容进行简单的修改,以便在我的表格 View 中提供一个完成按钮和标题? 2. 如果我必须使用导航 Controller (可能),我怎样才能制作一个带有 done 按钮和 title 的简单导航 Controller 并嵌入表格 View 其内?哦,我想以编程方式执行此操作。我想我更喜欢将 done 按钮和 title 放在导航栏中,不需要工具栏。谢谢!

最佳答案


要获得您正在寻找的内容,您确实需要使用 UINavigationController。这将提供 UINavigationBar,您可以在其中显示标题和按钮。

要使用 UINavigationController 实现此功能,您需要像这样进行平滑(假设您使用的是 ARC,因此您无需担心内存管理):

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{ 
    if (buttonIndex == 2) {
        AnswersViewController *aVC = [[AnswersViewController alloc] init];

        //Make our done button
        //Target is this same class, tapping the button will call dismissAnswersViewController:
        aVC.navigationItem.leftBarButtonItem =  [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissAnswersViewController:)];

       //Set the title of the view controller
       aVC.title = @"Answers";

        UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:aVC];
        [self presentViewController:aNavigationController
                       animated:YES
                     completion:NULL];
    }
} 

然后,您还可以在与 UIAlertView 委托(delegate)方法相同的类中实现 - (void)dismissAnswersViewController:(id)sender (基于我此处的实现)。

希望这有帮助!

关于objective-c - 需要一个非常简单的导航 Controller ,在选项卡栏 Controller 内带有表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11379247/

相关文章:

Objective-C:重构代码 - 如何获取指向 View 实例的指针?

ios - 如何保存带有混响效果的录制音频

ios - iPhone永久数据存储

ios - 使用 canEditRowAtIndexPath 从解析中删除图像

ios - 如何使用 Storyboard在自定义 uitableview 中为 5 个 UILabel 系列提供约束?

ios - 禁用具有单元可重用性的 UITableViewCell 中的 UIButton

ios - 转换从 View : and strange behavior with flip.

ios - iOS 的 WebView 禁止类名?

ios - 为标识符 (CELLNAME) 注册的 nib 无效 - nib 必须恰好包含一个顶级对象,该对象必须是 UITableViewCell 实例

objective-c - 从应用程序保存到联系人 : Nothing Happening/Error