gpt4 book ai didi

ios - 在不同的 View Controller 中执行一个 Action

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:50 25 4
gpt4 key购买 nike

在我的应用程序中,我试图创建一个带有按钮的按钮。我现在的代码如下:

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

}
}

这可行,但我需要添加的按钮添加到不同的 View Controller 中,而不是代码所在的 View Controller 中。我该怎么做?谢谢

最佳答案

您需要创建一个函数来在新的 View Controller 中添加按钮:

在 YourShortcutsViewController.h 中:

-(void)addFaceTimeButton:(UIButton*);

在 YourShortcutsViewController.m 中:

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

然后将您的代码更改为:

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

}

关于ios - 在不同的 View Controller 中执行一个 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21692122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com