【问题标题】:Multiple buttons to one Table View一个表视图的多个按钮
【发布时间】:2013-10-26 07:19:26
【问题描述】:

有人能指出我正确的方向吗?我有一个带有多个按钮的视图控制器。我希望按下按钮打开我自定义的一个表视图控制器并动态加载数据。我知道我可以为每次按下按钮制作多个表格视图控制器,但我认为必须有一种方法可以动态地做到这一点。让我明确一点,另一个视图将确定将哪些数据加载到表视图控制器中。我是 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)。感谢大家的帮助。

【问题讨论】:

  • 每个按钮包含什么?你想使用他们的文本值还是什么?

标签: ios objective-c xcode


【解决方案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 = ...
}
}

希望对你有帮助..

【讨论】:

    【解决方案2】:

    假设你的表有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。

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

    【讨论】:

      【解决方案3】:

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

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

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

      然后在按钮处理程序中

      [self perfomSegue:@"segue" sender:buttonTitle];
      

      【讨论】:

        【解决方案4】:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-03-21
          • 1970-01-01
          • 2015-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-19
          • 1970-01-01
          相关资源
          最近更新 更多