【问题标题】:Add subviews to subview, but from another class将子视图添加到子视图,但来自另一个类
【发布时间】:2013-01-26 16:54:08
【问题描述】:

我有一个名为 FirstController 的 ViewController,它有 3 个按钮,每次触摸其中一个按钮时,它都会打开 SecondController(我的另一个 ViewController)。但即使所有三个按钮都打开同一个 ViewController,我也不希望 ViewController 完全相同,但它会有不同的对象,具体取决于按下的按钮。我在 SecondController 中有一个 ScrollView,我想根据按下的按钮将不同的图像作为子视图添加到 ScrollView 中。

这是我目前得到的:


#import "FirstController.h"
#import "SecondController.h"

@interface Level1 ()

@end

@implementation FirstController

- (IBAction) button1 {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
SecondController *ViewForButton1 = [mainStoryboard instantiateViewControllerWithIdentifier:@"View2"];
}
- (IBAction) button2 {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
SecondController *ViewForButton2 = [mainStoryboard instantiateViewControllerWithIdentifier:@"View2"];
}
- (IBAction) button3 {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
SecondController *ViewForButton3 = [mainStoryboard instantiateViewControllerWithIdentifier:@"View2"];
}

@end

我知道如何将图像添加为孔视图的子视图,但我需要它位于 ScrollView 中! 我现在如何为此类实现 ScrollView 并向其添加子视图?

PS:我有超过 3 个按钮,但在这个例子中我只使用了 3 个。

【问题讨论】:

    标签: ios cocoa-touch cocoa class subclass


    【解决方案1】:

    Tag 分配给每个UIButton 并在点击按钮时获取标签并将此标签传递给YourSecondViewController 并根据点击按钮放置要显示的图像的条件。

    【讨论】:

    • 如果我只有 3 个按钮,我可以做到这一点,但我有很多按钮。所以我真的需要一种将图像从 FirstController 添加到 ScrollView 的方法。
    【解决方案2】:

    按照您编写它的方式,您的 mainStoryboard 对象都已实例化,并且仅在创建它们的各个方法内具有范围。您的 ViewForButton_ 对象也是如此。他们有不同的名字这一事实是无关紧要的。

    仅凭这一点,它们就成为彼此不同的对象。它们可以有自己的内部状态,与同一类的任何其他对象不同。

    更新

    在每个按钮方法中试试这个:

    - (IBAction) button1 {
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    SecondController *ViewForButton1 = [mainStoryboard instantiateViewControllerWithIdentifier:@"View2"];
    ... create views to add to the second view controller here
    ... add he views that you created to the second view controller
    
    }
    

    在某些时候,我猜您想显示与第二个视图控制器关联的视图。我将把它留给你,但我假设你会用同样的方法做到这一点。

    顺便说一句,这与 AppDelegate 本身没有任何关系。

    【讨论】:

    • 你是在建议我应该完全不同吗?也许我可以使用 AppDelagate 来做到这一点?
    【解决方案3】:

    我会建议以不同的方式来做到这一点。在您的滚动视图中放置不同的视图应该在 SecondController 中的 viewDidLoad 方法中完成。要将项目添加到滚动视图,您需要一个 IBOutlet 到该滚动视图,并且在您第一次从 FirstController 实例化控制器时尚未设置。因此,我将只有一个按钮方法,并使用它来实例化 SecondController,并在其中设置一个属性(在我的示例中称为 buttonTag),该属性取决于按下的按钮的标签。

    -(IBAction)goToSecondController:(UIButton *)sender {
        SecondController *second = [self.storyboard instantiateViewControllerWithIdentifier:@"Next"];
        second.buttonTag = sender.tag;
        [self.navigationController pushViewController:second animated:YES];
    }
    

    然后在 SecondController 中,在 switch 语句中使用该属性来添加你想要的东西:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        switch (self.buttonTag) {
            case 1:
                [self.scrollView addsubview:someView];
                break;
            case 2:
                [self.scrollView addsubview:someOtherView];
                break;
            case 3:
                [self.scrollView addsubview:anotherView];
                break;
            default:
                break;
        }
    }
    

    【讨论】:

    • 我明白,我可能会使用它,但如果我说我可能会得到 100-150 种不同的情况,你仍然认为这是一个好的解决方案吗?
    • @Olaknorr,你是说你有 100-150 个按钮吗?如果是这样,您应该改用表格视图。如果不知道您要添加的所有这些对象是什么,就很难再说了。您可以使用按钮标签或表格行作为项目数组的索引。一切都取决于了解更多关于你在做什么的细节
    猜你喜欢
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多