Question

In my application, I am trying to create a button, with a button. My code right now is as follows:

- (IBAction)doneTextField:(id)sender {

    YourShortcutsViewController *YSVC2 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
    YSVC2.stringFromTextField = self.textField.text;
    [self presentViewController:YSVC2 animated:YES completion:nil];

    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        btn.frame = CGRectMake(0, 0, 100, 50);
        [btn addTarget:self action:@selector(newButtonClicked) forControlEvents:UIControlEventTouchUpInside];
            [btn setTitle:@"FaceTime" forState:UIControlStateNormal];
        [self.view addSubview:btn];

    }
}

this works, but I need the button that is being added to add in a different view controller, not the view controller that the code is in. How would I do this?? Thank you

Was it helpful?

Solution

You need to create a function to add the button in the new view controller:

In YourShortcutsViewController.h:

-(void)addFaceTimeButton:(UIButton*);

In YourShortcutsViewController.m:

-(void)addFaceTimeButton:(UIButton*){
    [self.view addSubview:btn];
}

And then change your code to:

- (IBAction)doneTextField:(id)sender {

    YourShortcutsViewController *YSVC2 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
    YSVC2.stringFromTextField = self.textField.text;
    [self presentViewController:YSVC2 animated:YES completion:nil];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(0, 0, 100, 50);
    [btn addTarget:self action:@selector(newButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"FaceTime" forState:UIControlStateNormal];

    [YSVC2 addFaceTimeButton:btn];

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top