我有一个带有选项卡栏 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/