简体   繁体   English

在两个视图控制器之间执行代码/触发UI元素

[英]Executing code/triggering UI Elements between two viewcontrollers

I'm new to objective C and design patterns like MVC, protocols and so on but this is it: 我对目标C和MVC,协议等设计模式不熟悉,仅此而已:

I am trying to write an iOS app within two viewcontrollers: the first has a textview where the user can write into, and the second has a UISwitch that triggers on "Value changed" and saves a file. 我正在尝试在两个视图控制器中编写一个iOS应用程序:第一个具有用户可以写入的textview,第二个具有UISwitch,可在“值更改”时触发并保存文件。

If I toggle by hand the switch on the SecondViewController it will save the file and that's ok. 如果我手动切换SecondViewController上的开关,它将保存文件,没关系。

But I wish the file could be saved from the FirstView just when the user types a specific word, it auto-switches to the second view, and auto-activates the UIswitch and all the method already behind it. 但是我希望当用户键入一个特定的单词时,可以从FirstView中保存文件,它会自动切换到第二个视图,并自动激活UIswitch及其后面的所有方法。

I still can't get the two interfaces working this way. 我仍然无法以这种方式使这两个界面正常工作。 Thanks everybody in advance for helping. 预先感谢大家的帮助。 Cheers! 干杯!


this is connected in SecondViewController.h in the storyboard 这是在情节提要中的SecondViewController.h中连接的

-(IBAction)toggleFileSave:(id)sender;

and it is implemented as usual... 并照常实施...

@interface SecondViewController ()
@property (nonatomic,weak) IBOutlet UISwitch *mySaveFileSwitch;
@end

- (void) toggleFileSave:(id)sender {
    // how do I execute this code when the user 
    // type a specific word in the first view?? 
}

Create a BOOL flag in your SecondViewController. 在您的SecondViewController中创建一个BOOL标志。 Set it when the specific word is typed and push the view controller. 在键入特定单词时进行设置,然后按下视图控制器。 In the viewDidLoad of SecondViewController check the flag condition.If it is set call the required method. 在SecondViewController的viewDidLoad中检查标志条件,如果已设置则调用所需的方法。

When the specific word is typed: 输入特定单词时:

   ViewController2 *viewController = [ViewController2 alloc]init];
   viewController2.flag = YES;
   [self.navigationController pushViewController:viewController2 animated:YES];

In your text field delegate (add one if it doesn't exist) add this method: 在您的文本字段委托中(如果不存在,请添加一个)添加以下方法:

- (void)textFieldDidEndEditing:(UITextField *)textField {
    /* at this point the user finished editing */
    NSString *currentText = /* read text field value */
    if ([currentText isEqualToString:/* the magic word */]) {
         /* save the file, present a view controller, etc. */
    }
}

Check UITextFieldDelegate to know the available methods, you may need more than one to get the desired behaviour. 检查UITextFieldDelegate以了解可用的方法,您可能需要多个方法才能获得所需的行为。

If you want to load the second view controller in order to show the UI and the save the file you can do as サンディープ said in his or her answer: 如果要加载第二个视图控制器以显示UI和保存文件,可以按照サンディープ在他或她的回答中所说的那样进行:

SecondViewController *controller = [SecondViewController new]; /* init as usual */
controller.saveOnLoad = YES;

[self.navigationController pushViewController:controller animated:YES]; [self.navigationController pushViewController:controller动画:是];

Then, in SecondViewController : 然后,在SecondViewController

- (void)viewDidLoad {
    if (self.saveOnLoad) {
        /* save file in async block */
        /* set switch on */
    }
}

If you don't need to show the second view I'd move the saving functionality to its own class and use it from the first controller, showing just a confirmation message for instance. 如果您不需要显示第二个视图,则将保存功能移至其自己的类,并从第一个控制器使用它,例如仅显示一条确认消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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