简体   繁体   中英

Add subviews to subview, but from another class

I have a ViewController named FirstController that have 3 buttons, and each time one of those buttons get touched it open the SecondController(my other ViewController). But even tho all three buttons opens the same ViewController, I don't want that ViewController to be exactly the same, but it will have different objects in it, depending on what button was pressed. I have a ScrollView in the SecondController, and I want to add different images into the ScrollView as subviews depending on what button was pressed.

Here's what I got so far:


#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

I know how I can add the images as subviews of the hole View, but i need it to be in the ScrollView! How can I now implement the ScrollView to this class and add subviews to it?

PS: I got more that 3 buttons, but I only use 3 in this example.

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

The way you wrote it, your mainStoryboard objects are all instantiated and have scope only within the individual methods where they are created. The same is true for your ViewForButton_ objects. The fact that they have different names is irrelevant.

This makes them, by that fact alone, different objects from one another. They can have there own internal state that is different from any other object of the same class.

UPDATE

Try this in each button method:

- (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

}

At some point, I guess you want to show the view associated with the second view controller. I'll leave that up to you, but I assume you will do that within the same method.

Noe of this has anything to do with the AppDelegate, per se, by the way.

I would suggest as different way to do this. Putting different views in your scroll view should be done from within SecondController, in its viewDidLoad method. To add items to the scroll view, you will need an IBOutlet to that scroll view, and that won't be set yet when you first instantiate the controller from FirstController. So, I would just have one button method, and use it to instantiate a SecondController, and set a property in it (called buttonTag in my example) that's dependent on the tag of the button that was pressed.

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

Then in the SecondController, use that property in a switch statement to add the things you want:

- (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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM