0

有人可以指出我正确的方向吗?我有一个带有多个按钮的视图控制器。我希望按下按钮来打开我自定义的一个表视图控制器并动态加载数据。我知道我可以为每次按下按钮制作多个表格视图控制器,但我认为必须有一种方法可以动态地做到这一点。让我明确一点,另一个视图将确定将哪些数据加载到表视图控制器中。我是 iOS 和 Objective-C 的新手,但不是编程新手,所以在火箭科学的答案上放轻松。如果您需要查看一些代码,请告诉我。

基本上,我在一个视图控制器上有 4 个按钮,按下时将确定要在表视图上加载哪些数据。现在让我们保持简单,只说数据是 4 个 NSMutable 数组。

编辑:根据 babygau 的帖子,这是我想出的:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"allCattleSegue"]) {
        CattleTableViewController *controller = segue.destinationViewController;
        controller.cattleArray = [[NSMutableArray alloc] initWithObjects:@"cattle1",@"cattle2",@"cattle3", nil];
    }
    else if ([segue.identifier isEqualToString:@"bullsSegue"]){
        CattleTableViewController *controller = segue.destinationViewController;
        controller.cattleArray = [[NSMutableArray alloc] initWithObjects:@"bull1",@"bull2",@"bull3", nil];
    }
}

- (IBAction)allCattleTouchUpInside:(UIButton *)sender {
    [self performSegueWithIdentifier:@"allCattleSegue" sender:self];
}

- (IBAction)bullsTouchUpInside:(UIButton *)sender {
    [self performSegueWithIdentifier:@"bullsSegue" sender:self];
}

这样就完成了工作,但是现在当我按下后退按钮时,我的导航无法正常工作。它给了我一个空白屏幕。

更新:调试我看到它调用了 prepareForSegue 两次;一次将控制器作为发送者,再次将按钮作为发送者。仍在尝试了解其工作原理以及如何解决此导航问题。

已解决:显然我不需要连接动作(TouchUpInside)。感谢所有的帮助。

4

4 回答 4

1

您好,只需创建一个 Viewcontroller 和 TableViewController。为所有按钮设置标签,然后在 TableViewController 中创建一个 @property int 变量,然后像下面这样..

创建通用方法来触发UIControlTouchUpInside中的所有按钮。然后在 UITableViewController 中创建这样的 int 变量。

您像这样创建的TableViewController.h文件。

@property (nonatomic, assign) int buttonSelected;

ViewController.m像这样创建通用方法。

-(IBAction)commonMethod:(id)sender
{
TableViewController *tableVc = [[TableViewController alloc]initWithNibName:@"TableViewController" bundle:nil];
if(button.tag==0)
{
 tableVC.buttonSelected = 0;
}
if(button.tag==1)
{
tableVC.buttonSelected = 1;
}

[self.navigationController pushViewController:tableVc animated:YES];

}

TableViewConroller.m文件执行此操作

-(void)viewDidLoad
{
[super viewDidLoad];

if(buttonPressed == 0)
{
tableDataSourceArray = ...
}
else if (buttonPressed == 1)
{
tableDataSourceArray = ...
}
}

我希望它对你有帮助..

于 2013-10-25T04:50:14.343 回答
0

您可以传递诸如按钮标签或标识符之类的东西来告诉使用哪个可变数组。

如果您在 prepareForSegue 方法中使用 segue 进行代码转换,则可以将值传递给目标 TableViewController 即

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
        UITableViewController *controller = segue.destinationViewController;
        controller.buttonTitle = sender;
}

然后在按钮处理程序中

[self perfomSegue:@"segue" sender:buttonTitle];
于 2013-10-25T03:02:33.820 回答
0

为每个按钮分配标签。根据标签,您可以在表中填充不同的数据。只需要一个数组。根据标签将数据填充到该数组中

if(button.tag==0)
{
//array=@[..];
}
if(button.tag==1)
{
//array=@[..];
}
.
.
.

cellForRowWithIndexPath现在在委托方法中使用这个数组

于 2013-10-25T03:02:44.453 回答
0

假设您的表有tableViewData一个数组。

在每个按钮操作中,您使用以下模式代码:

[self performSegueWithIdentifier:@"YOUR_IDENTIFIER" target:sender];

实现以下 segue 委托方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    YOURCUSTOMTABLEVIEWCONTROLLER *tableVC= segue.destinationViewController;
    tableVC.tableViewData = YOUR_CORRESSPONDING_MUTABLE_ARRAY;
    [tableVC.tableView reloadData];

}

就是这样,您将根据单击的按钮动态显示 tableView。

注意:您必须在对应于 4 个按钮的 View Controller 之间创建 4 个 segue,并为每个 segue 命名。例如@"fromButton1toTableVC"、@"fromButton2toTableVC" 等等...

于 2013-10-25T03:07:16.930 回答