【问题标题】:Using programmatically created UIButtons with the same method to load different UIViewControllers使用通过相同方法以编程方式创建的 UIButton 来加载不同的 UIViewController
【发布时间】:2019-05-07 23:25:54
【问题描述】:

我以编程方式生成了所有共享相同选择器方法的 UIButton。当方法运行时,我希望该方法知道按下了哪个按钮,然后能够加载相应的 UIViewController。

-(void)buildButtons
{
    for( int i = 0; i < 5; i++ ) {
    UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [aButton setTag:i];
    [aButton addTarget:self action:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];
    [aView addSubview:aButton];
}

然后:

- (void)buttonClicked:(UIButton*)button
{
    NSLog(@"Button %ld clicked.", (long int)[button tag]);
    // code here that picks the correct viewController to push to...
    // for example tag 1 would create an instance of vcTwo.m and would then be pushed to the navigationController and be displayed on screen
}

假设我有三个 UIViewController 类(vcOne.m、vcTwo.m、vcThree.m),我希望它在按下按钮时运行“buttonClicked”并且代码选择相应的 viewController 来推送。我不想使用一系列 if 语句,因为最终可能有几十个/几百个 viewController。我是否必须实例化所有的 viewController 并将它们放在一个数组中?有没有更好的办法?

【问题讨论】:

  • 每个按钮都有一个tag 属性。您可以使用它来区分按下了哪个按钮。 :)
  • @karthikeyan - 这些一定是更好的方法吗?还是我错过了什么?如果有 100 个 viewController 会怎样?我需要一个 100 个 case 长的 switch 语句,然后是 100 个硬编码方法。看起来不是很OO。
  • @AbhishekBedi - 你会在我的问题中看到我已经在捕获按钮的标签属性的值并从那里开始工作;)
  • 你必须实例化所有的 viewController 并将它们放在数组中 - (void)buttonClicked:(UIButton*)button { NSLog(@"Button %ld clicked.", (long int)[button tag ]); NSInteger 标签=[按钮标签]; [self.navigationController pushViewController:[commonArray objectAtIndex:tag] 动画:YES]; }

标签: ios iphone objective-c cocoa-touch


【解决方案1】:

你在使用故事板吗?所以你可以根据按钮标签选择一个segue:

int i = (int)[button tag];
[self performSegueWithIdentifier:[NSString stringWithFormat:@"Segue%d", i] sender:self];

或:

UIViewController *viewController= [controller.storyboard instantiateViewControllerWithIdentifier:NSString stringWithFormat:@"ViewControllerNumber%d", i];

【讨论】:

  • 我没有使用故事板,但想要类似于您的第二个建议的内容:[self.navigationController pushViewController:[vcArray objectAtIndex:i] animated:NO];
  • 我建议您在知道按下了哪些按钮之前不要分配和初始化所有控制器。您将免费浪费内存。给笔尖起一个暗示性的名字,这样你就可以把它们和按钮标签联系起来。尝试这样的事情: UIViewController *aViewController = [[UIViewController alloc] initWithNibName:[NSString stringWithFormat:@"ViewControllerNumber%d", buttonTag] bundle:[NSBundle mainBundle]];
  • 或者,如果您愿意,您可以将所有 nib 名称存储在 NSArray 中,然后为该按钮分配并初始化相应的 NibName。但是手动将每个 nib 名称放入数组中,工作量很大。
  • 这听起来很有希望。如前所述,我没有使用故事板,我现在需要链接到 UIViewController 类的类文件,我还能使用 nib 名称吗?
  • 我在第一条评论中使用的代码是使用 xib(不是情节提要)时可以初始化 UIViewController 的方式。 YourViewController *controller = [[YourViewController alloc] initWithNibName:@"YourCustomNibName" bundle:nil]; [self.navigationController pushViewController:controller animated:YES];
【解决方案2】:

最后我选择了这个:

- (void) buttonClicked:(id)sender
{
    NSLog(@"Button tag = %li", (long)[sender tag]);
    FormularyVC *formularyVCInstance = [FormularyVC alloc];
    ProceduresVC *proceduresVCInstance = [ProceduresVC alloc];
    VetMedVC *vetMedVCInstance = [VetMedVC alloc];

    NSArray *vcArray = [NSArray arrayWithObjects:formularyVCInstance, proceduresVCInstance, vetMedVCInstance, nil];

    UIViewController *vcToLoad = [vcArray objectAtIndex:(int)[sender tag]];
    vcToLoad.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:vcToLoad animated:NO];

}

我创建了一个 ViewControllers 数组,我希望能够根据按下的按钮进行加载。当按钮被按下时,方法运行并且标签被作为参数。此标签用于通过检查其在数组索引上的位置来查找所需的 ViewController。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    相关资源
    最近更新 更多