Ios Xcode 6-如何从另一个viewcontroller中的af开关隐藏一个viewcontroller中的文本字段

Ios Xcode 6-如何从另一个viewcontroller中的af开关隐藏一个viewcontroller中的文本字段,ios,objective-c,Ios,Objective C,我有一个带有开关按钮的视图控制器(firstViewController)。开关按钮应在secondViewController中显示/隐藏文本字段。我该怎么做呢?你的应用程序应该按照设计模式来构建。您需要这样做: 将BOOL hideText变量添加到模型类中 将model.hideText设置为开关中开关的值 选中视图中的model.hideText,将显示具有文本字段的视图的处理程序 如果设置了model.hideText,则隐藏文本字段;否则,请出示它 非常简单。您只需使用委托设计模式

我有一个带有开关按钮的视图控制器(firstViewController)。开关按钮应在secondViewController中显示/隐藏文本字段。我该怎么做呢?

你的应用程序应该按照设计模式来构建。您需要这样做:

  • BOOL hideText
    变量添加到模型类中
  • model.hideText
    设置为开关中开关的值
  • 选中
    视图中的
    model.hideText
    ,将显示具有文本字段的视图的处理程序
  • 如果设置了
    model.hideText
    ,则隐藏文本字段;否则,请出示它

非常简单。您只需使用
委托
设计模式。浏览此链接

在代码中,您必须执行类似的操作。我假设您的开关在secondViewCOntroller中,您必须在FirstViewController中的开关上隐藏textField

步骤1)像这样在第二个视图控制器中创建协议,并创建该委托的属性。在switchButtonPressed方法中传递开关的值,并在那里调用该委托方法

@protocol secondViewControllerDelegate <NSObject>

-(void)switchButtonPressedOn:(BOOL)switchOn;

@end

@interface secondViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISwitch *switchOutlet;
@property(weak,nonatomic)id<secondViewControllerDelegate>delegate;
- (IBAction)switchButtonPressed:(id)sender;

@end
}

步骤2) 在FirstViewController中

  #import "swiftViewController.h"

@interface ViewController :   UIViewController<secondViewControllerDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property BOOL mySwitchState;

@end
如果要在nextViewController中隐藏文本字段,请在
prepareforsguemethod
中将开关状态传递给nextViewController

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
thirdViewController *thirdVC=segue.destinationViewController;
if (self.switchOutlet.on==YES) {
thirdVC.isSomethingEnabled=YES;    
         }

   }
在nextViewController的
viewDidLoad
中,隐藏文本字段

    if (self.isSomethingEnabled==YES) {
    self.textField.hidden=YES;
}

工作得很好。这是iOS中非常重要的概念。浏览上面的链接。

第二个视图控制器是如何显示的?您现在尝试过什么吗?是的,但无法使其工作。。。。。。我不知道如何在我的第一个和第二个ViewController之间进行委托Hanks Saheb:-)我在firstViewController中有开关,希望第二个ViewController中的文本字段隐藏:-)它比以前更简单。我更新了答案。如果有帮助,请投票表决,正确答案为:-)
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
thirdViewController *thirdVC=segue.destinationViewController;
if (self.switchOutlet.on==YES) {
thirdVC.isSomethingEnabled=YES;    
         }

   }
    if (self.isSomethingEnabled==YES) {
    self.textField.hidden=YES;
}